/
schema.js
335 lines (293 loc) · 10.1 KB
/
schema.js
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
/* @flow */
/**
* Copyright (c) 2015, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*/
import {
GraphQLObjectType,
GraphQLInputObjectType,
GraphQLInterfaceType,
GraphQLUnionType,
GraphQLList,
GraphQLNonNull
} from './definition';
import type { GraphQLType, GraphQLAbstractType } from './definition';
import { GraphQLDirective, specifiedDirectives } from './directives';
import { __Schema } from './introspection';
import find from '../jsutils/find';
import invariant from '../jsutils/invariant';
import { isEqualType, isTypeSubTypeOf } from '../utilities/typeComparators';
/**
* 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:
*
* const MyAppSchema = new 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:
*
* const MyAppSchema = new GraphQLSchema({
* ...
* directives: specifiedDirectives.concat([ myCustomDirective ]),
* })
*
*/
export class GraphQLSchema {
_queryType: GraphQLObjectType;
_mutationType: ?GraphQLObjectType;
_subscriptionType: ?GraphQLObjectType;
_directives: Array<GraphQLDirective>;
_typeMap: TypeMap;
_implementations: { [interfaceName: string]: Array<GraphQLObjectType> };
_possibleTypeMap:
?{ [abstractName: string]: { [possibleName: string]: boolean } };
constructor(config: GraphQLSchemaConfig) {
invariant(
typeof config === 'object',
'Must provide configuration object.'
);
invariant(
config.query instanceof GraphQLObjectType,
`Schema query must be Object Type but got: ${config.query}.`
);
this._queryType = config.query;
invariant(
!config.mutation || config.mutation instanceof GraphQLObjectType,
`Schema mutation must be Object Type if provided but got: ${
config.mutation}.`
);
this._mutationType = config.mutation;
invariant(
!config.subscription || config.subscription instanceof GraphQLObjectType,
`Schema subscription must be Object Type if provided but got: ${
config.subscription}.`
);
this._subscriptionType = config.subscription;
invariant(
!config.types || Array.isArray(config.types),
`Schema types must be Array if provided but got: ${config.types}.`
);
invariant(
!config.directives ||
Array.isArray(config.directives) && config.directives.every(
directive => directive instanceof GraphQLDirective
),
`Schema directives must be Array<GraphQLDirective> if provided but got: ${
config.directives}.`
);
// Provide specified directives (e.g. @include and @skip) by default.
this._directives = config.directives || specifiedDirectives;
// Build type map now to detect any errors within this schema.
let initialTypes: Array<?GraphQLType> = [
this.getQueryType(),
this.getMutationType(),
this.getSubscriptionType(),
__Schema
];
const types = config.types;
if (types) {
initialTypes = initialTypes.concat(types);
}
this._typeMap = initialTypes.reduce(
typeMapReducer,
(Object.create(null): TypeMap)
);
// Keep track of all implementations by interface name.
this._implementations = Object.create(null);
Object.keys(this._typeMap).forEach(typeName => {
const type = this._typeMap[typeName];
if (type instanceof GraphQLObjectType) {
type.getInterfaces().forEach(iface => {
const impls = this._implementations[iface.name];
if (impls) {
impls.push(type);
} else {
this._implementations[iface.name] = [ type ];
}
});
}
});
// Enforce correct interface implementations.
Object.keys(this._typeMap).forEach(typeName => {
const type = this._typeMap[typeName];
if (type instanceof GraphQLObjectType) {
type.getInterfaces().forEach(
iface => assertObjectImplementsInterface(this, type, iface)
);
}
});
}
getQueryType(): GraphQLObjectType {
return this._queryType;
}
getMutationType(): ?GraphQLObjectType {
return this._mutationType;
}
getSubscriptionType(): ?GraphQLObjectType {
return this._subscriptionType;
}
getTypeMap(): TypeMap {
return this._typeMap;
}
getType(name: string): ?GraphQLType {
return this.getTypeMap()[name];
}
getPossibleTypes(
abstractType: GraphQLAbstractType
): Array<GraphQLObjectType> {
if (abstractType instanceof GraphQLUnionType) {
return abstractType.getTypes();
}
invariant(abstractType instanceof GraphQLInterfaceType);
return this._implementations[abstractType.name];
}
isPossibleType(
abstractType: GraphQLAbstractType,
possibleType: GraphQLObjectType
): boolean {
let possibleTypeMap = this._possibleTypeMap;
if (!possibleTypeMap) {
this._possibleTypeMap = possibleTypeMap = Object.create(null);
}
if (!possibleTypeMap[abstractType.name]) {
const possibleTypes = this.getPossibleTypes(abstractType);
invariant(
Array.isArray(possibleTypes),
`Could not find possible implementing types for ${abstractType} in ` +
'schema. Check that schema.types is defined and is an array of' +
'all possible types in the schema.'
);
possibleTypeMap[abstractType.name] =
possibleTypes.reduce(
(map, type) => ((map[type.name] = true), map),
Object.create(null)
);
}
return Boolean(possibleTypeMap[abstractType.name][possibleType.name]);
}
getDirectives(): Array<GraphQLDirective> {
return this._directives;
}
getDirective(name: string): ?GraphQLDirective {
return find(this.getDirectives(), directive => directive.name === name);
}
}
type TypeMap = { [typeName: string]: GraphQLType }
type GraphQLSchemaConfig = {
query: GraphQLObjectType;
mutation?: ?GraphQLObjectType;
subscription?: ?GraphQLObjectType;
types?: ?Array<GraphQLType>;
directives?: ?Array<GraphQLDirective>;
}
function typeMapReducer(map: TypeMap, type: ?GraphQLType): TypeMap {
if (!type) {
return map;
}
if (type instanceof GraphQLList || type instanceof GraphQLNonNull) {
return typeMapReducer(map, type.ofType);
}
if (map[type.name]) {
invariant(
map[type.name] === type,
'Schema must contain unique named types but contains multiple ' +
`types named "${type}".`
);
return map;
}
map[type.name] = type;
let reducedMap = map;
if (type instanceof GraphQLUnionType) {
reducedMap = type.getTypes().reduce(typeMapReducer, reducedMap);
}
if (type instanceof GraphQLObjectType) {
reducedMap = type.getInterfaces().reduce(typeMapReducer, reducedMap);
}
if (type instanceof GraphQLObjectType ||
type instanceof GraphQLInterfaceType ||
type instanceof GraphQLInputObjectType) {
const fieldMap = type.getFields();
Object.keys(fieldMap).forEach(fieldName => {
const field = fieldMap[fieldName];
if (field.args) {
const fieldArgTypes = field.args.map(arg => arg.type);
reducedMap = fieldArgTypes.reduce(typeMapReducer, reducedMap);
}
reducedMap = typeMapReducer(reducedMap, field.type);
});
}
return reducedMap;
}
function assertObjectImplementsInterface(
schema: GraphQLSchema,
object: GraphQLObjectType,
iface: GraphQLInterfaceType
): void {
const objectFieldMap = object.getFields();
const ifaceFieldMap = iface.getFields();
// Assert each interface field is implemented.
Object.keys(ifaceFieldMap).forEach(fieldName => {
const objectField = objectFieldMap[fieldName];
const ifaceField = ifaceFieldMap[fieldName];
// Assert interface field exists on object.
invariant(
objectField,
`"${iface}" expects field "${fieldName}" but "${object}" does not ` +
'provide it.'
);
// Assert interface field type is satisfied by object field type, by being
// a valid subtype. (covariant)
invariant(
isTypeSubTypeOf(schema, objectField.type, ifaceField.type),
`${iface}.${fieldName} expects type "${ifaceField.type}" but ` +
`${object}.${fieldName} provides type "${objectField.type}".`
);
// Assert each interface field arg is implemented.
ifaceField.args.forEach(ifaceArg => {
const argName = ifaceArg.name;
const objectArg = find(objectField.args, arg => arg.name === argName);
// Assert interface field arg exists on object field.
invariant(
objectArg,
`${iface}.${fieldName} expects argument "${argName}" but ` +
`${object}.${fieldName} does not provide it.`
);
// Assert interface field arg type matches object field arg type.
// (invariant)
invariant(
isEqualType(ifaceArg.type, objectArg.type),
`${iface}.${fieldName}(${argName}:) expects type "${ifaceArg.type}" ` +
`but ${object}.${fieldName}(${argName}:) provides ` +
`type "${objectArg.type}".`
);
});
// Assert additional arguments must not be required.
objectField.args.forEach(objectArg => {
const argName = objectArg.name;
const ifaceArg = find(ifaceField.args, arg => arg.name === argName);
if (!ifaceArg) {
invariant(
!(objectArg.type instanceof GraphQLNonNull),
`${object}.${fieldName}(${argName}:) is of required type ` +
`"${objectArg.type}" but is not also provided by the ` +
`interface ${iface}.${fieldName}.`
);
}
});
});
}