UNPKG

12.3 kBJavaScriptView Raw
1'use strict';
2
3Object.defineProperty(exports, '__esModule', { value: true });
4
5var graphql = require('graphql');
6
7function builtInScalar(builtInType) {
8 return {
9 kind: 'Scalar',
10 builtInType,
11 };
12}
13function createTypesFactory() {
14 return {
15 String: builtInScalar(graphql.GraphQLString),
16 Int: builtInScalar(graphql.GraphQLInt),
17 Float: builtInScalar(graphql.GraphQLFloat),
18 Boolean: builtInScalar(graphql.GraphQLBoolean),
19 ID: builtInScalar(graphql.GraphQLID),
20 scalarType({ name, description, serialize, parseValue, parseLiteral, }) {
21 return {
22 kind: 'Scalar',
23 graphqlTypeConfig: {
24 name,
25 description,
26 serialize,
27 parseLiteral,
28 parseValue,
29 },
30 };
31 },
32 enumType({ name, description, values, }) {
33 return {
34 kind: 'Enum',
35 name,
36 description,
37 values,
38 };
39 },
40 arg(type, description) {
41 return {
42 kind: 'Argument',
43 type,
44 description,
45 };
46 },
47 defaultArg(type, defaultArg, description) {
48 return {
49 kind: 'DefaultArgument',
50 type: type,
51 description,
52 default: defaultArg,
53 };
54 },
55 field(name, { type, args = {}, resolve, description, deprecationReason, extensions }) {
56 return {
57 kind: 'Field',
58 name,
59 type,
60 description,
61 deprecationReason,
62 args,
63 resolve,
64 extensions
65 };
66 },
67 defaultField(name, type, opts) {
68 return {
69 kind: 'Field',
70 name: String(name),
71 type,
72 description: opts && opts.description,
73 deprecationReason: opts && opts.deprecationReason,
74 args: {},
75 resolve: (src) => src[name],
76 };
77 },
78 abstractField(name, type, opts) {
79 return {
80 kind: 'AbstractField',
81 name,
82 description: opts && opts.description,
83 deprecationReason: opts && opts.deprecationReason,
84 type,
85 };
86 },
87 objectType({ name, description, interfaces = [], fields, isTypeOf, extensions, }) {
88 const obj = {
89 kind: 'ObjectType',
90 name,
91 description,
92 interfaces,
93 fieldsFn: undefined,
94 isTypeOf,
95 extensions,
96 };
97 obj.fieldsFn = () => fields(obj);
98 return obj;
99 },
100 inputObjectType({ name, description, fields, }) {
101 let inputObj = {
102 kind: 'InputObject',
103 name,
104 description,
105 fieldsFn: null,
106 };
107 inputObj.fieldsFn = () => fields(inputObj);
108 return inputObj;
109 },
110 unionType({ name, types, resolveType, }) {
111 return {
112 kind: 'Union',
113 name,
114 types,
115 resolveType,
116 };
117 },
118 interfaceType({ name, description, interfaces = [], fields, }) {
119 const obj = {
120 kind: 'Interface',
121 name,
122 description,
123 interfaces,
124 fieldsFn: undefined,
125 };
126 obj.fieldsFn = () => fields(obj);
127 return obj;
128 },
129 List(ofType) {
130 return {
131 kind: 'List',
132 ofType: ofType,
133 };
134 },
135 ListInput(ofType) {
136 return {
137 kind: 'ListInput',
138 ofType: ofType,
139 };
140 },
141 NonNull(ofType) {
142 return {
143 kind: 'NonNull',
144 ofType: ofType,
145 };
146 },
147 NonNullInput(ofType) {
148 return {
149 kind: 'NonNullInput',
150 ofType: ofType,
151 };
152 },
153 queryType({ name = 'Query', fields, }) {
154 return {
155 kind: 'ObjectType',
156 name,
157 interfaces: [],
158 fieldsFn: () => fields,
159 };
160 },
161 mutationType({ name = 'Mutation', fields, }) {
162 return {
163 kind: 'ObjectType',
164 name,
165 interfaces: [],
166 fieldsFn: fields,
167 };
168 },
169 subscriptionField(name, { type, args = {}, subscribe, resolve, description, deprecationReason, }) {
170 return {
171 kind: 'SubscriptionField',
172 name,
173 type,
174 args,
175 subscribe,
176 resolve,
177 description,
178 deprecationReason,
179 };
180 },
181 subscriptionType({ name = 'Subscription', fields, }) {
182 return {
183 kind: 'SubscriptionObject',
184 name,
185 fields,
186 };
187 },
188 };
189}
190
191function buildGraphQLSchema(schema) {
192 const typeMap = new Map();
193 return new graphql.GraphQLSchema({
194 query: toGraphQLOutputType(schema.query, typeMap),
195 mutation: schema.mutation &&
196 toGraphQLOutputType(schema.mutation, typeMap),
197 subscription: schema.subscription && toGraphQLSubscriptionObject(schema.subscription, typeMap),
198 types: schema.types && schema.types.map((type) => toGraphQLOutputType(type, typeMap)),
199 directives: schema.directives
200 });
201}
202function toGraphQLArgs(args, typeMap) {
203 const graphqlArgs = {};
204 Object.keys(args).forEach((k) => {
205 const arg = args[k];
206 graphqlArgs[k] = {
207 type: toGraphQLInputType(arg.type, typeMap),
208 description: arg.description,
209 defaultValue: arg.kind === 'DefaultArgument' ? arg.default : undefined,
210 };
211 });
212 return graphqlArgs;
213}
214function toGraphQLSubscriptionObject(subscriptionObj, typeMap) {
215 return new graphql.GraphQLObjectType({
216 name: subscriptionObj.name,
217 fields: () => {
218 const gqlFieldConfig = {};
219 subscriptionObj.fields.forEach((field) => {
220 gqlFieldConfig[field.name] = {
221 type: toGraphQLOutputType(field.type, typeMap),
222 description: field.description,
223 subscribe: field.subscribe,
224 resolve: field.resolve,
225 args: toGraphQLArgs(field.args, typeMap),
226 deprecationReason: field.deprecationReason,
227 };
228 });
229 return gqlFieldConfig;
230 },
231 });
232}
233function toGraphQLInputType(t, typeMap) {
234 const found = typeMap.get(t);
235 if (found) {
236 return typeMap.get(t);
237 }
238 switch (t.kind) {
239 case 'Scalar':
240 case 'Enum':
241 return toGraphQLOutputType(t, typeMap);
242 case 'NonNullInput':
243 return new graphql.GraphQLNonNull(toGraphQLInputType(t.ofType, typeMap));
244 case 'ListInput':
245 return new graphql.GraphQLList(toGraphQLInputType(t.ofType, typeMap));
246 case 'InputObject':
247 const fields = t.fieldsFn();
248 function graphqlFields() {
249 const gqlFieldConfig = {};
250 Object.keys(fields).forEach((k) => {
251 const field = fields[k];
252 gqlFieldConfig[k] = {
253 type: toGraphQLInputType(field.type, typeMap),
254 description: field.description,
255 deprecationReason: field.deprecationReason,
256 };
257 });
258 return gqlFieldConfig;
259 }
260 const obj = new graphql.GraphQLInputObjectType({
261 name: t.name,
262 fields: graphqlFields,
263 });
264 typeMap.set(t, obj);
265 return obj;
266 }
267}
268function toGraphQLOutputType(t, typeMap) {
269 const found = typeMap.get(t);
270 if (found) {
271 return found;
272 }
273 switch (t.kind) {
274 case 'Scalar':
275 let scalar;
276 if ('builtInType' in t) {
277 scalar = t.builtInType;
278 }
279 else {
280 scalar = new graphql.GraphQLScalarType({
281 ...t.graphqlTypeConfig,
282 });
283 }
284 typeMap.set(t, scalar);
285 return scalar;
286 case 'Enum':
287 const enumT = new graphql.GraphQLEnumType({
288 name: t.name,
289 description: t.description,
290 values: t.values.reduce((acc, val) => {
291 acc[val.name] = {
292 value: val.value,
293 deprecationReason: val.deprecationReason,
294 description: val.description,
295 };
296 return acc;
297 }, {}),
298 });
299 typeMap.set(t, enumT);
300 return enumT;
301 case 'NonNull':
302 return new graphql.GraphQLNonNull(toGraphQLOutputType(t.ofType, typeMap));
303 case 'List':
304 return new graphql.GraphQLList(toGraphQLOutputType(t.ofType, typeMap));
305 case 'ObjectType':
306 const obj = new graphql.GraphQLObjectType({
307 name: t.name,
308 interfaces: t.interfaces.map((intf) => toGraphQLOutputType(intf, typeMap)),
309 isTypeOf: t.isTypeOf,
310 fields: () => {
311 const fields = t.fieldsFn();
312 const gqlFieldConfig = {};
313 fields.forEach((field) => {
314 gqlFieldConfig[field.name] = {
315 type: toGraphQLOutputType(field.type, typeMap),
316 description: field.description,
317 resolve: field.resolve,
318 args: toGraphQLArgs(field.args, typeMap),
319 deprecationReason: field.deprecationReason,
320 extensions: field.extensions
321 };
322 });
323 return gqlFieldConfig;
324 },
325 extensions: t.extensions,
326 });
327 typeMap.set(t, obj);
328 return obj;
329 case 'Union':
330 const union = new graphql.GraphQLUnionType({
331 name: t.name,
332 types: t.types.map((t) => toGraphQLOutputType(t, typeMap)),
333 resolveType: async (src, ctx, info) => {
334 const resolved = await t.resolveType(src, ctx, info);
335 if (typeof resolved === 'string' || resolved == null)
336 return resolved;
337 return typeMap.get(resolved);
338 },
339 });
340 typeMap.set(t, union);
341 return union;
342 case 'Interface':
343 const intf = new graphql.GraphQLInterfaceType({
344 name: t.name,
345 fields: () => {
346 const fields = t.fieldsFn();
347 const result = {};
348 fields.forEach((field) => {
349 result[field.name] = {
350 type: toGraphQLOutputType(field.type, typeMap),
351 description: field.description,
352 deprecationReason: field.deprecationReason,
353 };
354 });
355 return result;
356 },
357 interfaces: t.interfaces.map((intf) => toGraphQLOutputType(intf, typeMap)),
358 });
359 typeMap.set(t, intf);
360 return intf;
361 }
362}
363
364exports.buildGraphQLSchema = buildGraphQLSchema;
365exports.createTypesFactory = createTypesFactory;
366exports.toGraphQLInputType = toGraphQLInputType;
367exports.toGraphQLOutputType = toGraphQLOutputType;