-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
GraphQLPrinter.js
727 lines (640 loc) · 18.6 KB
/
GraphQLPrinter.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
/*jslint node:true*/
"use strict";
var kinds = require('graphql/language/kinds');
var types = require('graphql/type');
var util = require('util');
/**
* This is part of the Babel transform to convert embedded GraphQL RFC to
* JavaScript. It converts from GraphQL AST to a string of JavaScript code.
*/
function GraphQLPrinter(schema, rqlFunctionName) {
this.rqlFunctionName = rqlFunctionName;
this.schema = schema;
}
GraphQLPrinter.prototype.getCode = function(ast, substitutions) {
var options = {
rqlFunctionName: this.rqlFunctionName,
schema: this.schema,
substitutions: substitutions
};
switch (ast.kind) {
case kinds.OPERATION_DEFINITION:
switch (ast.operation) {
case 'query':
return printQuery(ast, options);
case 'mutation':
return printOperation(ast, options);
}
break;
case kinds.FRAGMENT_DEFINITION:
return printQueryFragment(ast, options);
}
throw new Error('unexpected type: ' + ast.kind);
};
function printQueryFragment(fragment, options) {
var argsCode = getFragmentCode(fragment, options);
var substitutionNames = options.substitutions.join(', ');
return [
'function(' + substitutionNames + ') {',
'var GraphQL = ' + options.rqlFunctionName + '.__GraphQL;',
'return new GraphQL.QueryFragment(' + argsCode + ');',
'}'
].join('');
}
function printInlineFragment(fragment, options) {
var argsCode = getFragmentCode(fragment, options);
return 'new GraphQL.QueryFragment(' + argsCode + ')';
}
function getFragmentCode(fragment, options) {
var typeName = getTypeName(fragment);
var type = options.schema.getType(typeName);
if (!type) {
throw new Error('Fragment was defined on nonexistent type ' + typeName);
}
var requisiteFields = {};
if (hasIdField(type)) {
requisiteFields.id = true;
}
var fieldsAndFragments = printFieldsAndFragments(
fragment.selectionSet,
type,
options,
requisiteFields,
typeName
);
var fields = fieldsAndFragments.fields;
var fragments = fieldsAndFragments.fragments;
var metadata = getRelayDirectiveMetadata(fragment);
var metadataCode = stringifyObject(metadata);
return getFunctionArgCode([
JSON.stringify(getName(fragment)),
JSON.stringify(getTypeName(fragment)),
fields,
fragments,
metadataCode
]);
}
/**
* Prints a top level query. This code is pretty similar to `printOperation`,
* unfortunately, GraphQL.Query is currently just different enough, to make it
* not worth unifying this code.
*/
function printQuery(query, options) {
var selections = getSelections(query);
if (selections.length !== 1) {
throw new Error('expected only single top level query');
}
// Validate the name of the root call. Throws if it doesn't exist.
var rootField = selections[0];
var rootCallName = getName(rootField);
var rootCallDecl = options.schema.getQueryType().getFields()[rootCallName];
var type = rootCallDecl.type;
var requisiteFields = {};
var rootCall = getRootCallForType(options.schema, type);
if (rootCall) {
requisiteFields[rootCall.arg] = true;
}
var callArgsCode = printArguments(rootField.arguments[0], options);
var fieldsAndFragments = printFieldsAndFragments(
rootField.selectionSet,
type,
options,
requisiteFields,
types.getNamedType(type).name
);
var fields = fieldsAndFragments.fields;
var fragments = fieldsAndFragments.fragments;
var metadata = {};
if (rootCallDecl.args.length > 1) {
throw new Error(util.format(
'Invalid root field `%s`; Relay only supports root fields with zero or ' +
'one argument',
rootCallName
));
} else if (rootCallDecl.args.length === 1) {
metadata.rootArg = rootCallDecl.args[0].name;
var rootCallTypeName = getTypeForMetadata(rootCallDecl.args[0].type);
if (rootCallTypeName !== 'scalar') {
metadata.rootCallType = rootCallTypeName;
}
}
var metadataCode = stringifyObject(metadata);
var argsCode = getFunctionArgCode([
JSON.stringify(getName(rootField)),
callArgsCode,
fields,
fragments,
metadataCode,
JSON.stringify(getName(query))
]);
var substitutionNames = options.substitutions.join(', ');
return (
'function(' + substitutionNames + ') {' +
'var GraphQL = ' + options.rqlFunctionName + '.__GraphQL;' +
'return new GraphQL.Query(' + argsCode + ');' +
'}'
);
}
function printOperation(operation, options) {
var selections = getSelections(operation);
if (selections.length !== 1) {
throw new Error('expected only single top level field on operation');
}
var rootField = selections[0];
if (operation.operation !== 'mutation') {
throw new Error('Unexpected operation type: ' + operation.operation);
}
var className = 'Mutation';
var field = options.schema.getMutationType().getFields()[getName(rootField)];
if (!field) {
throw new Error(
'Provided mutation ' + getName(rootField) + ' does not exist in schema.'
);
}
var type = types.getNamedType(field.type);
var requisiteFields = {clientMutationId: true};
var callCode =
'new GraphQL.Callv(' +
getFunctionArgCode([
JSON.stringify(getName(rootField)),
printCallVariable('input')
]) +
')';
if (field.args.length !== 1) {
throw new Error(util.format(
'Expected operation `%s` to have a single input field.',
getName(rootField)
));
}
var metadata = {
inputType: field.args[0].type.toString()
};
var fieldsAndFragments = printFieldsAndFragments(
rootField.selectionSet,
type,
options,
requisiteFields,
type.name
);
var fields = fieldsAndFragments.fields;
var fragments = fieldsAndFragments.fragments;
var argsCode = getFunctionArgCode([
JSON.stringify(getName(operation)),
JSON.stringify(type.name),
callCode,
fields,
fragments,
stringifyObject(metadata)
]);
var substitutionNames = options.substitutions.join(', ');
return (
'function(' + substitutionNames + ') {' +
'var GraphQL = ' + options.rqlFunctionName + '.__GraphQL;' +
'return new GraphQL.' + className + '(' + argsCode + ');' +
'}'
);
}
function printFieldsAndFragments(
selectionSet,
type,
options,
requisiteFields,
parentType
) {
var fields = [];
var fragments = [];
if (selectionSet && selectionSet.selections) {
selectionSet.selections.forEach(function(selection) {
if (selection.kind === kinds.FRAGMENT_SPREAD) {
// We assume that all spreads were added by us
fragments.push(printFragmentReference(getName(selection), options));
} else if (selection.kind === kinds.INLINE_FRAGMENT) {
fragments.push(printInlineFragment(selection, options));
} else if (selection.kind === kinds.FIELD) {
fields.push(selection);
} else {
throw new Error(util.format(
'Unsupported selection type `%s`.',
selection.kind
));
}
});
}
var fragmentsCode = null;
if (fragments.length) {
fragmentsCode = '[' + fragments.join(',') + ']';
}
return {
fields: printFields(fields, type, options, requisiteFields, parentType),
fragments: fragmentsCode,
};
}
function printArguments(args, options) {
if (!args) {
return null;
}
var value = args.value;
if (value.kind === kinds.LIST) {
return '[' + value.values.map(function(arg) {
return printArgument(arg, options)
}).join(', ') + ']';
} else {
return printArgument(value, options);
}
}
function printArgument(arg, options) {
switch (arg.kind) {
case kinds.INT:
return JSON.stringify(parseInt(arg.value, 10));
case kinds.FLOAT:
return JSON.stringify(parseFloat(arg.value));
case kinds.STRING:
case kinds.ENUM:
case kinds.BOOLEAN:
return JSON.stringify(arg.value);
case kinds.VARIABLE:
if (!arg.name || arg.name.kind !== kinds.NAME) {
throw new Error('Expected variable to have a name');
}
return printCallVariable(arg.name.value);
default:
throw new Error('Unexpected arg kind: ' + arg.kind);
}
}
function printCallVariable(name) {
return 'new GraphQL.CallVariable(' + JSON.stringify(name) + ')';
}
function printFields(fields, type, options, requisiteFields, parentType) {
var generateFields = {};
Object.keys(requisiteFields).forEach(function(name) {
generateFields[name] = true;
});
var fieldStrings = fields.map(function(field) {
var fieldName = getName(field);
delete generateFields[fieldName];
return printField(field, type, options, requisiteFields, false, parentType);
});
Object.keys(generateFields).forEach(function(fieldName) {
var generatedAST = {
kind: kinds.FIELD,
name: { kind: kinds.NAME, value: fieldName },
selectionSet: {selections: []},
arguments: [],
};
fieldStrings.push(
printField(generatedAST, type, options, requisiteFields, true, parentType)
);
});
if (fieldStrings.length === 0) {
return null;
}
return '[' + fieldStrings.join(', ') + ']';
}
function printFragmentReference(substitutionName, options) {
return options.rqlFunctionName + '.__frag(' + substitutionName + ')';
}
function printField(
field,
type,
options,
requisiteFields,
isGenerated,
parentType
) {
var fieldName = getName(field);
var fieldDecl = types.getNamedType(type).getFields()[fieldName];
var metadata = {
parentType: parentType,
};
if (!fieldDecl) {
throw new Error(util.format(
'Type "%s" doesn\'t have a field "%s".',
type.name,
fieldName
));
}
var subRequisiteFields = {};
if (hasIdField(types.getNamedType(fieldDecl.type))) {
subRequisiteFields.id = true;
}
// TODO: generalize to types that do not implement `Node`
// var rootCall = getRootCallForType(options.schema, fieldDecl.type);
// if (rootCall) {
// metadata.rootCall = rootCall.name;
// if (rootCall.arg) {
// metadata.pk = rootCall.arg;
// }
// }
if (alwaysImplementsNode(options.schema, fieldDecl.type)) {
metadata.rootCall = 'node';
metadata.pk = 'id';
}
if (isConnection(options.schema, fieldDecl)) {
metadata.connection = true;
if (!getArgNamed(fieldDecl, 'find')) {
metadata.nonFindable = true;
}
var hasEdgesSelection = false;
var selections = getSelections(field);
selections.forEach(function(subfield) {
var subfieldName = getName(subfield);
if (subfieldName === 'nodes') {
throw new Error(util.format(
'Unsupported "nodes" field on connection, `%s`. Instead, use ' +
'"edges{node{...}}".',
fieldName
));
}
if (subfieldName === 'edges') {
hasEdgesSelection = true;
}
});
if (hasEdgesSelection && !!fieldDecl.type.getFields()['pageInfo']) {
subRequisiteFields.pageInfo = true;
}
} else if (types.getNamedType(fieldDecl.type).name === 'PageInfo') {
subRequisiteFields.hasNextPage = true;
subRequisiteFields.hasPreviousPage = true;
} else if (isEdgeType(fieldDecl.type)) {
subRequisiteFields.cursor = true;
subRequisiteFields.node = true;
}
if (types.isAbstractType(fieldDecl.type)) {
metadata.dynamic = true;
}
if (isList(fieldDecl.type)) {
metadata.plural = true;
}
var fieldsAndFragments = printFieldsAndFragments(
field.selectionSet,
fieldDecl.type,
options,
subRequisiteFields,
types.getNamedType(fieldDecl.type).name
);
var fields = fieldsAndFragments.fields;
var fragments = fieldsAndFragments.fragments;
if (isGenerated) {
metadata.generated = true;
}
if (requisiteFields.hasOwnProperty(fieldName)) {
metadata.requisite = true;
}
var callsCode = printCalls(field, fieldDecl, options);
var fieldAliasCode = field.alias ?
JSON.stringify(field.alias.value) :
null;
var metadataCode = stringifyObject(metadata);
var argsCode = getFunctionArgCode([
JSON.stringify(fieldName),
fields,
fragments,
callsCode,
fieldAliasCode,
null,
metadataCode
]);
return 'new GraphQL.Field(' + argsCode + ')';
}
function printCalls(field, fieldDecl, options) {
if (field.arguments.length === 0) {
return null;
}
// Each GraphQL RFC argument is mapped to a separate call. For GraphQL FB
// calls with multiple arguments, we use GraphQL RFC array literals.
var callStrings = field.arguments.map(function(arg) {
var callName = getName(arg);
var callDecl = getArgNamed(fieldDecl, callName);
if (!callDecl) {
throw new Error(util.format(
'Unknown call "%s" on field "%s".',
callName,
fieldDecl.name
));
}
var metadata = {};
var typeName = getTypeForMetadata(callDecl.type);
if (typeName !== 'scalar') {
metadata.type = typeName;
}
return (
'new GraphQL.Callv(' +
getFunctionArgCode([
JSON.stringify(callName),
printArguments(arg, options),
stringifyObject(metadata),
]) +
')'
);
});
return '[' + callStrings.join(', ') + ']';
}
/**
* Collects the values of the `@relay` directive in an object, if the directive
* is defined and has values.
*
* Input:
* `fragment on User @relay(plural: true) {...}`
* Output:
* `{plural: true}`
*/
function getRelayDirectiveMetadata(node) {
var relayDirective;
node.directives.forEach(function(directive) {
if (getName(directive) === 'relay') {
relayDirective = directive;
}
});
if (!relayDirective) {
return;
}
return relayDirective.arguments.reduce(function(acc, arg) {
acc[getName(arg)] = getScalarValue(arg);
return acc;
}, {});
}
function getScalarValue(node) {
if (node && node.value && node.value.kind) {
var kind = node.value.kind;
var value = node.value.value;
if (kind === 'BooleanValue') {
return !!value;
} else if (kind === 'IntValue') {
return parseInt(value, 10);
} else {
if (kind !== 'StringValue') {
throw new Error(
'Expected `@relay(...)` argument values to be scalars, got ' +
kind
);
}
return value;
}
}
}
function getTypeForMetadata(type) {
type = types.getNamedType(type);
if (type instanceof types.GraphQLEnumType) {
return 'enum';
} else if (type instanceof types.GraphQLInputObjectType) {
return 'object';
} else if (type instanceof types.GraphQLScalarType) {
return 'scalar';
}
throw new Error('Unsupported call value type ' + type.name);
}
function isEnum(type) {
return types.getNullableType(type) instanceof types.GraphQLEnumType;
}
function isList(type) {
return types.getNullableType(type) instanceof types.GraphQLList;
}
function getName(node) {
if (node && node.name && node.name.kind === kinds.NAME) {
return node.name.value;
} else if (node && node.typeCondition) {
return getTypeName(node);
}
throw new Error('Expected node to have a name');
}
function getTypeName(node) {
if (node && node.typeCondition) {
if (node.typeCondition.kind === kinds.NAMED_TYPE) {
return getName(node.typeCondition);
} else if (node.typeCondition.kind === kinds.NAME) {
return node.typeCondition.value;
}
}
throw new Error('Expected node to have a name');
}
function isOrImplementsNode(schema, type) {
var namedType = types.getNamedType(type);
if (namedType.name === 'Node') {
return true;
}
if (!(namedType instanceof types.GraphQLObjectType)) {
return false;
}
var node = schema.getType('Node');
return namedType.getInterfaces().indexOf(node) !== -1;
}
function mightImplementNode(schema, type) {
var namedType = types.getNamedType(type);
if (isOrImplementsNode(schema, namedType)) {
return true;
}
if (!types.isAbstractType(namedType)) {
return false;
}
return namedType.getPossibleTypes().some(function (subtype) {
return isOrImplementsNode(schema, subtype);
});
}
function alwaysImplementsNode(schema, type) {
var namedType = types.getNamedType(type);
if (isOrImplementsNode(schema, namedType)) {
return true;
}
if (!types.isAbstractType(namedType)) {
return false;
}
return namedType.getPossibleTypes().every(function (subtype) {
return isOrImplementsNode(schema, subtype);
});
}
function getRootCallForType(schema, type) {
if (alwaysImplementsNode(schema, type)) {
return {name: 'node', arg: 'id'};
}
return null;
}
function isConnectionType(type) {
return /.+Connection$/.test(type.name);
}
function hasIdField(type) {
return !!(type.getFields && type.getFields()['id']);
}
function isEdgeType(type) {
var namedType = types.getNamedType(type);
return /.+Edge$/.test(namedType.name) &&
!!namedType.getFields()['node'] &&
!!namedType.getFields()['cursor'];
}
function getArgNamed(field, name) {
var remaining = field.args.filter(function (arg) {
return (arg.name === name);
});
return remaining.length === 1 ? remaining[0] : null;
}
function isConnection(schema, fieldDecl) {
if (!isConnectionType(fieldDecl.type)) {
return false;
}
// Connections must be limitable.
if (!getArgNamed(fieldDecl, 'first') && !getArgNamed(fieldDecl, 'last')) {
return false;
}
var fieldType = types.getNamedType(fieldDecl.type);
// Connections must have a non-scalar `edges` field.
var edgesField = fieldType.getFields()['edges'];
if (!edgesField) {
return false;
}
var edgesType = types.getNamedType(edgesField.type);
if (edgesType instanceof types.GraphQLScalarType) {
return false;
}
// Connections' `edges` field must have a non-scalar `node` field.
var edgesType = types.getNamedType(edgesField.type);
var nodeField = edgesType.getFields()['node'];
if (!nodeField) {
return false;
}
var nodeType = types.getNamedType(nodeField.type);
if (nodeType instanceof types.GraphQLScalarType) {
return false;
}
// Connections' `edges` field must have a scalar `cursor` field.
var cursorField = edgesType.getFields()['cursor'];
if (!cursorField) {
return false;
}
var cursorType = types.getNamedType(cursorField.type);
if (!(cursorType instanceof types.GraphQLScalarType)) {
return false;
}
return true;
}
function trimArray(arr) {
var lastIndex = -1;
for (var ii = arr.length - 1; ii >= 0; ii--) {
if (arr[ii] !== null) {
lastIndex = ii;
break;
}
}
arr.length = lastIndex + 1;
return arr;
}
function stringifyObject(obj) {
for (var ii in obj) {
if (obj.hasOwnProperty(ii)) {
return JSON.stringify(obj);
}
}
return null;
}
function getFunctionArgCode(arr) {
return trimArray(arr)
.map(function(arg) {
return arg === null ? 'null' : arg;
})
.join(', ');
}
function getSelections(node) {
if (node.selectionSet && node.selectionSet.selections) {
return node.selectionSet.selections;
}
return [];
}
module.exports = GraphQLPrinter;