···
const MAX_LINE_LENGTH = 80;
16
-
export function print(node: ASTNode): string {
18
-
switch (node.kind) {
19
-
case 'OperationDefinition':
21
-
node.operation === 'query' &&
23
-
!hasItems(node.variableDefinitions) &&
24
-
!hasItems(node.directives)
26
-
return print(node.selectionSet);
28
-
out = node.operation;
29
-
if (node.name) out += ' ' + node.name.value;
30
-
if (hasItems(node.variableDefinitions)) {
31
-
if (!node.name) out += ' ';
32
-
out += '(' + node.variableDefinitions.map(print).join(', ') + ')';
34
-
if (hasItems(node.directives)) out += ' ' + node.directives.map(print).join(' ');
35
-
return out + ' ' + print(node.selectionSet);
37
-
case 'VariableDefinition':
38
-
out = print(node.variable) + ': ' + print(node.type);
39
-
if (node.defaultValue) out += ' = ' + print(node.defaultValue);
40
-
if (hasItems(node.directives)) out += ' ' + node.directives.map(print).join(' ');
44
-
out = (node.alias ? print(node.alias) + ': ' : '') + node.name.value;
45
-
if (hasItems(node.arguments)) {
46
-
const args = node.arguments.map(print);
47
-
const argsLine = out + '(' + args.join(', ') + ')';
49
-
argsLine.length > MAX_LINE_LENGTH
50
-
? out + '(\n ' + args.join('\n').replace(/\n/g, '\n ') + '\n)'
53
-
if (hasItems(node.directives)) out += ' ' + node.directives.map(print).join(' ');
54
-
return node.selectionSet ? out + ' ' + print(node.selectionSet) : out;
57
-
return node.block ? printBlockString(node.value) : printString(node.value);
59
-
case 'BooleanValue':
60
-
return '' + node.value;
72
-
return '[' + node.values.map(print).join(', ') + ']';
75
-
return '{' + node.fields.map(print).join(', ') + '}';
78
-
return node.name.value + ': ' + print(node.value);
81
-
return '$' + node.name.value;
84
-
return hasItems(node.definitions) ? node.definitions.map(print).join('\n\n') : '';
86
-
case 'SelectionSet':
87
-
return '{\n ' + node.selections.map(print).join('\n').replace(/\n/g, '\n ') + '\n}';
90
-
return node.name.value + ': ' + print(node.value);
92
-
case 'FragmentSpread':
93
-
out = '...' + node.name.value;
94
-
if (hasItems(node.directives)) out += ' ' + node.directives.map(print).join(' ');
97
-
case 'InlineFragment':
99
-
if (node.typeCondition) out += ' on ' + node.typeCondition.name.value;
100
-
if (hasItems(node.directives)) out += ' ' + node.directives.map(print).join(' ');
101
-
return out + ' ' + print(node.selectionSet);
103
-
case 'FragmentDefinition':
104
-
out = 'fragment ' + node.name.value;
105
-
out += ' on ' + node.typeCondition.name.value;
106
-
if (hasItems(node.directives)) out += ' ' + node.directives.map(print).join(' ');
107
-
return out + ' ' + print(node.selectionSet);
110
-
out = '@' + node.name.value;
111
-
if (hasItems(node.arguments)) out += '(' + node.arguments.map(print).join(', ') + ')';
115
-
return node.name.value;
118
-
return '[' + print(node.type) + ']';
120
-
case 'NonNullType':
121
-
return print(node.type) + '!';
17
+
[NodeT in ASTNode as NodeT['kind']]?: (node: NodeT) => string;
19
+
OperationDefinition(node) {
21
+
node.operation === 'query' &&
23
+
!hasItems(node.variableDefinitions) &&
24
+
!hasItems(node.directives)
26
+
return nodes.SelectionSet!(node.selectionSet);
28
+
let out: string = node.operation;
29
+
if (node.name) out += ' ' + node.name.value;
30
+
if (hasItems(node.variableDefinitions)) {
31
+
if (!node.name) out += ' ';
32
+
out += '(' + node.variableDefinitions.map(nodes.VariableDefinition!).join(', ') + ')';
34
+
if (hasItems(node.directives)) out += ' ' + node.directives.map(nodes.Directive!).join(' ');
35
+
return out + ' ' + nodes.SelectionSet!(node.selectionSet);
37
+
VariableDefinition(node) {
38
+
let out = nodes.Variable!(node.variable) + ': ' + print(node.type);
39
+
if (node.defaultValue) out += ' = ' + print(node.defaultValue);
40
+
if (hasItems(node.directives)) out += ' ' + node.directives.map(nodes.Directive!).join(' ');
44
+
let out = (node.alias ? node.alias.value + ': ' : '') + node.name.value;
45
+
if (hasItems(node.arguments)) {
46
+
const args = node.arguments.map(nodes.Argument!);
47
+
const argsLine = out + '(' + args.join(', ') + ')';
49
+
argsLine.length > MAX_LINE_LENGTH
50
+
? out + '(\n ' + args.join('\n').replace(/\n/g, '\n ') + '\n)'
53
+
if (hasItems(node.directives)) out += ' ' + node.directives.map(nodes.Directive!).join(' ');
54
+
return node.selectionSet ? out + ' ' + nodes.SelectionSet!(node.selectionSet) : out;
57
+
return node.block ? printBlockString(node.value) : printString(node.value);
59
+
BooleanValue(node) {
60
+
return '' + node.value;
78
+
return '$' + node.name.value;
81
+
return '[' + node.values.map(print).join(', ') + ']';
84
+
return '{' + node.fields.map(nodes.ObjectField!).join(', ') + '}';
87
+
return node.name.value + ': ' + print(node.value);
90
+
return hasItems(node.definitions) ? node.definitions.map(print).join('\n\n') : '';
92
+
SelectionSet(node) {
93
+
return '{\n ' + node.selections.map(print).join('\n').replace(/\n/g, '\n ') + '\n}';
96
+
return node.name.value + ': ' + print(node.value);
98
+
FragmentSpread(node) {
99
+
let out = '...' + node.name.value;
100
+
if (hasItems(node.directives)) out += ' ' + node.directives.map(nodes.Directive!).join(' ');
103
+
InlineFragment(node) {
105
+
if (node.typeCondition) out += ' on ' + node.typeCondition.name.value;
106
+
if (hasItems(node.directives)) out += ' ' + node.directives.map(nodes.Directive!).join(' ');
107
+
return out + ' ' + print(node.selectionSet);
109
+
FragmentDefinition(node) {
110
+
let out = 'fragment ' + node.name.value;
111
+
out += ' on ' + node.typeCondition.name.value;
112
+
if (hasItems(node.directives)) out += ' ' + node.directives.map(nodes.Directive!).join(' ');
113
+
return out + ' ' + print(node.selectionSet);
116
+
let out = '@' + node.name.value;
117
+
if (hasItems(node.arguments)) out += '(' + node.arguments.map(nodes.Argument!).join(', ') + ')';
121
+
return node.name.value;
124
+
return '[' + print(node.type) + ']';
126
+
NonNullType(node) {
127
+
return print(node.type) + '!';
131
+
export function print(node: ASTNode): string {
132
+
return nodes[node.kind] ? nodes[node.kind]!(node as any) : '';