Mirror: The small sibling of the graphql package, slimmed down for client-side libraries.
1import { Kind } from 'graphql';
2import { printBlockString } from './blockString';
3import { printString } from './printString';
4
5export function print(node) {
6 if (Array.isArray(node)) {
7 return node.map(print);
8 } else if (node == null || typeof node !== 'object') {
9 return node ? ('' + node) : '';
10 }
11
12 switch (node.kind) {
13 case 'OperationDefinition': {
14 const prefix = join(
15 [
16 node.operation,
17 print(node.name) + wrap('(', join(print(node.variableDefinitions), ', '), ')'),
18 join(print(node.directives), ' '),
19 ],
20 ' ',
21 );
22
23 return (prefix === 'query' ? '' : prefix + ' ') + print(node.selectionSet);
24 }
25
26 case 'VariableDefinition':
27 return print(node.variable) +
28 ': ' +
29 print(node.type) +
30 wrap(' = ', print(node.defaultValue)) +
31 wrap(' ', join(print(node.directives), ' '));
32
33 case 'Field':
34 return join(
35 [
36 wrap('', print(node.alias), ': ')
37 + print(node.name)
38 + wrap('(', join(print(node.arguments), ', '), ')'),
39 join(print(node.directives), ' '),
40 print(node.selectionSet)
41 ],
42 ' '
43 );
44
45 case 'StringValue':
46 return node.isBlockString ? printBlockString(node.value) : printString(node.value);
47
48 case 'BooleanValue':
49 return node.value ? 'true' : 'false';
50
51 case 'NullValue':
52 return 'null';
53
54 case 'IntValue':
55 case 'FloatValue':
56 case 'EnumValue':
57 case 'Name':
58 return node.value;
59
60 case 'ListValue':
61 return '[' + join(print(node.values), ', ') + ']';
62
63 case 'ObjectValue':
64 return '{' + join(print(node.fields), ', ') + '}';
65
66 case 'ObjectField':
67 return node.name.value + ': ' + print(node.value);
68
69 case 'Variable': return '$' + node.name.value;
70 case 'Document':
71 return join(print(node.definitions), '\n\n');
72 case 'SelectionSet': return block(print(node.selections));
73 case 'Argument': return node.name.value + ': ' + print(node.value);
74
75 case 'FragmentSpread':
76 return '...' + print(node.name) + wrap(' ', join(print(node.directives), ' '));
77
78 case 'InlineFragment':
79 return join(
80 [
81 '...',
82 wrap('on ', print(node.typeCondition)),
83 join(print(node.directives), ' '),
84 print(node.selectionSet),
85 ],
86 ' ',
87 );
88
89 case 'FragmentDefinition':
90 return 'fragment ' + node.name.value +
91 wrap('(', join(print(node.variableDefinitions), ', '), ')') + ' ' +
92 'on ' + print(node.typeCondition) + ' ' +
93 wrap('', join(print(node.directives), ' '), ' ') +
94 print(node.selectionSet);
95
96 case 'Directive':
97 return '@' + node.name.value + wrap('(', join(print(node.args), ', '), ')');
98
99 case 'NamedType':
100 return node.name.value;
101
102 case 'ListType':
103 return '[' + print(node.type) + ']';
104
105 case 'NonNullType':
106 return print(node.type) + '!';
107
108 default: return '';
109 }
110}
111
112const join = (array, separator) =>
113 (array && array
114 .filter(x => x)
115 .join(separator || '')) || '';
116
117const block = array =>
118 wrap('{\n ', join(array, '\n').replace(/\n/g, '\n '), '\n}');
119
120const wrap = (start, value, end) => value
121 ? start + value + (end || '')
122 : '';