-
Notifications
You must be signed in to change notification settings - Fork 2k
/
Copy pathbuildClientSchema.js
400 lines (365 loc) · 12.1 KB
/
buildClientSchema.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
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
/**
* Copyright (c) 2015-present, Facebook, Inc.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow strict
*/
import inspect from '../jsutils/inspect';
import invariant from '../jsutils/invariant';
import keyMap from '../jsutils/keyMap';
import keyValMap from '../jsutils/keyValMap';
import { valueFromAST } from './valueFromAST';
import { parseValue } from '../language/parser';
import { GraphQLSchema } from '../type/schema';
import {
isInputType,
isOutputType,
GraphQLScalarType,
GraphQLObjectType,
GraphQLInterfaceType,
GraphQLUnionType,
GraphQLEnumType,
GraphQLInputObjectType,
GraphQLList,
GraphQLNonNull,
assertNullableType,
assertObjectType,
assertInterfaceType,
} from '../type/definition';
import type {
GraphQLType,
GraphQLInputType,
GraphQLOutputType,
GraphQLNamedType,
} from '../type/definition';
import { GraphQLDirective } from '../type/directives';
import { introspectionTypes, TypeKind } from '../type/introspection';
import { specifiedScalarTypes } from '../type/scalars';
import type {
IntrospectionQuery,
IntrospectionType,
IntrospectionScalarType,
IntrospectionObjectType,
IntrospectionInterfaceType,
IntrospectionUnionType,
IntrospectionEnumType,
IntrospectionInputObjectType,
IntrospectionTypeRef,
IntrospectionInputTypeRef,
IntrospectionOutputTypeRef,
IntrospectionNamedTypeRef,
} from './introspectionQuery';
import type { GraphQLSchemaValidationOptions } from '../type/schema';
type Options = {|
...GraphQLSchemaValidationOptions,
|};
/**
* Build a GraphQLSchema for use by client tools.
*
* Given the result of a client running the introspection query, creates and
* returns a GraphQLSchema instance which can be then used with all graphql-js
* tools, but cannot be used to execute a query, as introspection does not
* represent the "resolver", "parse" or "serialize" functions or any other
* server-internal mechanisms.
*
* This function expects a complete introspection result. Don't forget to check
* the "errors" field of a server response before calling this function.
*/
export function buildClientSchema(
introspection: IntrospectionQuery,
options?: Options,
): GraphQLSchema {
// Get the schema from the introspection result.
const schemaIntrospection = introspection.__schema;
// Converts the list of types into a keyMap based on the type names.
const typeIntrospectionMap = keyMap(
schemaIntrospection.types,
type => type.name,
);
// A cache to use to store the actual GraphQLType definition objects by name.
// Initialize to the GraphQL built in scalars. All functions below are inline
// so that this type def cache is within the scope of the closure.
const typeDefCache = keyMap(
specifiedScalarTypes.concat(introspectionTypes),
type => type.name,
);
// Given a type reference in introspection, return the GraphQLType instance.
// preferring cached instances before building new instances.
function getType(typeRef: IntrospectionTypeRef): GraphQLType {
if (typeRef.kind === TypeKind.LIST) {
const itemRef = typeRef.ofType;
if (!itemRef) {
throw new Error('Decorated type deeper than introspection query.');
}
return GraphQLList(getType(itemRef));
}
if (typeRef.kind === TypeKind.NON_NULL) {
const nullableRef = typeRef.ofType;
if (!nullableRef) {
throw new Error('Decorated type deeper than introspection query.');
}
const nullableType = getType(nullableRef);
return GraphQLNonNull(assertNullableType(nullableType));
}
if (!typeRef.name) {
throw new Error('Unknown type reference: ' + inspect(typeRef));
}
return getNamedType(typeRef.name);
}
function getNamedType(typeName: string): GraphQLNamedType {
if (typeDefCache[typeName]) {
return typeDefCache[typeName];
}
const typeIntrospection = typeIntrospectionMap[typeName];
if (!typeIntrospection) {
throw new Error(
`Invalid or incomplete schema, unknown type: ${typeName}. Ensure ` +
'that a full introspection query is used in order to build a ' +
'client schema.',
);
}
const typeDef = buildType(typeIntrospection);
typeDefCache[typeName] = typeDef;
return typeDef;
}
function getInputType(typeRef: IntrospectionInputTypeRef): GraphQLInputType {
const type = getType(typeRef);
invariant(
isInputType(type),
'Introspection must provide input type for arguments.',
);
return type;
}
function getOutputType(
typeRef: IntrospectionOutputTypeRef,
): GraphQLOutputType {
const type = getType(typeRef);
invariant(
isOutputType(type),
'Introspection must provide output type for fields.',
);
return type;
}
function getObjectType(
typeRef: IntrospectionNamedTypeRef<IntrospectionObjectType>,
): GraphQLObjectType {
const type = getType(typeRef);
return assertObjectType(type);
}
function getInterfaceType(
typeRef: IntrospectionTypeRef,
): GraphQLInterfaceType {
const type = getType(typeRef);
return assertInterfaceType(type);
}
// Given a type's introspection result, construct the correct
// GraphQLType instance.
function buildType(type: IntrospectionType): GraphQLNamedType {
if (type && type.name && type.kind) {
switch (type.kind) {
case TypeKind.SCALAR:
return buildScalarDef(type);
case TypeKind.OBJECT:
return buildObjectDef(type);
case TypeKind.INTERFACE:
return buildInterfaceDef(type);
case TypeKind.UNION:
return buildUnionDef(type);
case TypeKind.ENUM:
return buildEnumDef(type);
case TypeKind.INPUT_OBJECT:
return buildInputObjectDef(type);
}
}
throw new Error(
'Invalid or incomplete introspection result. Ensure that a full ' +
'introspection query is used in order to build a client schema:' +
inspect(type),
);
}
function buildScalarDef(
scalarIntrospection: IntrospectionScalarType,
): GraphQLScalarType {
const ofType = scalarIntrospection.ofType
? (getType(scalarIntrospection.ofType): any)
: undefined;
return new GraphQLScalarType({
name: scalarIntrospection.name,
description: scalarIntrospection.description,
ofType,
serialize: value => value,
});
}
function buildObjectDef(
objectIntrospection: IntrospectionObjectType,
): GraphQLObjectType {
if (!objectIntrospection.interfaces) {
throw new Error(
'Introspection result missing interfaces: ' +
inspect(objectIntrospection),
);
}
return new GraphQLObjectType({
name: objectIntrospection.name,
description: objectIntrospection.description,
interfaces: () => objectIntrospection.interfaces.map(getInterfaceType),
fields: () => buildFieldDefMap(objectIntrospection),
});
}
function buildInterfaceDef(
interfaceIntrospection: IntrospectionInterfaceType,
): GraphQLInterfaceType {
return new GraphQLInterfaceType({
name: interfaceIntrospection.name,
description: interfaceIntrospection.description,
fields: () => buildFieldDefMap(interfaceIntrospection),
});
}
function buildUnionDef(
unionIntrospection: IntrospectionUnionType,
): GraphQLUnionType {
if (!unionIntrospection.possibleTypes) {
throw new Error(
'Introspection result missing possibleTypes: ' +
inspect(unionIntrospection),
);
}
return new GraphQLUnionType({
name: unionIntrospection.name,
description: unionIntrospection.description,
types: () => unionIntrospection.possibleTypes.map(getObjectType),
});
}
function buildEnumDef(
enumIntrospection: IntrospectionEnumType,
): GraphQLEnumType {
if (!enumIntrospection.enumValues) {
throw new Error(
'Introspection result missing enumValues: ' +
inspect(enumIntrospection),
);
}
return new GraphQLEnumType({
name: enumIntrospection.name,
description: enumIntrospection.description,
values: keyValMap(
enumIntrospection.enumValues,
valueIntrospection => valueIntrospection.name,
valueIntrospection => ({
description: valueIntrospection.description,
deprecationReason: valueIntrospection.deprecationReason,
}),
),
});
}
function buildInputObjectDef(
inputObjectIntrospection: IntrospectionInputObjectType,
): GraphQLInputObjectType {
if (!inputObjectIntrospection.inputFields) {
throw new Error(
'Introspection result missing inputFields: ' +
inspect(inputObjectIntrospection),
);
}
return new GraphQLInputObjectType({
name: inputObjectIntrospection.name,
description: inputObjectIntrospection.description,
fields: () => buildInputValueDefMap(inputObjectIntrospection.inputFields),
});
}
function buildFieldDefMap(typeIntrospection) {
if (!typeIntrospection.fields) {
throw new Error(
'Introspection result missing fields: ' + inspect(typeIntrospection),
);
}
return keyValMap(
typeIntrospection.fields,
fieldIntrospection => fieldIntrospection.name,
fieldIntrospection => {
if (!fieldIntrospection.args) {
throw new Error(
'Introspection result missing field args: ' +
inspect(fieldIntrospection),
);
}
return {
description: fieldIntrospection.description,
deprecationReason: fieldIntrospection.deprecationReason,
type: getOutputType(fieldIntrospection.type),
args: buildInputValueDefMap(fieldIntrospection.args),
};
},
);
}
function buildInputValueDefMap(inputValueIntrospections) {
return keyValMap(
inputValueIntrospections,
inputValue => inputValue.name,
buildInputValue,
);
}
function buildInputValue(inputValueIntrospection) {
const type = getInputType(inputValueIntrospection.type);
const defaultValue = inputValueIntrospection.defaultValue
? valueFromAST(parseValue(inputValueIntrospection.defaultValue), type)
: undefined;
return {
description: inputValueIntrospection.description,
type,
defaultValue,
};
}
function buildDirective(directiveIntrospection) {
if (!directiveIntrospection.args) {
throw new Error(
'Introspection result missing directive args: ' +
inspect(directiveIntrospection),
);
}
if (!directiveIntrospection.locations) {
throw new Error(
'Introspection result missing directive locations: ' +
inspect(directiveIntrospection),
);
}
return new GraphQLDirective({
name: directiveIntrospection.name,
description: directiveIntrospection.description,
locations: directiveIntrospection.locations.slice(),
args: buildInputValueDefMap(directiveIntrospection.args),
});
}
// Iterate through all types, getting the type definition for each, ensuring
// that any type not directly referenced by a field will get created.
const types = schemaIntrospection.types.map(typeIntrospection =>
getNamedType(typeIntrospection.name),
);
// Get the root Query, Mutation, and Subscription types.
const queryType = schemaIntrospection.queryType
? getObjectType(schemaIntrospection.queryType)
: null;
const mutationType = schemaIntrospection.mutationType
? getObjectType(schemaIntrospection.mutationType)
: null;
const subscriptionType = schemaIntrospection.subscriptionType
? getObjectType(schemaIntrospection.subscriptionType)
: null;
// Get the directives supported by Introspection, assuming empty-set if
// directives were not queried for.
const directives = schemaIntrospection.directives
? schemaIntrospection.directives.map(buildDirective)
: [];
// Then produce and return a Schema with these types.
return new GraphQLSchema({
query: queryType,
mutation: mutationType,
subscription: subscriptionType,
types,
directives,
assumeValid: options && options.assumeValid,
allowedLegacyNames: options && options.allowedLegacyNames,
});
}