forked from graphql-go/graphql
-
Notifications
You must be signed in to change notification settings - Fork 0
/
introspection.go
1009 lines (954 loc) · 28 KB
/
introspection.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package graphql
import (
"fmt"
"reflect"
"sort"
"github.com/machship/graphql/language/ast"
"github.com/machship/graphql/language/printer"
)
const (
TypeKindScalar = "SCALAR"
TypeKindObject = "OBJECT"
TypeKindInterface = "INTERFACE"
TypeKindUnion = "UNION"
TypeKindEnum = "ENUM"
TypeKindInputObject = "INPUT_OBJECT"
TypeKindList = "LIST"
TypeKindNonNull = "NON_NULL"
)
const (
appliedDirectivesField string = "appliedDirectives"
argIncludeNonStandard string = "includeNonStandard"
)
var (
// SchemaType is the type definition of __Schema.
SchemaType *Object
// DirectiveType is the type definition of __Directive.
DirectiveType *Object
// TypeType is the type definition of __Type.
TypeType *Object
// FieldType is the type definition of __Field.
FieldType *Object
// InputValueType is the type definition of __InputValue.
InputValueType *Object
// EnumValueType is the type definition of __EnumValue.
EnumValueType *Object
// TypeKindEnumType is the type definition of __TypeKind.
TypeKindEnumType *Enum
// DirectiveLocationEnumType is the type definition of __DirectiveLocation.
DirectiveLocationEnumType *Enum
// DirectiveArgumentType is the type definition for __DirectiveArgument.
//
// Note: __DirectiveArgument is not a part of the official graphql specification. It
// is an extension to the specification implemented by the graphql-dotnet and
// graphql-java libraries.
DirectiveArgumentType *Object
// AppliedDirectiveType is the type definition of __AppliedDirective.
//
// Note: __AppliedDirective is not a part of the official graphql specification. It
// is an extension to the specification implemented by the graphql-dotnet and
// graphql-java libraries.
AppliedDirectiveType *Object
// Meta-field definitions.
// SchemaMetaFieldDef Meta field definition for Schema
SchemaMetaFieldDef *FieldDefinition
// TypeMetaFieldDef Meta field definition for types
TypeMetaFieldDef *FieldDefinition
// TypeNameMetaFieldDef Meta field definition for type names
TypeNameMetaFieldDef *FieldDefinition
)
func init() {
DirectiveArgumentType = NewObject(ObjectConfig{
Name: "__DirectiveArgument",
Description: "A Directive Argument is a name/value pair that can be passed to a directive",
Fields: Fields{
"name": {
Type: NewNonNull(String),
Description: "The name of the directive argument",
Resolve: func(p ResolveParams) (any, error) {
if arg, ok := p.Source.(*DirectiveArgument); ok {
return arg.Name, nil
}
return nil, nil
},
},
"value": {
Type: NewNonNull(String),
Description: "The value of the directive argument",
Resolve: func(p ResolveParams) (any, error) {
if arg, ok := p.Source.(*DirectiveArgument); ok {
return arg.Value, nil
}
return nil, nil
},
},
},
})
AppliedDirectiveType = NewObject(ObjectConfig{
Name: "__AppliedDirective",
Description: "An Applied Directive is a directive that has been applied to a field, " +
"argument, input field, or type",
Fields: Fields{
"name": {
Type: NewNonNull(String),
Description: "The name of the applied directive",
},
"args": {
Type: NewNonNull(
NewList(
NewNonNull(
DirectiveArgumentType,
),
),
),
Description: "The arguments of the applied directive",
},
},
})
TypeKindEnumType = NewEnum(EnumConfig{
Name: "__TypeKind",
Description: "An enum describing what kind of type a given `__Type` is",
Values: EnumValueConfigMap{
"SCALAR": &EnumValueConfig{
Value: TypeKindScalar,
Description: "Indicates this type is a scalar.",
},
"OBJECT": &EnumValueConfig{
Value: TypeKindObject,
Description: "Indicates this type is an object. " +
"`fields` and `interfaces` are valid fields.",
},
"INTERFACE": &EnumValueConfig{
Value: TypeKindInterface,
Description: "Indicates this type is an interface. " +
"`fields` and `possibleTypes` are valid fields.",
},
"UNION": &EnumValueConfig{
Value: TypeKindUnion,
Description: "Indicates this type is a union. " +
"`possibleTypes` is a valid field.",
},
"ENUM": &EnumValueConfig{
Value: TypeKindEnum,
Description: "Indicates this type is an enum. " +
"`enumValues` is a valid field.",
},
"INPUT_OBJECT": &EnumValueConfig{
Value: TypeKindInputObject,
Description: "Indicates this type is an input object. " +
"`inputFields` is a valid field.",
},
"LIST": &EnumValueConfig{
Value: TypeKindList,
Description: "Indicates this type is a list. " +
"`ofType` is a valid field.",
},
"NON_NULL": &EnumValueConfig{
Value: TypeKindNonNull,
Description: "Indicates this type is a non-null. " +
"`ofType` is a valid field.",
},
},
})
DirectiveLocationEnumType = NewEnum(EnumConfig{
Name: "__DirectiveLocation",
Description: "A Directive can be adjacent to many parts of the GraphQL language, a " +
"__DirectiveLocation describes one such possible adjacencies.",
Values: EnumValueConfigMap{
"QUERY": &EnumValueConfig{
Value: DirectiveLocationQuery,
Description: "Location adjacent to a query operation.",
},
"MUTATION": &EnumValueConfig{
Value: DirectiveLocationMutation,
Description: "Location adjacent to a mutation operation.",
},
"SUBSCRIPTION": &EnumValueConfig{
Value: DirectiveLocationSubscription,
Description: "Location adjacent to a subscription operation.",
},
"FIELD": &EnumValueConfig{
Value: DirectiveLocationField,
Description: "Location adjacent to a field.",
},
"FRAGMENT_DEFINITION": &EnumValueConfig{
Value: DirectiveLocationFragmentDefinition,
Description: "Location adjacent to a fragment definition.",
},
"FRAGMENT_SPREAD": &EnumValueConfig{
Value: DirectiveLocationFragmentSpread,
Description: "Location adjacent to a fragment spread.",
},
"INLINE_FRAGMENT": &EnumValueConfig{
Value: DirectiveLocationInlineFragment,
Description: "Location adjacent to an inline fragment.",
},
"SCHEMA": &EnumValueConfig{
Value: DirectiveLocationSchema,
Description: "Location adjacent to a schema definition.",
},
"SCALAR": &EnumValueConfig{
Value: DirectiveLocationScalar,
Description: "Location adjacent to a scalar definition.",
},
"OBJECT": &EnumValueConfig{
Value: DirectiveLocationObject,
Description: "Location adjacent to a object definition.",
},
"FIELD_DEFINITION": &EnumValueConfig{
Value: DirectiveLocationFieldDefinition,
Description: "Location adjacent to a field definition.",
},
"ARGUMENT_DEFINITION": &EnumValueConfig{
Value: DirectiveLocationArgumentDefinition,
Description: "Location adjacent to an argument definition.",
},
"INTERFACE": &EnumValueConfig{
Value: DirectiveLocationInterface,
Description: "Location adjacent to an interface definition.",
},
"UNION": &EnumValueConfig{
Value: DirectiveLocationUnion,
Description: "Location adjacent to a union definition.",
},
"ENUM": &EnumValueConfig{
Value: DirectiveLocationEnum,
Description: "Location adjacent to an enum definition.",
},
"ENUM_VALUE": &EnumValueConfig{
Value: DirectiveLocationEnumValue,
Description: "Location adjacent to an enum value definition.",
},
"INPUT_OBJECT": &EnumValueConfig{
Value: DirectiveLocationInputObject,
Description: "Location adjacent to an input object type definition.",
},
"INPUT_FIELD_DEFINITION": &EnumValueConfig{
Value: DirectiveLocationInputFieldDefinition,
Description: "Location adjacent to an input object field definition.",
},
},
})
// Note: some fields (for e.g "fields", "interfaces") are defined later due to cyclic reference
TypeType = NewObject(ObjectConfig{
Name: "__Type",
Description: "The fundamental unit of any GraphQL Schema is the type. There are " +
"many kinds of types in GraphQL as represented by the `__TypeKind` enum." +
"\n\nDepending on the kind of a type, certain fields describe " +
"information about that type. Scalar types provide no information " +
"beyond a name and description, while Enum types provide their values. " +
"Object and Interface types provide the fields they describe. Abstract " +
"types, Union and Interface, provide the Object types possible " +
"at runtime. List and NonNull types compose other types.",
Fields: Fields{
"kind": &Field{
Type: NewNonNull(TypeKindEnumType),
Resolve: func(p ResolveParams) (any, error) {
switch p.Source.(type) {
case *Scalar:
return TypeKindScalar, nil
case *Object:
return TypeKindObject, nil
case *Interface:
return TypeKindInterface, nil
case *Union:
return TypeKindUnion, nil
case *Enum:
return TypeKindEnum, nil
case *InputObject:
return TypeKindInputObject, nil
case *List:
return TypeKindList, nil
case *NonNull:
return TypeKindNonNull, nil
}
return nil, fmt.Errorf("unknown kind of type: %v", p.Source)
},
},
"name": &Field{
Type: String,
},
"description": &Field{
Type: String,
},
"fields": &Field{},
"interfaces": &Field{},
"possibleTypes": &Field{},
"enumValues": &Field{},
"inputFields": &Field{},
"ofType": &Field{},
appliedDirectivesField: {
Resolve: appliedDirectiveResolver,
Type: NewNonNull(
NewList(
NewNonNull(
AppliedDirectiveType,
),
),
),
},
},
})
InputValueType = NewObject(ObjectConfig{
Name: "__InputValue",
Description: "Arguments provided to Fields or Directives and the input fields of an " +
"InputObject are represented as Input Values which describe their type " +
"and optionally a default value.",
Fields: Fields{
"name": &Field{
Type: NewNonNull(String),
},
"description": &Field{
Type: String,
},
"type": &Field{
Type: NewNonNull(TypeType),
},
"defaultValue": &Field{
Type: String,
Description: "A GraphQL-formatted string representing the default value for this " +
"input value.",
Resolve: func(p ResolveParams) (any, error) {
if inputVal, ok := p.Source.(*Argument); ok {
if inputVal.DefaultValue == nil {
return nil, nil
}
if isNullish(inputVal.DefaultValue) {
return nil, nil
}
astVal := astFromValue(inputVal.DefaultValue, inputVal)
return printer.Print(astVal), nil
}
if inputVal, ok := p.Source.(*InputObjectField); ok {
if inputVal.DefaultValue == nil {
return nil, nil
}
astVal := astFromValue(inputVal.DefaultValue, inputVal)
return printer.Print(astVal), nil
}
return nil, nil
},
},
appliedDirectivesField: {
Resolve: appliedDirectiveResolver,
Type: NewNonNull(
NewList(
NewNonNull(
AppliedDirectiveType,
),
),
),
},
},
})
FieldType = NewObject(ObjectConfig{
Name: "__Field",
Description: "Object and Interface types are described by a list of Fields, each of " +
"which has a name, potentially a list of arguments, and a return type.",
Fields: Fields{
"name": &Field{
Type: NewNonNull(String),
},
"description": &Field{
Type: String,
},
"args": &Field{
Type: NewNonNull(NewList(NewNonNull(InputValueType))),
Resolve: func(p ResolveParams) (any, error) {
if field, ok := p.Source.(*FieldDefinition); ok {
return field.Args, nil
}
return []any{}, nil
},
},
"type": &Field{
Type: NewNonNull(TypeType),
},
"isDeprecated": &Field{
Type: NewNonNull(Boolean),
Resolve: func(p ResolveParams) (any, error) {
if field, ok := p.Source.(*FieldDefinition); ok {
return (field.DeprecationReason != ""), nil
}
return false, nil
},
},
"deprecationReason": &Field{
Type: String,
Resolve: func(p ResolveParams) (any, error) {
if field, ok := p.Source.(*FieldDefinition); ok {
if field.DeprecationReason != "" {
return field.DeprecationReason, nil
}
}
return nil, nil
},
},
appliedDirectivesField: {
Resolve: appliedDirectiveResolver,
Type: NewNonNull(
NewList(
NewNonNull(
AppliedDirectiveType,
),
),
),
},
},
})
DirectiveType = NewObject(ObjectConfig{
Name: "__Directive",
Description: "A Directive provides a way to describe alternate runtime execution and " +
"type validation behavior in a GraphQL document. " +
"\n\nIn some cases, you need to provide options to alter GraphQL's " +
"execution behavior in ways field arguments will not suffice, such as " +
"conditionally including or skipping a field. Directives provide this by " +
"describing additional information to the executor.",
Fields: Fields{
"name": &Field{
Type: NewNonNull(String),
},
"description": &Field{
Type: String,
},
"locations": &Field{
Type: NewNonNull(NewList(
NewNonNull(DirectiveLocationEnumType),
)),
},
"args": &Field{
Type: NewNonNull(NewList(
NewNonNull(InputValueType),
)),
},
// NOTE: the following three fields are deprecated and are no longer part
// of the GraphQL specification.
"onOperation": &Field{
DeprecationReason: "Use `locations`.",
Type: NewNonNull(Boolean),
Resolve: func(p ResolveParams) (any, error) {
if dir, ok := p.Source.(*Directive); ok {
res := false
for _, loc := range dir.Locations {
if loc == DirectiveLocationQuery ||
loc == DirectiveLocationMutation ||
loc == DirectiveLocationSubscription {
res = true
break
}
}
return res, nil
}
return false, nil
},
},
"onFragment": &Field{
DeprecationReason: "Use `locations`.",
Type: NewNonNull(Boolean),
Resolve: func(p ResolveParams) (any, error) {
if dir, ok := p.Source.(*Directive); ok {
res := false
for _, loc := range dir.Locations {
if loc == DirectiveLocationFragmentSpread ||
loc == DirectiveLocationInlineFragment ||
loc == DirectiveLocationFragmentDefinition {
res = true
break
}
}
return res, nil
}
return false, nil
},
},
"onField": &Field{
DeprecationReason: "Use `locations`.",
Type: NewNonNull(Boolean),
Resolve: func(p ResolveParams) (any, error) {
if dir, ok := p.Source.(*Directive); ok {
res := false
for _, loc := range dir.Locations {
if loc == DirectiveLocationField {
res = true
break
}
}
return res, nil
}
return false, nil
},
},
appliedDirectivesField: {
Resolve: appliedDirectiveResolver,
Type: NewNonNull(
NewList(
NewNonNull(
AppliedDirectiveType,
),
),
),
},
},
})
SchemaType = NewObject(ObjectConfig{
Name: "__Schema",
Description: `A GraphQL Schema defines the capabilities of a GraphQL server. ` +
`It exposes all available types and directives on the server, as well as ` +
`the entry points for query, mutation, and subscription operations.`,
Fields: Fields{
"types": &Field{
Description: "A list of all types supported by this server.",
Type: NewNonNull(NewList(
NewNonNull(TypeType),
)),
Resolve: func(p ResolveParams) (any, error) {
if schema, ok := p.Source.(Schema); ok {
results := []Type{}
for _, ttype := range schema.TypeMap() {
results = append(results, ttype)
}
return results, nil
}
return []Type{}, nil
},
},
"queryType": &Field{
Description: "The type that query operations will be rooted at.",
Type: NewNonNull(TypeType),
Resolve: func(p ResolveParams) (any, error) {
if schema, ok := p.Source.(Schema); ok {
return schema.QueryType(), nil
}
return nil, nil
},
},
"mutationType": &Field{
Description: `If this server supports mutation, the type that ` +
`mutation operations will be rooted at.`,
Type: TypeType,
Resolve: func(p ResolveParams) (any, error) {
if schema, ok := p.Source.(Schema); ok {
if schema.MutationType() != nil {
return schema.MutationType(), nil
}
}
return nil, nil
},
},
"subscriptionType": &Field{
Description: `If this server supports subscription, the type that ` +
`subscription operations will be rooted at.`,
Type: TypeType,
Resolve: func(p ResolveParams) (any, error) {
if schema, ok := p.Source.(Schema); ok {
if schema.SubscriptionType() != nil {
return schema.SubscriptionType(), nil
}
}
return nil, nil
},
},
"directives": &Field{
Description: `A list of all directives supported by this server.`,
Type: NewNonNull(NewList(
NewNonNull(DirectiveType),
)),
Resolve: func(p ResolveParams) (any, error) {
if schema, ok := p.Source.(Schema); ok {
return schema.Directives(), nil
}
return nil, nil
},
},
appliedDirectivesField: {
Resolve: func(p ResolveParams) (any, error) {
// TODO: figure out why `Schema` is not being passed as a pointer
if schema, ok := p.Source.(Schema); ok {
return schema.AppliedDirectives(), nil
}
return nil, nil
},
Type: NewNonNull(
NewList(
NewNonNull(
AppliedDirectiveType,
),
),
),
},
},
})
EnumValueType = NewObject(ObjectConfig{
Name: "__EnumValue",
Description: "One possible value for a given Enum. Enum values are unique values, not " +
"a placeholder for a string or numeric value. However an Enum value is " +
"returned in a JSON response as a string.",
Fields: Fields{
"name": &Field{
Type: NewNonNull(String),
},
"description": &Field{
Type: String,
},
"isDeprecated": &Field{
Type: NewNonNull(Boolean),
Resolve: func(p ResolveParams) (any, error) {
if field, ok := p.Source.(*EnumValueDefinition); ok {
return (field.DeprecationReason != ""), nil
}
return false, nil
},
},
"deprecationReason": &Field{
Type: String,
Resolve: func(p ResolveParams) (any, error) {
if field, ok := p.Source.(*EnumValueDefinition); ok {
if field.DeprecationReason != "" {
return field.DeprecationReason, nil
}
}
return nil, nil
},
},
appliedDirectivesField: {
Resolve: appliedDirectiveResolver,
Type: NewNonNull(
NewList(
NewNonNull(
AppliedDirectiveType,
),
),
),
},
},
})
// Again, adding field configs to __Type that have cyclic reference here
// because golang don't like them too much during init/compile-time
TypeType.AddFieldConfig("fields", &Field{
Type: NewList(NewNonNull(FieldType)),
Args: FieldConfigArgument{
"includeDeprecated": &ArgumentConfig{
Type: Boolean,
DefaultValue: false,
},
},
Resolve: func(p ResolveParams) (any, error) {
includeDeprecated, _ := p.Args["includeDeprecated"].(bool)
switch ttype := p.Source.(type) {
case *Object:
if ttype == nil {
return nil, nil
}
fields := []*FieldDefinition{}
var fieldNames sort.StringSlice
for name, field := range ttype.Fields() {
if !includeDeprecated && field.DeprecationReason != "" {
continue
}
fieldNames = append(fieldNames, name)
}
sort.Sort(fieldNames)
for _, name := range fieldNames {
fields = append(fields, ttype.Fields()[name])
}
return fields, nil
case *Interface:
if ttype == nil {
return nil, nil
}
fields := []*FieldDefinition{}
for _, field := range ttype.Fields() {
if !includeDeprecated && field.DeprecationReason != "" {
continue
}
fields = append(fields, field)
}
return fields, nil
}
return nil, nil
},
})
TypeType.AddFieldConfig("interfaces", &Field{
Type: NewList(NewNonNull(TypeType)),
Resolve: func(p ResolveParams) (any, error) {
if ttype, ok := p.Source.(*Object); ok {
return ttype.Interfaces(), nil
}
return nil, nil
},
})
TypeType.AddFieldConfig("possibleTypes", &Field{
Type: NewList(NewNonNull(TypeType)),
Resolve: func(p ResolveParams) (any, error) {
switch ttype := p.Source.(type) {
case *Interface:
return p.Info.Schema.PossibleTypes(ttype), nil
case *Union:
return p.Info.Schema.PossibleTypes(ttype), nil
}
return nil, nil
},
})
TypeType.AddFieldConfig("enumValues", &Field{
Type: NewList(NewNonNull(EnumValueType)),
Args: FieldConfigArgument{
"includeDeprecated": &ArgumentConfig{
Type: Boolean,
DefaultValue: false,
},
},
Resolve: func(p ResolveParams) (any, error) {
includeDeprecated, _ := p.Args["includeDeprecated"].(bool)
if ttype, ok := p.Source.(*Enum); ok {
if includeDeprecated {
return ttype.Values(), nil
}
values := []*EnumValueDefinition{}
for _, value := range ttype.Values() {
if value.DeprecationReason != "" {
continue
}
values = append(values, value)
}
return values, nil
}
return nil, nil
},
})
TypeType.AddFieldConfig("inputFields", &Field{
Type: NewList(NewNonNull(InputValueType)),
Resolve: func(p ResolveParams) (any, error) {
if ttype, ok := p.Source.(*InputObject); ok {
fields := []*InputObjectField{}
for _, field := range ttype.Fields() {
fields = append(fields, field)
}
return fields, nil
}
return nil, nil
},
})
TypeType.AddFieldConfig("ofType", &Field{
Type: TypeType,
})
DirectiveArgumentType.ensureCache()
AppliedDirectiveType.ensureCache()
SchemaType.ensureCache()
DirectiveType.ensureCache()
TypeType.ensureCache()
FieldType.ensureCache()
InputValueType.ensureCache()
EnumValueType.ensureCache()
// Note that these are FieldDefinition and not FieldConfig,
// so the format for args is different.
SchemaMetaFieldDef = &FieldDefinition{
Name: "__schema",
Type: NewNonNull(SchemaType),
Description: "Access the current type schema of this server.",
Args: []*Argument{
{
PrivateName: argIncludeNonStandard,
Type: Boolean,
},
},
Resolve: func(p ResolveParams) (any, error) {
raw, ok := p.Args[argIncludeNonStandard]
if ok {
nonStandard, ok := raw.(bool)
if !ok {
return nil, fmt.Errorf("failed to assert argument %q: %v (%T) as bool", argIncludeNonStandard, raw, raw)
}
if nonStandard {
return p.Info.Schema, nil
}
}
typeMap, err := newStandardTypeMap(p.Info.Schema.typeMap)
if err != nil {
return nil, fmt.Errorf("failed to create standard type map: %w", err)
}
schema := p.Info.Schema
schema.typeMap = typeMap
return schema, nil
},
}
TypeMetaFieldDef = &FieldDefinition{
Name: "__type",
Type: TypeType,
Description: "Request the type information of a single type.",
Args: []*Argument{
{
PrivateName: "name",
Type: NewNonNull(String),
},
},
Resolve: func(p ResolveParams) (any, error) {
name, ok := p.Args["name"].(string)
if !ok {
return nil, nil
}
return p.Info.Schema.Type(name), nil
},
}
TypeNameMetaFieldDef = &FieldDefinition{
Name: "__typename",
Type: NewNonNull(String),
Description: "The name of the current Object type at runtime.",
Args: []*Argument{},
Resolve: func(p ResolveParams) (any, error) {
return p.Info.ParentType.Name(), nil
},
}
}
// appliedDirectiveResolver is a resolver to be used where types return
// an `appliedDirectives` field.
func appliedDirectiveResolver(p ResolveParams) (any, error) {
if adp, ok := p.Source.(AppliedDirectiveProvider); ok {
return adp.AppliedDirectives(), nil
}
return nil, nil
}
// Produces a GraphQL Value AST given a Golang value.
//
// Optionally, a GraphQL type may be provided, which will be used to
// disambiguate between value primitives.
//
// | JSON Value | GraphQL Value |
// | ------------- | -------------------- |
// | Object | Input Object |
// | Array | List |
// | Boolean | Boolean |
// | String | String / Enum Value |
// | Number | Int / Float |
func astFromValue(value any, ttype Type) ast.Value {
if ttype, ok := ttype.(*NonNull); ok {
// Note: we're not checking that the result is non-null.
// This function is not responsible for validating the input value.
val := astFromValue(value, ttype.OfType)
return val
}
if isNullish(value) {
return nil
}
valueVal := reflect.ValueOf(value)
if !valueVal.IsValid() {
return nil
}
if valueVal.Type().Kind() == reflect.Ptr {
valueVal = valueVal.Elem()
}
if !valueVal.IsValid() {
return nil
}
// Convert Golang slice to GraphQL list. If the Type is a list, but
// the value is not an array, convert the value using the list's item type.
if ttype, ok := ttype.(*List); ok {
if valueVal.Type().Kind() == reflect.Slice {
itemType := ttype.OfType
values := []ast.Value{}
for i := 0; i < valueVal.Len(); i++ {
item := valueVal.Index(i).Interface()
itemAST := astFromValue(item, itemType)
if itemAST != nil {
values = append(values, itemAST)
}
}
return ast.NewListValue(&ast.ListValue{
Values: values,
})
}
// Because GraphQL will accept single values as a "list of one" when
// expecting a list, if there's a non-array value and an expected list type,
// create an AST using the list's item type.
val := astFromValue(value, ttype.OfType)
return val
}
if valueVal.Type().Kind() == reflect.Map {
// TODO: implement astFromValue from Map to Value
}
switch v := value.(type) {
case bool:
return ast.NewBooleanValue(&ast.BooleanValue{
Value: v,
})
case int:
if ttype == Float {
return ast.NewIntValue(&ast.IntValue{
Value: fmt.Sprintf("%v.0", v),
})
}
return ast.NewIntValue(&ast.IntValue{
Value: fmt.Sprintf("%v", v),
})
case float32, float64:
return ast.NewFloatValue(&ast.FloatValue{
Value: fmt.Sprintf("%v", v),
})
case string:
if _, ok := ttype.(*Enum); ok {
return ast.NewEnumValue(&ast.EnumValue{
Value: fmt.Sprintf("%v", v),
})
}
return ast.NewStringValue(&ast.StringValue{
Value: fmt.Sprintf("%v", v),
})
}
// fallback, treat as string
return ast.NewStringValue(&ast.StringValue{
Value: fmt.Sprintf("%v", value),
})
}
// newStandardTypeMap returns a copy of the provided TypeMap with all
// non-standard types removed. It also removes the non-standard fields
// of the standard types.
func newStandardTypeMap(in TypeMap) (TypeMap, error) {
// subtract two for __AppliedDirective and __DirectiveArgument
out := make(TypeMap, len(in)-2)
for k, v := range in {
switch k {
case DirectiveArgumentType.PrivateName,
AppliedDirectiveType.PrivateName:
// skip non-standard types
continue
case SchemaType.PrivateName,
DirectiveType.PrivateName,
TypeType.PrivateName,
FieldType.PrivateName,
InputValueType.PrivateName,
EnumValueType.PrivateName:
obj, ok := v.(*Object)
if !ok {
// this should never happen because the types are defined
// as objects in the package-level variables above
return nil, fmt.Errorf("could not assert value at key %q as *Object; found %T", k, v)
}
v = &Object{
typeConfig: obj.typeConfig,
initialisedFields: obj.initialisedFields,
fields: newStandardFieldDefinitionMap(obj.fields),
initialisedInterfaces: obj.initialisedInterfaces,
interfaces: obj.interfaces,
err: obj.err,
PrivateName: obj.PrivateName,
PrivateDescription: obj.PrivateDescription,
IsTypeOf: obj.IsTypeOf,
}
}
out[k] = v
}
return out, nil
}
// newStandardFieldDefinitionMap returns a copy of the provided FieldDefinitionMap with
// all non-standard fields removed.
func newStandardFieldDefinitionMap(in FieldDefinitionMap) FieldDefinitionMap {
// subtract one for appliedDirectives field
out := make(FieldDefinitionMap, len(in)-1)
for k, v := range in {