UNPKG

12.6 kBJavaScriptView Raw
1'use strict';
2
3Object.defineProperty(exports, '__esModule', {
4 value: true,
5});
6exports.buildClientSchema = buildClientSchema;
7
8var _devAssert = require('../jsutils/devAssert.js');
9
10var _inspect = require('../jsutils/inspect.js');
11
12var _isObjectLike = require('../jsutils/isObjectLike.js');
13
14var _keyValMap = require('../jsutils/keyValMap.js');
15
16var _parser = require('../language/parser.js');
17
18var _definition = require('../type/definition.js');
19
20var _directives = require('../type/directives.js');
21
22var _introspection = require('../type/introspection.js');
23
24var _scalars = require('../type/scalars.js');
25
26var _schema = require('../type/schema.js');
27
28var _valueFromAST = require('./valueFromAST.js');
29
30/**
31 * Build a GraphQLSchema for use by client tools.
32 *
33 * Given the result of a client running the introspection query, creates and
34 * returns a GraphQLSchema instance which can be then used with all graphql-js
35 * tools, but cannot be used to execute a query, as introspection does not
36 * represent the "resolver", "parse" or "serialize" functions or any other
37 * server-internal mechanisms.
38 *
39 * This function expects a complete introspection result. Don't forget to check
40 * the "errors" field of a server response before calling this function.
41 */
42function buildClientSchema(introspection, options) {
43 ((0, _isObjectLike.isObjectLike)(introspection) &&
44 (0, _isObjectLike.isObjectLike)(introspection.__schema)) ||
45 (0, _devAssert.devAssert)(
46 false,
47 `Invalid or incomplete introspection result. Ensure that you are passing "data" property of introspection response and no "errors" was returned alongside: ${(0,
48 _inspect.inspect)(introspection)}.`,
49 ); // Get the schema from the introspection result.
50
51 const schemaIntrospection = introspection.__schema; // Iterate through all types, getting the type definition for each.
52
53 const typeMap = (0, _keyValMap.keyValMap)(
54 schemaIntrospection.types,
55 (typeIntrospection) => typeIntrospection.name,
56 (typeIntrospection) => buildType(typeIntrospection),
57 ); // Include standard types only if they are used.
58
59 for (const stdType of [
60 ..._scalars.specifiedScalarTypes,
61 ..._introspection.introspectionTypes,
62 ]) {
63 if (typeMap[stdType.name]) {
64 typeMap[stdType.name] = stdType;
65 }
66 } // Get the root Query, Mutation, and Subscription types.
67
68 const queryType = schemaIntrospection.queryType
69 ? getObjectType(schemaIntrospection.queryType)
70 : null;
71 const mutationType = schemaIntrospection.mutationType
72 ? getObjectType(schemaIntrospection.mutationType)
73 : null;
74 const subscriptionType = schemaIntrospection.subscriptionType
75 ? getObjectType(schemaIntrospection.subscriptionType)
76 : null; // Get the directives supported by Introspection, assuming empty-set if
77 // directives were not queried for.
78
79 const directives = schemaIntrospection.directives
80 ? schemaIntrospection.directives.map(buildDirective)
81 : []; // Then produce and return a Schema with these types.
82
83 return new _schema.GraphQLSchema({
84 description: schemaIntrospection.description,
85 query: queryType,
86 mutation: mutationType,
87 subscription: subscriptionType,
88 types: Object.values(typeMap),
89 directives,
90 assumeValid:
91 options === null || options === void 0 ? void 0 : options.assumeValid,
92 }); // Given a type reference in introspection, return the GraphQLType instance.
93 // preferring cached instances before building new instances.
94
95 function getType(typeRef) {
96 if (typeRef.kind === _introspection.TypeKind.LIST) {
97 const itemRef = typeRef.ofType;
98
99 if (!itemRef) {
100 throw new Error('Decorated type deeper than introspection query.');
101 }
102
103 return new _definition.GraphQLList(getType(itemRef));
104 }
105
106 if (typeRef.kind === _introspection.TypeKind.NON_NULL) {
107 const nullableRef = typeRef.ofType;
108
109 if (!nullableRef) {
110 throw new Error('Decorated type deeper than introspection query.');
111 }
112
113 const nullableType = getType(nullableRef);
114 return new _definition.GraphQLNonNull(
115 (0, _definition.assertNullableType)(nullableType),
116 );
117 }
118
119 return getNamedType(typeRef);
120 }
121
122 function getNamedType(typeRef) {
123 const typeName = typeRef.name;
124
125 if (!typeName) {
126 throw new Error(
127 `Unknown type reference: ${(0, _inspect.inspect)(typeRef)}.`,
128 );
129 }
130
131 const type = typeMap[typeName];
132
133 if (!type) {
134 throw new Error(
135 `Invalid or incomplete schema, unknown type: ${typeName}. Ensure that a full introspection query is used in order to build a client schema.`,
136 );
137 }
138
139 return type;
140 }
141
142 function getObjectType(typeRef) {
143 return (0, _definition.assertObjectType)(getNamedType(typeRef));
144 }
145
146 function getInterfaceType(typeRef) {
147 return (0, _definition.assertInterfaceType)(getNamedType(typeRef));
148 } // Given a type's introspection result, construct the correct
149 // GraphQLType instance.
150
151 function buildType(type) {
152 // eslint-disable-next-line @typescript-eslint/prefer-optional-chain
153 if (type != null && type.name != null && type.kind != null) {
154 // FIXME: Properly type IntrospectionType, it's a breaking change so fix in v17
155 // eslint-disable-next-line @typescript-eslint/switch-exhaustiveness-check
156 switch (type.kind) {
157 case _introspection.TypeKind.SCALAR:
158 return buildScalarDef(type);
159
160 case _introspection.TypeKind.OBJECT:
161 return buildObjectDef(type);
162
163 case _introspection.TypeKind.INTERFACE:
164 return buildInterfaceDef(type);
165
166 case _introspection.TypeKind.UNION:
167 return buildUnionDef(type);
168
169 case _introspection.TypeKind.ENUM:
170 return buildEnumDef(type);
171
172 case _introspection.TypeKind.INPUT_OBJECT:
173 return buildInputObjectDef(type);
174 }
175 }
176
177 const typeStr = (0, _inspect.inspect)(type);
178 throw new Error(
179 `Invalid or incomplete introspection result. Ensure that a full introspection query is used in order to build a client schema: ${typeStr}.`,
180 );
181 }
182
183 function buildScalarDef(scalarIntrospection) {
184 return new _definition.GraphQLScalarType({
185 name: scalarIntrospection.name,
186 description: scalarIntrospection.description,
187 specifiedByURL: scalarIntrospection.specifiedByURL,
188 });
189 }
190
191 function buildImplementationsList(implementingIntrospection) {
192 // TODO: Temporary workaround until GraphQL ecosystem will fully support
193 // 'interfaces' on interface types.
194 if (
195 implementingIntrospection.interfaces === null &&
196 implementingIntrospection.kind === _introspection.TypeKind.INTERFACE
197 ) {
198 return [];
199 }
200
201 if (!implementingIntrospection.interfaces) {
202 const implementingIntrospectionStr = (0, _inspect.inspect)(
203 implementingIntrospection,
204 );
205 throw new Error(
206 `Introspection result missing interfaces: ${implementingIntrospectionStr}.`,
207 );
208 }
209
210 return implementingIntrospection.interfaces.map(getInterfaceType);
211 }
212
213 function buildObjectDef(objectIntrospection) {
214 return new _definition.GraphQLObjectType({
215 name: objectIntrospection.name,
216 description: objectIntrospection.description,
217 interfaces: () => buildImplementationsList(objectIntrospection),
218 fields: () => buildFieldDefMap(objectIntrospection),
219 });
220 }
221
222 function buildInterfaceDef(interfaceIntrospection) {
223 return new _definition.GraphQLInterfaceType({
224 name: interfaceIntrospection.name,
225 description: interfaceIntrospection.description,
226 interfaces: () => buildImplementationsList(interfaceIntrospection),
227 fields: () => buildFieldDefMap(interfaceIntrospection),
228 });
229 }
230
231 function buildUnionDef(unionIntrospection) {
232 if (!unionIntrospection.possibleTypes) {
233 const unionIntrospectionStr = (0, _inspect.inspect)(unionIntrospection);
234 throw new Error(
235 `Introspection result missing possibleTypes: ${unionIntrospectionStr}.`,
236 );
237 }
238
239 return new _definition.GraphQLUnionType({
240 name: unionIntrospection.name,
241 description: unionIntrospection.description,
242 types: () => unionIntrospection.possibleTypes.map(getObjectType),
243 });
244 }
245
246 function buildEnumDef(enumIntrospection) {
247 if (!enumIntrospection.enumValues) {
248 const enumIntrospectionStr = (0, _inspect.inspect)(enumIntrospection);
249 throw new Error(
250 `Introspection result missing enumValues: ${enumIntrospectionStr}.`,
251 );
252 }
253
254 return new _definition.GraphQLEnumType({
255 name: enumIntrospection.name,
256 description: enumIntrospection.description,
257 values: (0, _keyValMap.keyValMap)(
258 enumIntrospection.enumValues,
259 (valueIntrospection) => valueIntrospection.name,
260 (valueIntrospection) => ({
261 description: valueIntrospection.description,
262 deprecationReason: valueIntrospection.deprecationReason,
263 }),
264 ),
265 });
266 }
267
268 function buildInputObjectDef(inputObjectIntrospection) {
269 if (!inputObjectIntrospection.inputFields) {
270 const inputObjectIntrospectionStr = (0, _inspect.inspect)(
271 inputObjectIntrospection,
272 );
273 throw new Error(
274 `Introspection result missing inputFields: ${inputObjectIntrospectionStr}.`,
275 );
276 }
277
278 return new _definition.GraphQLInputObjectType({
279 name: inputObjectIntrospection.name,
280 description: inputObjectIntrospection.description,
281 fields: () => buildInputValueDefMap(inputObjectIntrospection.inputFields),
282 });
283 }
284
285 function buildFieldDefMap(typeIntrospection) {
286 if (!typeIntrospection.fields) {
287 throw new Error(
288 `Introspection result missing fields: ${(0, _inspect.inspect)(
289 typeIntrospection,
290 )}.`,
291 );
292 }
293
294 return (0, _keyValMap.keyValMap)(
295 typeIntrospection.fields,
296 (fieldIntrospection) => fieldIntrospection.name,
297 buildField,
298 );
299 }
300
301 function buildField(fieldIntrospection) {
302 const type = getType(fieldIntrospection.type);
303
304 if (!(0, _definition.isOutputType)(type)) {
305 const typeStr = (0, _inspect.inspect)(type);
306 throw new Error(
307 `Introspection must provide output type for fields, but received: ${typeStr}.`,
308 );
309 }
310
311 if (!fieldIntrospection.args) {
312 const fieldIntrospectionStr = (0, _inspect.inspect)(fieldIntrospection);
313 throw new Error(
314 `Introspection result missing field args: ${fieldIntrospectionStr}.`,
315 );
316 }
317
318 return {
319 description: fieldIntrospection.description,
320 deprecationReason: fieldIntrospection.deprecationReason,
321 type,
322 args: buildInputValueDefMap(fieldIntrospection.args),
323 };
324 }
325
326 function buildInputValueDefMap(inputValueIntrospections) {
327 return (0, _keyValMap.keyValMap)(
328 inputValueIntrospections,
329 (inputValue) => inputValue.name,
330 buildInputValue,
331 );
332 }
333
334 function buildInputValue(inputValueIntrospection) {
335 const type = getType(inputValueIntrospection.type);
336
337 if (!(0, _definition.isInputType)(type)) {
338 const typeStr = (0, _inspect.inspect)(type);
339 throw new Error(
340 `Introspection must provide input type for arguments, but received: ${typeStr}.`,
341 );
342 }
343
344 const defaultValue =
345 inputValueIntrospection.defaultValue != null
346 ? (0, _valueFromAST.valueFromAST)(
347 (0, _parser.parseValue)(inputValueIntrospection.defaultValue),
348 type,
349 )
350 : undefined;
351 return {
352 description: inputValueIntrospection.description,
353 type,
354 defaultValue,
355 deprecationReason: inputValueIntrospection.deprecationReason,
356 };
357 }
358
359 function buildDirective(directiveIntrospection) {
360 if (!directiveIntrospection.args) {
361 const directiveIntrospectionStr = (0, _inspect.inspect)(
362 directiveIntrospection,
363 );
364 throw new Error(
365 `Introspection result missing directive args: ${directiveIntrospectionStr}.`,
366 );
367 }
368
369 if (!directiveIntrospection.locations) {
370 const directiveIntrospectionStr = (0, _inspect.inspect)(
371 directiveIntrospection,
372 );
373 throw new Error(
374 `Introspection result missing directive locations: ${directiveIntrospectionStr}.`,
375 );
376 }
377
378 return new _directives.GraphQLDirective({
379 name: directiveIntrospection.name,
380 description: directiveIntrospection.description,
381 isRepeatable: directiveIntrospection.isRepeatable,
382 locations: directiveIntrospection.locations.slice(),
383 args: buildInputValueDefMap(directiveIntrospection.args),
384 });
385 }
386}