forked from GraphQLSwift/GraphQL
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDefinition.swift
More file actions
1459 lines (1287 loc) · 43.7 KB
/
Definition.swift
File metadata and controls
1459 lines (1287 loc) · 43.7 KB
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
import Foundation
import OrderedCollections
/**
* These are all of the possible kinds of types.
*/
public protocol GraphQLType: CustomDebugStringConvertible, Sendable {}
extension GraphQLScalarType: GraphQLType {}
extension GraphQLObjectType: GraphQLType {}
extension GraphQLInterfaceType: GraphQLType {}
extension GraphQLUnionType: GraphQLType {}
extension GraphQLEnumType: GraphQLType {}
extension GraphQLInputObjectType: GraphQLType {}
extension GraphQLList: GraphQLType {}
extension GraphQLNonNull: GraphQLType {}
/**
* These types may be used as input types for arguments and directives.
*/
public protocol GraphQLInputType: GraphQLType {}
extension GraphQLScalarType: GraphQLInputType {}
extension GraphQLEnumType: GraphQLInputType {}
extension GraphQLInputObjectType: GraphQLInputType {}
extension GraphQLList: GraphQLInputType {}
extension GraphQLNonNull: GraphQLInputType {}
// TODO: Conditional conformances
// extension GraphQLList : GraphQLInputType where Element : GraphQLInputType {}
// extension GraphQLNonNull : GraphQLInputType where Element : (GraphQLScalarType | GraphQLEnumType
// | GraphQLInputObjectType | GraphQLList<GraphQLInputType>) {}
func isInputType(type: GraphQLType?) -> Bool {
let namedType = getNamedType(type: type)
return namedType is GraphQLInputType
}
/**
* These types may be used as output types as the result of fields.
*/
public protocol GraphQLOutputType: GraphQLType {}
extension GraphQLScalarType: GraphQLOutputType {}
extension GraphQLObjectType: GraphQLOutputType {}
extension GraphQLInterfaceType: GraphQLOutputType {}
extension GraphQLUnionType: GraphQLOutputType {}
extension GraphQLEnumType: GraphQLOutputType {}
extension GraphQLList: GraphQLOutputType {}
extension GraphQLNonNull: GraphQLOutputType {}
// TODO: Conditional conformances
// extension GraphQLList : GraphQLOutputType where Element : GraphQLOutputType {}
// extension GraphQLNonNull : GraphQLInputType where Element : (GraphQLScalarType |
// GraphQLObjectType | GraphQLInterfaceType | GraphQLUnionType | GraphQLEnumType |
// GraphQLList<GraphQLOutputType>) {}
/**
* These types may describe types which may be leaf values.
*/
public protocol GraphQLLeafType: GraphQLNamedType {
func serialize(value: Any) throws -> Map
func parseValue(value: Map) throws -> Map
func parseLiteral(valueAST: Value) throws -> Map
}
extension GraphQLScalarType: GraphQLLeafType {}
extension GraphQLEnumType: GraphQLLeafType {}
func isLeafType(type: GraphQLType?) -> Bool {
let namedType = getNamedType(type: type)
return namedType is GraphQLScalarType ||
namedType is GraphQLEnumType
}
/**
* These types may describe the parent context of a selection set.
*/
public protocol GraphQLCompositeType: GraphQLNamedType, GraphQLOutputType {}
extension GraphQLObjectType: GraphQLCompositeType {}
extension GraphQLInterfaceType: GraphQLCompositeType {}
extension GraphQLUnionType: GraphQLCompositeType {}
/**
* These types may describe the parent context of a selection set.
*/
public protocol GraphQLAbstractType: GraphQLNamedType {
var resolveType: GraphQLTypeResolve? { get }
}
extension GraphQLInterfaceType: GraphQLAbstractType {}
extension GraphQLUnionType: GraphQLAbstractType {}
/**
* These types can all accept null as a value.
*/
public protocol GraphQLNullableType: GraphQLType {}
extension GraphQLScalarType: GraphQLNullableType {}
extension GraphQLObjectType: GraphQLNullableType {}
extension GraphQLInterfaceType: GraphQLNullableType {}
extension GraphQLUnionType: GraphQLNullableType {}
extension GraphQLEnumType: GraphQLNullableType {}
extension GraphQLInputObjectType: GraphQLNullableType {}
extension GraphQLList: GraphQLNullableType {}
func getNullableType(type: GraphQLType?) -> GraphQLNullableType? {
if let type = type as? GraphQLNonNull {
return type.ofType
}
return type as? GraphQLNullableType
}
/**
* These named types do not include modifiers like List or NonNull.
*/
public protocol GraphQLNamedType: GraphQLNullableType {
var name: String { get }
}
extension GraphQLScalarType: GraphQLNamedType {}
extension GraphQLObjectType: GraphQLNamedType {}
extension GraphQLInterfaceType: GraphQLNamedType {}
extension GraphQLUnionType: GraphQLNamedType {}
extension GraphQLEnumType: GraphQLNamedType {}
extension GraphQLInputObjectType: GraphQLNamedType {}
public func getNamedType(type: GraphQLType?) -> GraphQLNamedType? {
var unmodifiedType = type
while let type = unmodifiedType as? GraphQLWrapperType {
unmodifiedType = type.wrappedType
}
return unmodifiedType as? GraphQLNamedType
}
/**
* These types wrap other types.
*/
protocol GraphQLWrapperType: GraphQLType {
var wrappedType: GraphQLType { get }
}
extension GraphQLList: GraphQLWrapperType {}
extension GraphQLNonNull: GraphQLWrapperType {}
/**
* Scalar Type Definition
*
* The leaf values of any request and input values to arguments are
* Scalars (or Enums) and are defined with a name and a series of functions
* used to parse input from ast or variables and to ensure validity.
*
* Example:
*
* let oddType = try ScalarType(
* name: "Bool",
* serialize: {
* try $0.map.asBool(converting: true)
* }
* )
*
*/
public final class GraphQLScalarType: Sendable {
public let name: String
public let description: String?
public let specifiedByURL: String?
public let astNode: ScalarTypeDefinition?
public let extensionASTNodes: [ScalarExtensionDefinition]
public let kind: TypeKind = .scalar
let serialize: @Sendable (Any) throws -> Map
let parseValue: @Sendable (Map) throws -> Map
let parseLiteral: @Sendable (Value) throws -> Map
public init(
name: String,
description: String? = nil,
specifiedByURL: String? = nil,
serialize: @escaping @Sendable (Any) throws -> Map = { try map(from: $0) },
parseValue: (@Sendable (Map) throws -> Map)? = nil,
parseLiteral: (@Sendable (Value) throws -> Map)? = nil,
astNode: ScalarTypeDefinition? = nil,
extensionASTNodes: [ScalarExtensionDefinition] = []
) throws {
try assertValid(name: name)
self.name = name
self.description = description
self.specifiedByURL = specifiedByURL
self.astNode = astNode
self.extensionASTNodes = extensionASTNodes
self.serialize = serialize
self.parseValue = parseValue ?? defaultParseValue
self.parseLiteral = parseLiteral ?? defaultParseLiteral
}
/// Serializes an internal value to include in a response.
public func serialize(value: Any) throws -> Map {
return try serialize(value)
}
/// Parses an externally provided value to use as an input.
public func parseValue(value: Map) throws -> Map {
return try parseValue(value)
}
/// Parses an externally provided literal value to use as an input.
public func parseLiteral(valueAST: Value) throws -> Map {
return try parseLiteral(valueAST)
}
}
let defaultParseValue: (@Sendable (Map) throws -> Map) = { value in
value
}
let defaultParseLiteral: (@Sendable (Value) throws -> Map) = { value in
try valueFromASTUntyped(valueAST: value)
}
extension GraphQLScalarType: CustomDebugStringConvertible {
public var debugDescription: String {
return name
}
}
extension GraphQLScalarType: Hashable {
public func hash(into hasher: inout Hasher) {
hasher.combine(ObjectIdentifier(self))
}
public static func == (lhs: GraphQLScalarType, rhs: GraphQLScalarType) -> Bool {
return lhs.hashValue == rhs.hashValue
}
}
/**
* Object Type Definition
*
* Almost all of the GraphQL types you define will be object types. Object types
* have a name, but most importantly describe their fields.
*
* Example:
*
* let AddressType = GraphQLObjectType(
* name: "Address",
* fields: [
* "street": GraphQLField(type: GraphQLString),
* "number": GraphQLField(type: GraphQLInt),
* "formatted": GraphQLField(
* type: GraphQLString,
* resolve: { address, _, _, _ in
* guard let address = address as? Address {
* return Map.null
* }
*
* return "\(address.number) \(address.street)"
* }
* )
* ]
* )
*
* When two types need to refer to each other, or a type needs to refer to
* itself in a field, you can use a closure to supply the fields lazily.
*
* Example:
*
* let PersonType = GraphQLObjectType(
* name: "Person",
* fields: {
* [
* "name": GraphQLField(type: GraphQLString),
* "bestFriend": GraphQLField(type: PersonType),
* ]
* }
* )
*
*/
public final class GraphQLObjectType: @unchecked Sendable {
public let name: String
public let description: String?
/// The fields that the object defines. These may be mutated during setup, but should not be
/// modified once the schema is being used for execution.
public var fields: () throws -> GraphQLFieldMap {
get {
fieldFunc
}
set {
fieldFunc = newValue
// Clear the cache when setting a new function
cacheQueue.sync(flags: .barrier) {
fieldCache = nil
}
}
}
private var fieldFunc: () throws -> GraphQLFieldMap
private var fieldCache: GraphQLFieldDefinitionMap?
/// The interfaces that the object conforms to. These may be mutated during setup, but should
/// not be modified once the schema is being used for execution.
public var interfaces: () throws -> [GraphQLInterfaceType] {
get {
interfaceFunc
}
set {
interfaceFunc = newValue
// Clear the cache when setting a new function
cacheQueue.sync(flags: .barrier) {
interfaceCache = nil
}
}
}
private var interfaceFunc: () throws -> [GraphQLInterfaceType]
private var interfaceCache: [GraphQLInterfaceType]?
public let isTypeOf: GraphQLIsTypeOf?
public let astNode: ObjectTypeDefinition?
public let extensionASTNodes: [TypeExtensionDefinition]
public let kind: TypeKind = .object
public init(
name: String,
description: String? = nil,
fields: GraphQLFieldMap = [:],
interfaces: [GraphQLInterfaceType] = [],
isTypeOf: GraphQLIsTypeOf? = nil,
astNode: ObjectTypeDefinition? = nil,
extensionASTNodes: [TypeExtensionDefinition] = []
) throws {
try assertValid(name: name)
self.name = name
self.description = description
fieldFunc = { fields }
interfaceFunc = { interfaces }
self.isTypeOf = isTypeOf
self.astNode = astNode
self.extensionASTNodes = extensionASTNodes
}
public init(
name: String,
description: String? = nil,
fields: @escaping () throws -> GraphQLFieldMap,
interfaces: @escaping () throws -> [GraphQLInterfaceType] = { [] },
isTypeOf: GraphQLIsTypeOf? = nil,
astNode: ObjectTypeDefinition? = nil,
extensionASTNodes: [TypeExtensionDefinition] = []
) throws {
try assertValid(name: name)
self.name = name
self.description = description
fieldFunc = fields
interfaceFunc = interfaces
self.isTypeOf = isTypeOf
self.astNode = astNode
self.extensionASTNodes = extensionASTNodes
}
func getFields() throws -> GraphQLFieldDefinitionMap {
if let cached = cacheQueue.sync(execute: { fieldCache }) {
return cached
}
let fields = try defineFieldMap(
name: name,
fields: fields()
)
return cacheQueue.sync(flags: .barrier) {
if let fieldCache {
return fieldCache
} else {
fieldCache = fields
return fields
}
}
}
func getInterfaces() throws -> [GraphQLInterfaceType] {
if let cached = cacheQueue.sync(execute: { interfaceCache }) {
return cached
}
let interfaces = try interfaces()
return cacheQueue.sync(flags: .barrier) {
if let interfaceCache {
return interfaceCache
} else {
interfaceCache = interfaces
return interfaces
}
}
}
}
extension GraphQLObjectType: CustomDebugStringConvertible {
public var debugDescription: String {
return name
}
}
extension GraphQLObjectType: Hashable {
public func hash(into hasher: inout Hasher) {
hasher.combine(ObjectIdentifier(self))
}
public static func == (lhs: GraphQLObjectType, rhs: GraphQLObjectType) -> Bool {
return lhs.hashValue == rhs.hashValue
}
}
func defineFieldMap(name: String, fields: GraphQLFieldMap) throws -> GraphQLFieldDefinitionMap {
var fieldMap = GraphQLFieldDefinitionMap(minimumCapacity: fields.count)
for (name, config) in fields {
try assertValid(name: name)
let field = try GraphQLFieldDefinition(
name: name,
type: config.type,
description: config.description,
deprecationReason: config.deprecationReason,
args: defineArgumentMap(args: config.args),
resolve: config.resolve,
subscribe: config.subscribe,
astNode: config.astNode
)
fieldMap[name] = field
}
return fieldMap
}
func defineArgumentMap(args: GraphQLArgumentConfigMap) throws -> [GraphQLArgumentDefinition] {
var arguments: [GraphQLArgumentDefinition] = []
for (name, config) in args {
try assertValid(name: name)
let argument = GraphQLArgumentDefinition(
name: name,
type: config.type,
defaultValue: config.defaultValue,
description: config.description,
deprecationReason: config.deprecationReason,
astNode: config.astNode
)
arguments.append(argument)
}
return arguments
}
public protocol TypeResolveResultRepresentable {
var typeResolveResult: TypeResolveResult { get }
}
extension GraphQLObjectType: TypeResolveResultRepresentable {
public var typeResolveResult: TypeResolveResult {
return .type(self)
}
}
extension String: TypeResolveResultRepresentable {
public var typeResolveResult: TypeResolveResult {
return .name(self)
}
}
public enum TypeResolveResult: Sendable {
case type(GraphQLObjectType)
case name(String)
}
public typealias GraphQLTypeResolve = @Sendable (
_ value: any Sendable,
_ info: GraphQLResolveInfo
) throws -> TypeResolveResultRepresentable
public typealias GraphQLIsTypeOf = @Sendable (
_ source: any Sendable,
_ info: GraphQLResolveInfo
) throws -> Bool
public typealias GraphQLFieldResolve = @Sendable (
_ source: any Sendable,
_ args: Map,
_ context: any Sendable,
_ info: GraphQLResolveInfo
) async throws -> (any Sendable)?
public typealias GraphQLFieldResolveInput = @Sendable (
_ source: any Sendable,
_ args: Map,
_ context: any Sendable,
_ info: GraphQLResolveInfo
) throws -> (any Sendable)?
public struct GraphQLResolveInfo: Sendable {
public let fieldName: String
public let fieldASTs: [Field]
public let returnType: GraphQLOutputType
public let parentType: GraphQLCompositeType
public let path: IndexPath
public let schema: GraphQLSchema
public let fragments: [String: FragmentDefinition]
public let rootValue: any Sendable
public let operation: OperationDefinition
public let variableValues: [String: any Sendable]
}
public typealias GraphQLFieldMap = OrderedDictionary<String, GraphQLField>
public final class GraphQLField: @unchecked Sendable {
public let type: GraphQLOutputType
public let args: GraphQLArgumentConfigMap
public let deprecationReason: String?
public let description: String?
public var resolve: GraphQLFieldResolve? {
get {
fieldPropertyQueue.sync { _resolve }
}
set {
fieldPropertyQueue.sync(flags: .barrier) { _resolve = newValue }
}
}
private var _resolve: GraphQLFieldResolve?
public var subscribe: GraphQLFieldResolve? {
get {
fieldPropertyQueue.sync { _subscribe }
}
set {
fieldPropertyQueue.sync(flags: .barrier) { _subscribe = newValue }
}
}
private var _subscribe: GraphQLFieldResolve?
public let astNode: FieldDefinition?
public init(
type: GraphQLOutputType,
description: String? = nil,
deprecationReason: String? = nil,
args: GraphQLArgumentConfigMap = [:],
astNode: FieldDefinition? = nil
) {
self.type = type
self.args = args
self.deprecationReason = deprecationReason
self.description = description
self.astNode = astNode
_resolve = nil
_subscribe = nil
}
public init(
type: GraphQLOutputType,
description: String? = nil,
deprecationReason: String? = nil,
args: GraphQLArgumentConfigMap = [:],
resolve: GraphQLFieldResolve?,
subscribe: GraphQLFieldResolve? = nil,
astNode: FieldDefinition? = nil
) {
self.type = type
self.args = args
self.deprecationReason = deprecationReason
self.description = description
self.astNode = astNode
_resolve = resolve
_subscribe = subscribe
}
public init(
type: GraphQLOutputType,
description: String? = nil,
deprecationReason: String? = nil,
args: GraphQLArgumentConfigMap = [:],
astNode: FieldDefinition? = nil,
resolve: @escaping GraphQLFieldResolveInput
) {
self.type = type
self.args = args
self.deprecationReason = deprecationReason
self.description = description
self.astNode = astNode
_resolve = { source, args, context, info in
try resolve(source, args, context, info)
}
_subscribe = nil
}
}
public typealias GraphQLFieldDefinitionMap = OrderedDictionary<String, GraphQLFieldDefinition>
public final class GraphQLFieldDefinition: Sendable {
public let name: String
public let description: String?
public let type: GraphQLOutputType
public let args: [GraphQLArgumentDefinition]
public let resolve: GraphQLFieldResolve?
public let subscribe: GraphQLFieldResolve?
public let deprecationReason: String?
public let isDeprecated: Bool
public let astNode: FieldDefinition?
init(
name: String,
type: GraphQLOutputType,
description: String? = nil,
deprecationReason: String? = nil,
args: [GraphQLArgumentDefinition] = [],
resolve: GraphQLFieldResolve?,
subscribe: GraphQLFieldResolve? = nil,
astNode: FieldDefinition? = nil
) {
self.name = name
self.description = description
self.type = type
self.args = args
self.resolve = resolve
self.subscribe = subscribe
self.deprecationReason = deprecationReason
isDeprecated = deprecationReason != nil
self.astNode = astNode
}
func toField() -> GraphQLField {
return .init(
type: type,
description: description,
deprecationReason: deprecationReason,
args: argConfigMap(),
resolve: resolve,
subscribe: subscribe,
astNode: astNode
)
}
func argConfigMap() -> GraphQLArgumentConfigMap {
var argConfigs: GraphQLArgumentConfigMap = [:]
for argDef in args {
argConfigs[argDef.name] = argDef.toArg()
}
return argConfigs
}
}
public typealias GraphQLArgumentConfigMap = OrderedDictionary<String, GraphQLArgument>
public struct GraphQLArgument: Sendable {
public let type: GraphQLInputType
public let description: String?
public let defaultValue: Map?
public let deprecationReason: String?
public let astNode: InputValueDefinition?
public init(
type: GraphQLInputType,
description: String? = nil,
defaultValue: Map? = nil,
deprecationReason: String? = nil,
astNode: InputValueDefinition? = nil
) {
self.type = type
self.description = description
self.defaultValue = defaultValue
self.deprecationReason = deprecationReason
self.astNode = astNode
}
}
public struct GraphQLArgumentDefinition: Sendable {
public let name: String
public let type: GraphQLInputType
public let defaultValue: Map?
public let description: String?
public let deprecationReason: String?
public let astNode: InputValueDefinition?
init(
name: String,
type: GraphQLInputType,
defaultValue: Map? = nil,
description: String? = nil,
deprecationReason: String? = nil,
astNode: InputValueDefinition? = nil
) {
self.name = name
self.type = type
self.defaultValue = defaultValue
self.description = description
self.deprecationReason = deprecationReason
self.astNode = astNode
}
func toArg() -> GraphQLArgument {
return .init(
type: type,
description: description,
defaultValue: defaultValue,
deprecationReason: deprecationReason,
astNode: astNode
)
}
}
public func isRequiredArgument(_ arg: GraphQLArgumentDefinition) -> Bool {
return arg.type is GraphQLNonNull && arg.defaultValue == nil
}
/**
* Interface Type Definition
*
* When a field can return one of a heterogeneous set of types, a Interface type
* is used to describe what types are possible, what fields are in common across
* all types, as well as a function to determine which type is actually used
* when the field is resolved.
*
* Example:
*
* let EntityType = GraphQLInterfaceType(
* name: "Entity",
* fields: {
* "name": GraphQLField(type: GraphQLString)
* }
* )
*
*/
public final class GraphQLInterfaceType: @unchecked Sendable {
public let name: String
public let description: String?
public let resolveType: GraphQLTypeResolve?
/// The fields that the interface defines. These may be mutated during setup, but should not be
/// modified once the schema is being used for execution.
public var fields: () throws -> GraphQLFieldMap {
get {
fieldFunc
}
set {
fieldFunc = newValue
// Clear the cache when setting a new function
cacheQueue.sync(flags: .barrier) {
fieldCache = nil
}
}
}
private var fieldFunc: () throws -> GraphQLFieldMap
private var fieldCache: GraphQLFieldDefinitionMap?
/// The interfaces that the interface conforms to. This may be mutated during setup, but should
/// not be modified once the schema is being used for execution.
public var interfaces: () throws -> [GraphQLInterfaceType] {
get {
interfaceFunc
}
set {
interfaceFunc = newValue
// Clear the cache when setting a new function
cacheQueue.sync(flags: .barrier) {
interfaceCache = nil
}
}
}
private var interfaceFunc: () throws -> [GraphQLInterfaceType]
private var interfaceCache: [GraphQLInterfaceType]?
public let astNode: InterfaceTypeDefinition?
public let extensionASTNodes: [InterfaceExtensionDefinition]
public let kind: TypeKind = .interface
public init(
name: String,
description: String? = nil,
interfaces: [GraphQLInterfaceType] = [],
fields: GraphQLFieldMap = [:],
resolveType: GraphQLTypeResolve? = nil,
astNode: InterfaceTypeDefinition? = nil,
extensionASTNodes: [InterfaceExtensionDefinition] = []
) throws {
try assertValid(name: name)
self.name = name
self.description = description
fieldFunc = { fields }
interfaceFunc = { interfaces }
self.resolveType = resolveType
self.astNode = astNode
self.extensionASTNodes = extensionASTNodes
}
public init(
name: String,
description: String? = nil,
fields: @escaping () throws -> GraphQLFieldMap,
interfaces: @escaping () throws -> [GraphQLInterfaceType] = { [] },
resolveType: GraphQLTypeResolve? = nil,
astNode: InterfaceTypeDefinition? = nil,
extensionASTNodes: [InterfaceExtensionDefinition] = []
) throws {
try assertValid(name: name)
self.name = name
self.description = description
fieldFunc = fields
interfaceFunc = interfaces
self.resolveType = resolveType
self.astNode = astNode
self.extensionASTNodes = extensionASTNodes
}
func getFields() throws -> GraphQLFieldDefinitionMap {
if let cached = cacheQueue.sync(execute: { fieldCache }) {
return cached
}
let fields = try defineFieldMap(
name: name,
fields: fields()
)
return cacheQueue.sync(flags: .barrier) {
if let fieldCache {
return fieldCache
} else {
fieldCache = fields
return fields
}
}
}
func getInterfaces() throws -> [GraphQLInterfaceType] {
if let cached = cacheQueue.sync(execute: { interfaceCache }) {
return cached
}
let interfaces = try interfaces()
return cacheQueue.sync(flags: .barrier) {
if let interfaceCache {
return interfaceCache
} else {
interfaceCache = interfaces
return interfaces
}
}
}
}
extension GraphQLInterfaceType: CustomDebugStringConvertible {
public var debugDescription: String {
return name
}
}
extension GraphQLInterfaceType: Hashable {
public func hash(into hasher: inout Hasher) {
hasher.combine(ObjectIdentifier(self))
}
public static func == (lhs: GraphQLInterfaceType, rhs: GraphQLInterfaceType) -> Bool {
return lhs.hashValue == rhs.hashValue
}
}
public typealias GraphQLUnionTypeExtensions = [String: String]?
/**
* Union Type Definition
*
* When a field can return one of a heterogeneous set of types, a Union type
* is used to describe what types are possible as well as providing a function
* to determine which type is actually used when the field is resolved.
*
* Example:
*
* let PetType = try GraphQLUnionType(
* name: "Pet",
* types: [DogType, CatType],
* resolveType: { value, context, info in
* switch value {
* case is Dog:
* return DogType
* case is Cat:
* return CatType
* default:
* return Map.null
* }
* }
* )
*
*/
public final class GraphQLUnionType: @unchecked Sendable {
public let kind: TypeKind = .union
public let name: String
public let description: String?
public let resolveType: GraphQLTypeResolve?
/// The types that belong to the union. This may be mutated during setup, but must not be
/// modified once the schema is being used for execution.
public internal(set) var types: () throws -> [GraphQLObjectType]
public let possibleTypeNames: [String: Bool]
let extensions: [GraphQLUnionTypeExtensions]
let astNode: UnionTypeDefinition?
let extensionASTNodes: [UnionExtensionDefinition]
public init(
name: String,
description: String? = nil,
resolveType: GraphQLTypeResolve? = nil,
types: [GraphQLObjectType],
extensions: [GraphQLUnionTypeExtensions] = [],
astNode: UnionTypeDefinition? = nil,
extensionASTNodes: [UnionExtensionDefinition] = []
) throws {
try assertValid(name: name)
self.name = name
self.description = description
self.resolveType = resolveType
self.types = { types }
self.extensions = extensions
self.astNode = astNode
self.extensionASTNodes = extensionASTNodes
possibleTypeNames = [:]
}
public init(
name: String,
description: String? = nil,
resolveType: GraphQLTypeResolve? = nil,
types: @escaping () throws -> [GraphQLObjectType],
extensions: [GraphQLUnionTypeExtensions] = [],
astNode: UnionTypeDefinition? = nil,
extensionASTNodes: [UnionExtensionDefinition] = []
) throws {
try assertValid(name: name)
self.name = name
self.description = description
self.resolveType = resolveType
self.types = types
self.extensions = extensions
self.astNode = astNode
self.extensionASTNodes = extensionASTNodes
possibleTypeNames = [:]
}
func getTypes() throws -> [GraphQLObjectType] {
try types()
}
}
extension GraphQLUnionType: CustomDebugStringConvertible {
public var debugDescription: String {
return name
}
}
extension GraphQLUnionType: Hashable {
public func hash(into hasher: inout Hasher) {
hasher.combine(ObjectIdentifier(self))
}
public static func == (lhs: GraphQLUnionType, rhs: GraphQLUnionType) -> Bool {
return lhs.hashValue == rhs.hashValue
}
}
/**
* Enum Type Definition
*
* Some leaf values of requests and input values are Enums. GraphQL serializes
* Enum values as strings, however internally Enums can be represented by any
* kind of type, often integers.
*
* Example:
*
* let RGBType = GraphQLEnumType(
* name: "RGB",
* values: {
* "RED": GraphQLEnumValue(value: 0),
* "GREEN": GraphQLEnumValue(value: 1),
* "BLUE": GraphQLEnumValue(value: 2)
* }
* )
*
* Note: If a value is not provided in a definition, the name of the enum value
* will be used as its internal value.
*/
public final class GraphQLEnumType: Sendable {
public let name: String
public let description: String?
public let values: [GraphQLEnumValueDefinition]
public let astNode: EnumTypeDefinition?
public let extensionASTNodes: [EnumExtensionDefinition]
public let valueLookup: [Map: GraphQLEnumValueDefinition]
public let nameLookup: [String: GraphQLEnumValueDefinition]