UNPKG

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