1 | import objectValues from '../polyfills/objectValues';
|
2 | import inspect from '../jsutils/inspect';
|
3 | import devAssert from '../jsutils/devAssert';
|
4 | import keyValMap from '../jsutils/keyValMap';
|
5 | import isObjectLike from '../jsutils/isObjectLike';
|
6 | import { parseValue } from '../language/parser';
|
7 | import { GraphQLDirective } from '../type/directives';
|
8 | import { specifiedScalarTypes } from '../type/scalars';
|
9 | import { introspectionTypes, TypeKind } from '../type/introspection';
|
10 | import { GraphQLSchema } from '../type/schema';
|
11 | import { isInputType, isOutputType, GraphQLScalarType, GraphQLObjectType, GraphQLInterfaceType, GraphQLUnionType, GraphQLEnumType, GraphQLInputObjectType, GraphQLList, GraphQLNonNull, assertNullableType, assertObjectType, assertInterfaceType } from '../type/definition';
|
12 | import { valueFromAST } from './valueFromAST';
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 | export function buildClientSchema(introspection, options) {
|
27 | isObjectLike(introspection) && isObjectLike(introspection.__schema) || devAssert(0, 'Invalid or incomplete introspection result. Ensure that you are passing "data" property of introspection response and no "errors" was returned alongside: ' + inspect(introspection));
|
28 |
|
29 | var schemaIntrospection = introspection.__schema;
|
30 |
|
31 | var typeMap = keyValMap(schemaIntrospection.types, function (typeIntrospection) {
|
32 | return typeIntrospection.name;
|
33 | }, function (typeIntrospection) {
|
34 | return buildType(typeIntrospection);
|
35 | });
|
36 |
|
37 | for (var _i2 = 0, _ref2 = [].concat(specifiedScalarTypes, introspectionTypes); _i2 < _ref2.length; _i2++) {
|
38 | var stdType = _ref2[_i2];
|
39 |
|
40 | if (typeMap[stdType.name]) {
|
41 | typeMap[stdType.name] = stdType;
|
42 | }
|
43 | }
|
44 |
|
45 |
|
46 | var queryType = schemaIntrospection.queryType ? getObjectType(schemaIntrospection.queryType) : null;
|
47 | var mutationType = schemaIntrospection.mutationType ? getObjectType(schemaIntrospection.mutationType) : null;
|
48 | var subscriptionType = schemaIntrospection.subscriptionType ? getObjectType(schemaIntrospection.subscriptionType) : null;
|
49 |
|
50 |
|
51 | var directives = schemaIntrospection.directives ? schemaIntrospection.directives.map(buildDirective) : [];
|
52 |
|
53 | return new GraphQLSchema({
|
54 | query: queryType,
|
55 | mutation: mutationType,
|
56 | subscription: subscriptionType,
|
57 | types: objectValues(typeMap),
|
58 | directives: directives,
|
59 | assumeValid: options && options.assumeValid,
|
60 | allowedLegacyNames: options && options.allowedLegacyNames
|
61 | });
|
62 |
|
63 |
|
64 | function getType(typeRef) {
|
65 | if (typeRef.kind === TypeKind.LIST) {
|
66 | var itemRef = typeRef.ofType;
|
67 |
|
68 | if (!itemRef) {
|
69 | throw new Error('Decorated type deeper than introspection query.');
|
70 | }
|
71 |
|
72 | return GraphQLList(getType(itemRef));
|
73 | }
|
74 |
|
75 | if (typeRef.kind === TypeKind.NON_NULL) {
|
76 | var nullableRef = typeRef.ofType;
|
77 |
|
78 | if (!nullableRef) {
|
79 | throw new Error('Decorated type deeper than introspection query.');
|
80 | }
|
81 |
|
82 | var nullableType = getType(nullableRef);
|
83 | return GraphQLNonNull(assertNullableType(nullableType));
|
84 | }
|
85 |
|
86 | if (!typeRef.name) {
|
87 | throw new Error('Unknown type reference: ' + inspect(typeRef));
|
88 | }
|
89 |
|
90 | return getNamedType(typeRef.name);
|
91 | }
|
92 |
|
93 | function getNamedType(typeName) {
|
94 | var type = typeMap[typeName];
|
95 |
|
96 | if (!type) {
|
97 | 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."));
|
98 | }
|
99 |
|
100 | return type;
|
101 | }
|
102 |
|
103 | function getInputType(typeRef) {
|
104 | var type = getType(typeRef);
|
105 |
|
106 | if (isInputType(type)) {
|
107 | return type;
|
108 | }
|
109 |
|
110 | throw new Error('Introspection must provide input type for arguments, but received: ' + inspect(type) + '.');
|
111 | }
|
112 |
|
113 | function getOutputType(typeRef) {
|
114 | var type = getType(typeRef);
|
115 |
|
116 | if (isOutputType(type)) {
|
117 | return type;
|
118 | }
|
119 |
|
120 | throw new Error('Introspection must provide output type for fields, but received: ' + inspect(type) + '.');
|
121 | }
|
122 |
|
123 | function getObjectType(typeRef) {
|
124 | var type = getType(typeRef);
|
125 | return assertObjectType(type);
|
126 | }
|
127 |
|
128 | function getInterfaceType(typeRef) {
|
129 | var type = getType(typeRef);
|
130 | return assertInterfaceType(type);
|
131 | }
|
132 |
|
133 |
|
134 |
|
135 | function buildType(type) {
|
136 | if (type && type.name && type.kind) {
|
137 | switch (type.kind) {
|
138 | case TypeKind.SCALAR:
|
139 | return buildScalarDef(type);
|
140 |
|
141 | case TypeKind.OBJECT:
|
142 | return buildObjectDef(type);
|
143 |
|
144 | case TypeKind.INTERFACE:
|
145 | return buildInterfaceDef(type);
|
146 |
|
147 | case TypeKind.UNION:
|
148 | return buildUnionDef(type);
|
149 |
|
150 | case TypeKind.ENUM:
|
151 | return buildEnumDef(type);
|
152 |
|
153 | case TypeKind.INPUT_OBJECT:
|
154 | return buildInputObjectDef(type);
|
155 | }
|
156 | }
|
157 |
|
158 | 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));
|
159 | }
|
160 |
|
161 | function buildScalarDef(scalarIntrospection) {
|
162 | return new GraphQLScalarType({
|
163 | name: scalarIntrospection.name,
|
164 | description: scalarIntrospection.description
|
165 | });
|
166 | }
|
167 |
|
168 | function buildObjectDef(objectIntrospection) {
|
169 | if (!objectIntrospection.interfaces) {
|
170 | throw new Error('Introspection result missing interfaces: ' + inspect(objectIntrospection));
|
171 | }
|
172 |
|
173 | return new GraphQLObjectType({
|
174 | name: objectIntrospection.name,
|
175 | description: objectIntrospection.description,
|
176 | interfaces: function interfaces() {
|
177 | return objectIntrospection.interfaces.map(getInterfaceType);
|
178 | },
|
179 | fields: function fields() {
|
180 | return buildFieldDefMap(objectIntrospection);
|
181 | }
|
182 | });
|
183 | }
|
184 |
|
185 | function buildInterfaceDef(interfaceIntrospection) {
|
186 | return new GraphQLInterfaceType({
|
187 | name: interfaceIntrospection.name,
|
188 | description: interfaceIntrospection.description,
|
189 | fields: function fields() {
|
190 | return buildFieldDefMap(interfaceIntrospection);
|
191 | }
|
192 | });
|
193 | }
|
194 |
|
195 | function buildUnionDef(unionIntrospection) {
|
196 | if (!unionIntrospection.possibleTypes) {
|
197 | throw new Error('Introspection result missing possibleTypes: ' + inspect(unionIntrospection));
|
198 | }
|
199 |
|
200 | return new GraphQLUnionType({
|
201 | name: unionIntrospection.name,
|
202 | description: unionIntrospection.description,
|
203 | types: function types() {
|
204 | return unionIntrospection.possibleTypes.map(getObjectType);
|
205 | }
|
206 | });
|
207 | }
|
208 |
|
209 | function buildEnumDef(enumIntrospection) {
|
210 | if (!enumIntrospection.enumValues) {
|
211 | throw new Error('Introspection result missing enumValues: ' + inspect(enumIntrospection));
|
212 | }
|
213 |
|
214 | return new GraphQLEnumType({
|
215 | name: enumIntrospection.name,
|
216 | description: enumIntrospection.description,
|
217 | values: keyValMap(enumIntrospection.enumValues, function (valueIntrospection) {
|
218 | return valueIntrospection.name;
|
219 | }, function (valueIntrospection) {
|
220 | return {
|
221 | description: valueIntrospection.description,
|
222 | deprecationReason: valueIntrospection.deprecationReason
|
223 | };
|
224 | })
|
225 | });
|
226 | }
|
227 |
|
228 | function buildInputObjectDef(inputObjectIntrospection) {
|
229 | if (!inputObjectIntrospection.inputFields) {
|
230 | throw new Error('Introspection result missing inputFields: ' + inspect(inputObjectIntrospection));
|
231 | }
|
232 |
|
233 | return new GraphQLInputObjectType({
|
234 | name: inputObjectIntrospection.name,
|
235 | description: inputObjectIntrospection.description,
|
236 | fields: function fields() {
|
237 | return buildInputValueDefMap(inputObjectIntrospection.inputFields);
|
238 | }
|
239 | });
|
240 | }
|
241 |
|
242 | function buildFieldDefMap(typeIntrospection) {
|
243 | if (!typeIntrospection.fields) {
|
244 | throw new Error('Introspection result missing fields: ' + inspect(typeIntrospection));
|
245 | }
|
246 |
|
247 | return keyValMap(typeIntrospection.fields, function (fieldIntrospection) {
|
248 | return fieldIntrospection.name;
|
249 | }, function (fieldIntrospection) {
|
250 | if (!fieldIntrospection.args) {
|
251 | throw new Error('Introspection result missing field args: ' + inspect(fieldIntrospection));
|
252 | }
|
253 |
|
254 | return {
|
255 | description: fieldIntrospection.description,
|
256 | deprecationReason: fieldIntrospection.deprecationReason,
|
257 | type: getOutputType(fieldIntrospection.type),
|
258 | args: buildInputValueDefMap(fieldIntrospection.args)
|
259 | };
|
260 | });
|
261 | }
|
262 |
|
263 | function buildInputValueDefMap(inputValueIntrospections) {
|
264 | return keyValMap(inputValueIntrospections, function (inputValue) {
|
265 | return inputValue.name;
|
266 | }, buildInputValue);
|
267 | }
|
268 |
|
269 | function buildInputValue(inputValueIntrospection) {
|
270 | var type = getInputType(inputValueIntrospection.type);
|
271 | var defaultValue = inputValueIntrospection.defaultValue ? valueFromAST(parseValue(inputValueIntrospection.defaultValue), type) : undefined;
|
272 | return {
|
273 | description: inputValueIntrospection.description,
|
274 | type: type,
|
275 | defaultValue: defaultValue
|
276 | };
|
277 | }
|
278 |
|
279 | function buildDirective(directiveIntrospection) {
|
280 | if (!directiveIntrospection.args) {
|
281 | throw new Error('Introspection result missing directive args: ' + inspect(directiveIntrospection));
|
282 | }
|
283 |
|
284 | if (!directiveIntrospection.locations) {
|
285 | throw new Error('Introspection result missing directive locations: ' + inspect(directiveIntrospection));
|
286 | }
|
287 |
|
288 | return new GraphQLDirective({
|
289 | name: directiveIntrospection.name,
|
290 | description: directiveIntrospection.description,
|
291 | locations: directiveIntrospection.locations.slice(),
|
292 | args: buildInputValueDefMap(directiveIntrospection.args)
|
293 | });
|
294 | }
|
295 | }
|