-
Notifications
You must be signed in to change notification settings - Fork 70
/
Schema.swift
420 lines (353 loc) · 13.5 KB
/
Schema.swift
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
/**
* Schema Definition
*
* A Schema is created by supplying the root types of each type of operation,
* query and mutation (optional). A schema definition is then supplied to the
* validator and executor.
*
* Example:
*
* let MyAppSchema = GraphQLSchema(
* query: MyAppQueryRootType,
* mutation: MyAppMutationRootType,
* )
*
* Note: If an array of `directives` are provided to GraphQLSchema, that will be
* the exact list of directives represented and allowed. If `directives` is not
* provided then a default set of the specified directives (e.g. @include and
* @skip) will be used. If you wish to provide *additional* directives to these
* specified directives, you must explicitly declare them. Example:
*
* let MyAppSchema = GraphQLSchema(
* ...
* directives: specifiedDirectives + [myCustomDirective],
* ...
* )
*
*/
public final class GraphQLSchema {
public let queryType: GraphQLObjectType
public let mutationType: GraphQLObjectType?
public let subscriptionType: GraphQLObjectType?
public let directives: [GraphQLDirective]
public let typeMap: TypeMap
public let implementations: [String: InterfaceImplementations]
private var subTypeMap: [String: [String: Bool]] = [:]
public init(
query: GraphQLObjectType,
mutation: GraphQLObjectType? = nil,
subscription: GraphQLObjectType? = nil,
types: [GraphQLNamedType] = [],
directives: [GraphQLDirective] = []
) throws {
queryType = query
mutationType = mutation
subscriptionType = subscription
// Provide specified directives (e.g. @include and @skip) by default.
self.directives = directives.isEmpty ? specifiedDirectives : directives
// Build type map now to detect any errors within this schema.
var initialTypes: [GraphQLNamedType] = [
queryType,
]
if let mutation = mutationType {
initialTypes.append(mutation)
}
if let subscription = subscriptionType {
initialTypes.append(subscription)
}
initialTypes.append(__Schema)
if !types.isEmpty {
initialTypes.append(contentsOf: types)
}
var typeMap = TypeMap()
for type in initialTypes {
typeMap = try typeMapReducer(typeMap: typeMap, type: type)
}
self.typeMap = typeMap
try replaceTypeReferences(typeMap: typeMap)
// Keep track of all implementations by interface name.
implementations = collectImplementations(types: Array(typeMap.values))
// Enforce correct interface implementations.
for (_, type) in typeMap {
if let object = type as? GraphQLObjectType {
for interface in object.interfaces {
try assert(object: object, implementsInterface: interface, schema: self)
}
}
}
}
public func getType(name: String) -> GraphQLNamedType? {
return typeMap[name]
}
public func getPossibleTypes(abstractType: GraphQLAbstractType) -> [GraphQLObjectType] {
if let unionType = abstractType as? GraphQLUnionType {
return unionType.types
}
if let interfaceType = abstractType as? GraphQLInterfaceType {
return getImplementations(interfaceType: interfaceType).objects
}
fatalError(
"Should be impossible. Only UnionType and InterfaceType should conform to AbstractType"
)
}
public func getImplementations(
interfaceType: GraphQLInterfaceType
) -> InterfaceImplementations {
guard let matchingImplementations = implementations[interfaceType.name] else {
// If we ask for an interface that hasn't been defined, just return no types.
return InterfaceImplementations()
}
return matchingImplementations
}
// @deprecated: use isSubType instead - will be removed in the future.
public func isPossibleType(
abstractType: GraphQLAbstractType,
possibleType: GraphQLObjectType
) throws -> Bool {
isSubType(abstractType: abstractType, maybeSubType: possibleType)
}
public func isSubType(
abstractType: GraphQLAbstractType,
maybeSubType: GraphQLNamedType
) -> Bool {
var map = subTypeMap[abstractType.name]
if map == nil {
map = [:]
if let unionType = abstractType as? GraphQLUnionType {
for type in unionType.types {
map?[type.name] = true
}
}
if let interfaceType = abstractType as? GraphQLInterfaceType {
let implementations = getImplementations(interfaceType: interfaceType)
for type in implementations.objects {
map?[type.name] = true
}
for type in implementations.interfaces {
map?[type.name] = true
}
}
subTypeMap[abstractType.name] = map
}
let isSubType = map?[maybeSubType.name] != nil
return isSubType
}
public func getDirective(name: String) -> GraphQLDirective? {
for directive in directives where directive.name == name {
return directive
}
return nil
}
}
extension GraphQLSchema: Encodable {
private enum CodingKeys: String, CodingKey {
case queryType
case mutationType
case subscriptionType
case directives
}
}
public typealias TypeMap = [String: GraphQLNamedType]
public struct InterfaceImplementations {
public let objects: [GraphQLObjectType]
public let interfaces: [GraphQLInterfaceType]
public init(
objects: [GraphQLObjectType] = [],
interfaces: [GraphQLInterfaceType] = []
) {
self.objects = objects
self.interfaces = interfaces
}
}
func collectImplementations(
types: [GraphQLNamedType]
) -> [String: InterfaceImplementations] {
var implementations: [String: InterfaceImplementations] = [:]
for type in types {
if let type = type as? GraphQLInterfaceType {
if implementations[type.name] == nil {
implementations[type.name] = InterfaceImplementations()
}
// Store implementations by interface.
for iface in type.interfaces {
implementations[iface.name] = InterfaceImplementations(
interfaces: (implementations[iface.name]?.interfaces ?? []) + [type]
)
}
}
if let type = type as? GraphQLObjectType {
// Store implementations by objects.
for iface in type.interfaces {
implementations[iface.name] = InterfaceImplementations(
objects: (implementations[iface.name]?.objects ?? []) + [type]
)
}
}
}
return implementations
}
func typeMapReducer(typeMap: TypeMap, type: GraphQLType) throws -> TypeMap {
var typeMap = typeMap
if let type = type as? GraphQLWrapperType {
return try typeMapReducer(typeMap: typeMap, type: type.wrappedType)
}
guard let type = type as? GraphQLNamedType else {
return typeMap // Should never happen
}
if let existingType = typeMap[type.name] {
if existingType is GraphQLTypeReference {
if type is GraphQLTypeReference {
// Just short circuit because they're both type references
return typeMap
}
// Otherwise, fall through and override the type reference
} else {
if type is GraphQLTypeReference {
// Just ignore the reference and keep the concrete one
return typeMap
} else if !(existingType == type) {
throw GraphQLError(
message:
"Schema must contain unique named types but contains multiple " +
"types named \"\(type.name)\"."
)
} else {
// Otherwise, it's already been defined so short circuit
return typeMap
}
}
}
typeMap[type.name] = type
if let type = type as? GraphQLUnionType {
typeMap = try type.types.reduce(typeMap, typeMapReducer)
}
if let type = type as? GraphQLObjectType {
typeMap = try type.interfaces.reduce(typeMap, typeMapReducer)
for (_, field) in type.fields {
if !field.args.isEmpty {
let fieldArgTypes = field.args.map { $0.type }
typeMap = try fieldArgTypes.reduce(typeMap, typeMapReducer)
}
typeMap = try typeMapReducer(typeMap: typeMap, type: field.type)
}
}
if let type = type as? GraphQLInterfaceType {
typeMap = try type.interfaces.reduce(typeMap, typeMapReducer)
for (_, field) in type.fields {
if !field.args.isEmpty {
let fieldArgTypes = field.args.map { $0.type }
typeMap = try fieldArgTypes.reduce(typeMap, typeMapReducer)
}
typeMap = try typeMapReducer(typeMap: typeMap, type: field.type)
}
}
if let type = type as? GraphQLInputObjectType {
for (_, field) in type.fields {
typeMap = try typeMapReducer(typeMap: typeMap, type: field.type)
}
}
return typeMap
}
func assert(
object: GraphQLObjectType,
implementsInterface interface: GraphQLInterfaceType,
schema: GraphQLSchema
) throws {
let objectFieldMap = object.fields
let interfaceFieldMap = interface.fields
for (fieldName, interfaceField) in interfaceFieldMap {
guard let objectField = objectFieldMap[fieldName] else {
throw GraphQLError(
message:
"\(interface.name) expects field \(fieldName) " +
"but \(object.name) does not provide it."
)
}
// Assert interface field type is satisfied by object field type, by being
// a valid subtype. (covariant)
guard try isTypeSubTypeOf(schema, objectField.type, interfaceField.type) else {
throw GraphQLError(
message:
"\(interface.name).\(fieldName) expects type \"\(interfaceField.type)\" " +
"but " +
"\(object.name).\(fieldName) provides type \"\(objectField.type)\"."
)
}
// Assert each interface field arg is implemented.
for interfaceArg in interfaceField.args {
let argName = interfaceArg.name
guard let objectArg = objectField.args.find({ $0.name == argName }) else {
throw GraphQLError(
message:
"\(interface.name).\(fieldName) expects argument \"\(argName)\" but " +
"\(object.name).\(fieldName) does not provide it."
)
}
// Assert interface field arg type matches object field arg type.
// (invariant)
guard isEqualType(interfaceArg.type, objectArg.type) else {
throw GraphQLError(
message:
"\(interface.name).\(fieldName)(\(argName):) expects type " +
"\"\(interfaceArg.type)\" but " +
"\(object.name).\(fieldName)(\(argName):) provides type " +
"\"\(objectArg.type)\"."
)
}
}
// Assert additional arguments must not be required.
for objectArg in objectField.args {
let argName = objectArg.name
if
interfaceField.args.find({ $0.name == argName }) == nil,
isRequiredArgument(objectArg)
{
throw GraphQLError(
message:
"\(object.name).\(fieldName) includes required argument (\(argName):) that is missing " +
"from the Interface field \(interface.name).\(fieldName)."
)
}
}
}
}
func replaceTypeReferences(typeMap: TypeMap) throws {
for type in typeMap {
if let typeReferenceContainer = type.value as? GraphQLTypeReferenceContainer {
try typeReferenceContainer.replaceTypeReferences(typeMap: typeMap)
}
}
// Check that no type names map to TypeReferences. That is, they have all been resolved to
// actual types.
for (typeName, graphQLNamedType) in typeMap {
if graphQLNamedType is GraphQLTypeReference {
throw GraphQLError(
message: "Type \"\(typeName)\" was referenced but not defined."
)
}
}
}
func resolveTypeReference(type: GraphQLType, typeMap: TypeMap) throws -> GraphQLType {
if let type = type as? GraphQLTypeReference {
guard let resolvedType = typeMap[type.name] else {
throw GraphQLError(
message: "Type \"\(type.name)\" not found in schema."
)
}
return resolvedType
}
if let type = type as? GraphQLList {
return try type.replaceTypeReferences(typeMap: typeMap)
}
if let type = type as? GraphQLNonNull {
return try type.replaceTypeReferences(typeMap: typeMap)
}
return type
}
func resolveTypeReferences(types: [GraphQLType], typeMap: TypeMap) throws -> [GraphQLType] {
var resolvedTypes: [GraphQLType] = []
for type in types {
try resolvedTypes.append(resolveTypeReference(type: type, typeMap: typeMap))
}
return resolvedTypes
}