1 | import objectValues from "../polyfills/objectValues.mjs";
|
2 | import inspect from "../jsutils/inspect.mjs";
|
3 | import devAssert from "../jsutils/devAssert.mjs";
|
4 | import keyValMap from "../jsutils/keyValMap.mjs";
|
5 | import isObjectLike from "../jsutils/isObjectLike.mjs";
|
6 | import { parseValue } from "../language/parser.mjs";
|
7 | import { GraphQLSchema } from "../type/schema.mjs";
|
8 | import { GraphQLDirective } from "../type/directives.mjs";
|
9 | import { specifiedScalarTypes } from "../type/scalars.mjs";
|
10 | import { introspectionTypes, TypeKind } from "../type/introspection.mjs";
|
11 | import { isInputType, isOutputType, GraphQLList, GraphQLNonNull, GraphQLScalarType, GraphQLObjectType, GraphQLInterfaceType, GraphQLUnionType, GraphQLEnumType, GraphQLInputObjectType, assertNullableType, assertObjectType, assertInterfaceType } from "../type/definition.mjs";
|
12 | import { valueFromAST } from "./valueFromAST.mjs";
|
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: ".concat(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 | description: schemaIntrospection.description,
|
55 | query: queryType,
|
56 | mutation: mutationType,
|
57 | subscription: subscriptionType,
|
58 | types: objectValues(typeMap),
|
59 | directives: directives,
|
60 | assumeValid: options === null || options === void 0 ? void 0 : options.assumeValid
|
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 new 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 new GraphQLNonNull(assertNullableType(nullableType));
|
84 | }
|
85 |
|
86 | return getNamedType(typeRef);
|
87 | }
|
88 |
|
89 | function getNamedType(typeRef) {
|
90 | var typeName = typeRef.name;
|
91 |
|
92 | if (!typeName) {
|
93 | throw new Error("Unknown type reference: ".concat(inspect(typeRef), "."));
|
94 | }
|
95 |
|
96 | var type = typeMap[typeName];
|
97 |
|
98 | if (!type) {
|
99 | 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."));
|
100 | }
|
101 |
|
102 | return type;
|
103 | }
|
104 |
|
105 | function getObjectType(typeRef) {
|
106 | return assertObjectType(getNamedType(typeRef));
|
107 | }
|
108 |
|
109 | function getInterfaceType(typeRef) {
|
110 | return assertInterfaceType(getNamedType(typeRef));
|
111 | }
|
112 |
|
113 |
|
114 |
|
115 | function buildType(type) {
|
116 | if (type != null && type.name != null && type.kind != null) {
|
117 | switch (type.kind) {
|
118 | case TypeKind.SCALAR:
|
119 | return buildScalarDef(type);
|
120 |
|
121 | case TypeKind.OBJECT:
|
122 | return buildObjectDef(type);
|
123 |
|
124 | case TypeKind.INTERFACE:
|
125 | return buildInterfaceDef(type);
|
126 |
|
127 | case TypeKind.UNION:
|
128 | return buildUnionDef(type);
|
129 |
|
130 | case TypeKind.ENUM:
|
131 | return buildEnumDef(type);
|
132 |
|
133 | case TypeKind.INPUT_OBJECT:
|
134 | return buildInputObjectDef(type);
|
135 | }
|
136 | }
|
137 |
|
138 | var typeStr = inspect(type);
|
139 | throw new Error("Invalid or incomplete introspection result. Ensure that a full introspection query is used in order to build a client schema: ".concat(typeStr, "."));
|
140 | }
|
141 |
|
142 | function buildScalarDef(scalarIntrospection) {
|
143 | return new GraphQLScalarType({
|
144 | name: scalarIntrospection.name,
|
145 | description: scalarIntrospection.description,
|
146 | specifiedByUrl: scalarIntrospection.specifiedByUrl
|
147 | });
|
148 | }
|
149 |
|
150 | function buildImplementationsList(implementingIntrospection) {
|
151 |
|
152 |
|
153 | if (implementingIntrospection.interfaces === null && implementingIntrospection.kind === TypeKind.INTERFACE) {
|
154 | return [];
|
155 | }
|
156 |
|
157 | if (!implementingIntrospection.interfaces) {
|
158 | var implementingIntrospectionStr = inspect(implementingIntrospection);
|
159 | throw new Error("Introspection result missing interfaces: ".concat(implementingIntrospectionStr, "."));
|
160 | }
|
161 |
|
162 | return implementingIntrospection.interfaces.map(getInterfaceType);
|
163 | }
|
164 |
|
165 | function buildObjectDef(objectIntrospection) {
|
166 | return new GraphQLObjectType({
|
167 | name: objectIntrospection.name,
|
168 | description: objectIntrospection.description,
|
169 | interfaces: function interfaces() {
|
170 | return buildImplementationsList(objectIntrospection);
|
171 | },
|
172 | fields: function fields() {
|
173 | return buildFieldDefMap(objectIntrospection);
|
174 | }
|
175 | });
|
176 | }
|
177 |
|
178 | function buildInterfaceDef(interfaceIntrospection) {
|
179 | return new GraphQLInterfaceType({
|
180 | name: interfaceIntrospection.name,
|
181 | description: interfaceIntrospection.description,
|
182 | interfaces: function interfaces() {
|
183 | return buildImplementationsList(interfaceIntrospection);
|
184 | },
|
185 | fields: function fields() {
|
186 | return buildFieldDefMap(interfaceIntrospection);
|
187 | }
|
188 | });
|
189 | }
|
190 |
|
191 | function buildUnionDef(unionIntrospection) {
|
192 | if (!unionIntrospection.possibleTypes) {
|
193 | var unionIntrospectionStr = inspect(unionIntrospection);
|
194 | throw new Error("Introspection result missing possibleTypes: ".concat(unionIntrospectionStr, "."));
|
195 | }
|
196 |
|
197 | return new GraphQLUnionType({
|
198 | name: unionIntrospection.name,
|
199 | description: unionIntrospection.description,
|
200 | types: function types() {
|
201 | return unionIntrospection.possibleTypes.map(getObjectType);
|
202 | }
|
203 | });
|
204 | }
|
205 |
|
206 | function buildEnumDef(enumIntrospection) {
|
207 | if (!enumIntrospection.enumValues) {
|
208 | var enumIntrospectionStr = inspect(enumIntrospection);
|
209 | throw new Error("Introspection result missing enumValues: ".concat(enumIntrospectionStr, "."));
|
210 | }
|
211 |
|
212 | return new GraphQLEnumType({
|
213 | name: enumIntrospection.name,
|
214 | description: enumIntrospection.description,
|
215 | values: keyValMap(enumIntrospection.enumValues, function (valueIntrospection) {
|
216 | return valueIntrospection.name;
|
217 | }, function (valueIntrospection) {
|
218 | return {
|
219 | description: valueIntrospection.description,
|
220 | deprecationReason: valueIntrospection.deprecationReason
|
221 | };
|
222 | })
|
223 | });
|
224 | }
|
225 |
|
226 | function buildInputObjectDef(inputObjectIntrospection) {
|
227 | if (!inputObjectIntrospection.inputFields) {
|
228 | var inputObjectIntrospectionStr = inspect(inputObjectIntrospection);
|
229 | throw new Error("Introspection result missing inputFields: ".concat(inputObjectIntrospectionStr, "."));
|
230 | }
|
231 |
|
232 | return new GraphQLInputObjectType({
|
233 | name: inputObjectIntrospection.name,
|
234 | description: inputObjectIntrospection.description,
|
235 | fields: function fields() {
|
236 | return buildInputValueDefMap(inputObjectIntrospection.inputFields);
|
237 | }
|
238 | });
|
239 | }
|
240 |
|
241 | function buildFieldDefMap(typeIntrospection) {
|
242 | if (!typeIntrospection.fields) {
|
243 | throw new Error("Introspection result missing fields: ".concat(inspect(typeIntrospection), "."));
|
244 | }
|
245 |
|
246 | return keyValMap(typeIntrospection.fields, function (fieldIntrospection) {
|
247 | return fieldIntrospection.name;
|
248 | }, buildField);
|
249 | }
|
250 |
|
251 | function buildField(fieldIntrospection) {
|
252 | var type = getType(fieldIntrospection.type);
|
253 |
|
254 | if (!isOutputType(type)) {
|
255 | var typeStr = inspect(type);
|
256 | throw new Error("Introspection must provide output type for fields, but received: ".concat(typeStr, "."));
|
257 | }
|
258 |
|
259 | if (!fieldIntrospection.args) {
|
260 | var fieldIntrospectionStr = inspect(fieldIntrospection);
|
261 | throw new Error("Introspection result missing field args: ".concat(fieldIntrospectionStr, "."));
|
262 | }
|
263 |
|
264 | return {
|
265 | description: fieldIntrospection.description,
|
266 | deprecationReason: fieldIntrospection.deprecationReason,
|
267 | type: type,
|
268 | args: buildInputValueDefMap(fieldIntrospection.args)
|
269 | };
|
270 | }
|
271 |
|
272 | function buildInputValueDefMap(inputValueIntrospections) {
|
273 | return keyValMap(inputValueIntrospections, function (inputValue) {
|
274 | return inputValue.name;
|
275 | }, buildInputValue);
|
276 | }
|
277 |
|
278 | function buildInputValue(inputValueIntrospection) {
|
279 | var type = getType(inputValueIntrospection.type);
|
280 |
|
281 | if (!isInputType(type)) {
|
282 | var typeStr = inspect(type);
|
283 | throw new Error("Introspection must provide input type for arguments, but received: ".concat(typeStr, "."));
|
284 | }
|
285 |
|
286 | var defaultValue = inputValueIntrospection.defaultValue != null ? valueFromAST(parseValue(inputValueIntrospection.defaultValue), type) : undefined;
|
287 | return {
|
288 | description: inputValueIntrospection.description,
|
289 | type: type,
|
290 | defaultValue: defaultValue,
|
291 | deprecationReason: inputValueIntrospection.deprecationReason
|
292 | };
|
293 | }
|
294 |
|
295 | function buildDirective(directiveIntrospection) {
|
296 | if (!directiveIntrospection.args) {
|
297 | var directiveIntrospectionStr = inspect(directiveIntrospection);
|
298 | throw new Error("Introspection result missing directive args: ".concat(directiveIntrospectionStr, "."));
|
299 | }
|
300 |
|
301 | if (!directiveIntrospection.locations) {
|
302 | var _directiveIntrospectionStr = inspect(directiveIntrospection);
|
303 |
|
304 | throw new Error("Introspection result missing directive locations: ".concat(_directiveIntrospectionStr, "."));
|
305 | }
|
306 |
|
307 | return new GraphQLDirective({
|
308 | name: directiveIntrospection.name,
|
309 | description: directiveIntrospection.description,
|
310 | isRepeatable: directiveIntrospection.isRepeatable,
|
311 | locations: directiveIntrospection.locations.slice(),
|
312 | args: buildInputValueDefMap(directiveIntrospection.args)
|
313 | });
|
314 | }
|
315 | }
|