UNPKG

11 kBJavaScriptView Raw
1/**
2 * Copyright (c) 2015-present, Facebook, Inc.
3 *
4 * This source code is licensed under the MIT license found in the
5 * LICENSE file in the root directory of this source tree.
6 *
7 *
8 */
9import inspect from '../jsutils/inspect';
10import invariant from '../jsutils/invariant';
11import keyMap from '../jsutils/keyMap';
12import keyValMap from '../jsutils/keyValMap';
13import { valueFromAST } from './valueFromAST';
14import { parseValue } from '../language/parser';
15import { GraphQLSchema } from '../type/schema';
16import { isInputType, isOutputType, GraphQLScalarType, GraphQLObjectType, GraphQLInterfaceType, GraphQLUnionType, GraphQLEnumType, GraphQLInputObjectType, GraphQLList, GraphQLNonNull, assertNullableType, assertObjectType, assertInterfaceType } from '../type/definition';
17import { GraphQLDirective } from '../type/directives';
18import { introspectionTypes, TypeKind } from '../type/introspection';
19import { specifiedScalarTypes } from '../type/scalars';
20
21/**
22 * Build a GraphQLSchema for use by client tools.
23 *
24 * Given the result of a client running the introspection query, creates and
25 * returns a GraphQLSchema instance which can be then used with all graphql-js
26 * tools, but cannot be used to execute a query, as introspection does not
27 * represent the "resolver", "parse" or "serialize" functions or any other
28 * server-internal mechanisms.
29 *
30 * This function expects a complete introspection result. Don't forget to check
31 * the "errors" field of a server response before calling this function.
32 */
33export function buildClientSchema(introspection, options) {
34 // Get the schema from the introspection result.
35 var schemaIntrospection = introspection.__schema; // Converts the list of types into a keyMap based on the type names.
36
37 var typeIntrospectionMap = keyMap(schemaIntrospection.types, function (type) {
38 return type.name;
39 }); // A cache to use to store the actual GraphQLType definition objects by name.
40 // Initialize to the GraphQL built in scalars. All functions below are inline
41 // so that this type def cache is within the scope of the closure.
42
43 var typeDefCache = keyMap(specifiedScalarTypes.concat(introspectionTypes), function (type) {
44 return type.name;
45 }); // Given a type reference in introspection, return the GraphQLType instance.
46 // preferring cached instances before building new instances.
47
48 function getType(typeRef) {
49 if (typeRef.kind === TypeKind.LIST) {
50 var itemRef = typeRef.ofType;
51
52 if (!itemRef) {
53 throw new Error('Decorated type deeper than introspection query.');
54 }
55
56 return GraphQLList(getType(itemRef));
57 }
58
59 if (typeRef.kind === TypeKind.NON_NULL) {
60 var nullableRef = typeRef.ofType;
61
62 if (!nullableRef) {
63 throw new Error('Decorated type deeper than introspection query.');
64 }
65
66 var nullableType = getType(nullableRef);
67 return GraphQLNonNull(assertNullableType(nullableType));
68 }
69
70 if (!typeRef.name) {
71 throw new Error('Unknown type reference: ' + inspect(typeRef));
72 }
73
74 return getNamedType(typeRef.name);
75 }
76
77 function getNamedType(typeName) {
78 if (typeDefCache[typeName]) {
79 return typeDefCache[typeName];
80 }
81
82 var typeIntrospection = typeIntrospectionMap[typeName];
83
84 if (!typeIntrospection) {
85 throw new Error("Invalid or incomplete schema, unknown type: ".concat(typeName, ". Ensure ") + 'that a full introspection query is used in order to build a ' + 'client schema.');
86 }
87
88 var typeDef = buildType(typeIntrospection);
89 typeDefCache[typeName] = typeDef;
90 return typeDef;
91 }
92
93 function getInputType(typeRef) {
94 var type = getType(typeRef);
95 !isInputType(type) ? invariant(0, 'Introspection must provide input type for arguments, but received: ' + inspect(type) + '.') : void 0;
96 return type;
97 }
98
99 function getOutputType(typeRef) {
100 var type = getType(typeRef);
101 !isOutputType(type) ? invariant(0, 'Introspection must provide output type for fields, but received: ' + inspect(type) + '.') : void 0;
102 return type;
103 }
104
105 function getObjectType(typeRef) {
106 var type = getType(typeRef);
107 return assertObjectType(type);
108 }
109
110 function getInterfaceType(typeRef) {
111 var type = getType(typeRef);
112 return assertInterfaceType(type);
113 } // Given a type's introspection result, construct the correct
114 // GraphQLType instance.
115
116
117 function buildType(type) {
118 if (type && type.name && type.kind) {
119 switch (type.kind) {
120 case TypeKind.SCALAR:
121 return buildScalarDef(type);
122
123 case TypeKind.OBJECT:
124 return buildObjectDef(type);
125
126 case TypeKind.INTERFACE:
127 return buildInterfaceDef(type);
128
129 case TypeKind.UNION:
130 return buildUnionDef(type);
131
132 case TypeKind.ENUM:
133 return buildEnumDef(type);
134
135 case TypeKind.INPUT_OBJECT:
136 return buildInputObjectDef(type);
137 }
138 }
139
140 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));
141 }
142
143 function buildScalarDef(scalarIntrospection) {
144 return new GraphQLScalarType({
145 name: scalarIntrospection.name,
146 description: scalarIntrospection.description,
147 serialize: function serialize(value) {
148 return value;
149 }
150 });
151 }
152
153 function buildObjectDef(objectIntrospection) {
154 if (!objectIntrospection.interfaces) {
155 throw new Error('Introspection result missing interfaces: ' + inspect(objectIntrospection));
156 }
157
158 return new GraphQLObjectType({
159 name: objectIntrospection.name,
160 description: objectIntrospection.description,
161 interfaces: function interfaces() {
162 return objectIntrospection.interfaces.map(getInterfaceType);
163 },
164 fields: function fields() {
165 return buildFieldDefMap(objectIntrospection);
166 }
167 });
168 }
169
170 function buildInterfaceDef(interfaceIntrospection) {
171 return new GraphQLInterfaceType({
172 name: interfaceIntrospection.name,
173 description: interfaceIntrospection.description,
174 fields: function fields() {
175 return buildFieldDefMap(interfaceIntrospection);
176 }
177 });
178 }
179
180 function buildUnionDef(unionIntrospection) {
181 if (!unionIntrospection.possibleTypes) {
182 throw new Error('Introspection result missing possibleTypes: ' + inspect(unionIntrospection));
183 }
184
185 return new GraphQLUnionType({
186 name: unionIntrospection.name,
187 description: unionIntrospection.description,
188 types: function types() {
189 return unionIntrospection.possibleTypes.map(getObjectType);
190 }
191 });
192 }
193
194 function buildEnumDef(enumIntrospection) {
195 if (!enumIntrospection.enumValues) {
196 throw new Error('Introspection result missing enumValues: ' + inspect(enumIntrospection));
197 }
198
199 return new GraphQLEnumType({
200 name: enumIntrospection.name,
201 description: enumIntrospection.description,
202 values: keyValMap(enumIntrospection.enumValues, function (valueIntrospection) {
203 return valueIntrospection.name;
204 }, function (valueIntrospection) {
205 return {
206 description: valueIntrospection.description,
207 deprecationReason: valueIntrospection.deprecationReason
208 };
209 })
210 });
211 }
212
213 function buildInputObjectDef(inputObjectIntrospection) {
214 if (!inputObjectIntrospection.inputFields) {
215 throw new Error('Introspection result missing inputFields: ' + inspect(inputObjectIntrospection));
216 }
217
218 return new GraphQLInputObjectType({
219 name: inputObjectIntrospection.name,
220 description: inputObjectIntrospection.description,
221 fields: function fields() {
222 return buildInputValueDefMap(inputObjectIntrospection.inputFields);
223 }
224 });
225 }
226
227 function buildFieldDefMap(typeIntrospection) {
228 if (!typeIntrospection.fields) {
229 throw new Error('Introspection result missing fields: ' + inspect(typeIntrospection));
230 }
231
232 return keyValMap(typeIntrospection.fields, function (fieldIntrospection) {
233 return fieldIntrospection.name;
234 }, function (fieldIntrospection) {
235 if (!fieldIntrospection.args) {
236 throw new Error('Introspection result missing field args: ' + inspect(fieldIntrospection));
237 }
238
239 return {
240 description: fieldIntrospection.description,
241 deprecationReason: fieldIntrospection.deprecationReason,
242 type: getOutputType(fieldIntrospection.type),
243 args: buildInputValueDefMap(fieldIntrospection.args)
244 };
245 });
246 }
247
248 function buildInputValueDefMap(inputValueIntrospections) {
249 return keyValMap(inputValueIntrospections, function (inputValue) {
250 return inputValue.name;
251 }, buildInputValue);
252 }
253
254 function buildInputValue(inputValueIntrospection) {
255 var type = getInputType(inputValueIntrospection.type);
256 var defaultValue = inputValueIntrospection.defaultValue ? valueFromAST(parseValue(inputValueIntrospection.defaultValue), type) : undefined;
257 return {
258 description: inputValueIntrospection.description,
259 type: type,
260 defaultValue: defaultValue
261 };
262 }
263
264 function buildDirective(directiveIntrospection) {
265 if (!directiveIntrospection.args) {
266 throw new Error('Introspection result missing directive args: ' + inspect(directiveIntrospection));
267 }
268
269 if (!directiveIntrospection.locations) {
270 throw new Error('Introspection result missing directive locations: ' + inspect(directiveIntrospection));
271 }
272
273 return new GraphQLDirective({
274 name: directiveIntrospection.name,
275 description: directiveIntrospection.description,
276 locations: directiveIntrospection.locations.slice(),
277 args: buildInputValueDefMap(directiveIntrospection.args)
278 });
279 } // Iterate through all types, getting the type definition for each, ensuring
280 // that any type not directly referenced by a field will get created.
281
282
283 var types = schemaIntrospection.types.map(function (typeIntrospection) {
284 return getNamedType(typeIntrospection.name);
285 }); // Get the root Query, Mutation, and Subscription types.
286
287 var queryType = schemaIntrospection.queryType ? getObjectType(schemaIntrospection.queryType) : null;
288 var mutationType = schemaIntrospection.mutationType ? getObjectType(schemaIntrospection.mutationType) : null;
289 var subscriptionType = schemaIntrospection.subscriptionType ? getObjectType(schemaIntrospection.subscriptionType) : null; // Get the directives supported by Introspection, assuming empty-set if
290 // directives were not queried for.
291
292 var directives = schemaIntrospection.directives ? schemaIntrospection.directives.map(buildDirective) : []; // Then produce and return a Schema with these types.
293
294 return new GraphQLSchema({
295 query: queryType,
296 mutation: mutationType,
297 subscription: subscriptionType,
298 types: types,
299 directives: directives,
300 assumeValid: options && options.assumeValid,
301 allowedLegacyNames: options && options.allowedLegacyNames
302 });
303}
\No newline at end of file