···
1
-
import type { ASTNode } from './ast';
11
+
VariableDefinitionNode,
12
+
OperationDefinitionNode,
13
+
FragmentDefinitionNode,
29
+
function mapJoin<T>(value: readonly T[], joiner: string, mapper: (value: T) => string): string {
31
+
for (let index = 0; index < value.length; index++) {
32
+
if (index) out += joiner;
33
+
out += mapper(value[index]);
3
-
export function printString(string: string) {
38
+
function printString(string: string) {
return JSON.stringify(string);
7
-
export function printBlockString(string: string) {
42
+
function printBlockString(string: string) {
return '"""\n' + string.replace(/"""/g, '\\"""') + '\n"""';
11
-
const hasItems = <T>(array: ReadonlyArray<T> | undefined | null): array is ReadonlyArray<T> =>
12
-
!!(array && array.length);
const MAX_LINE_LENGTH = 80;
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);
51
+
OperationDefinition(node: OperationDefinitionNode): string {
let out: string = node.operation;
if (node.name) out += ' ' + node.name.value;
30
-
if (hasItems(node.variableDefinitions)) {
54
+
if (node.variableDefinitions && node.variableDefinitions.length) {
if (!node.name) out += ' ';
32
-
out += '(' + node.variableDefinitions.map(nodes.VariableDefinition!).join(', ') + ')';
56
+
out += '(' + mapJoin(node.variableDefinitions, ', ', nodes.VariableDefinition) + ')';
34
-
if (hasItems(node.directives)) out += ' ' + node.directives.map(nodes.Directive!).join(' ');
35
-
return out + ' ' + nodes.SelectionSet!(node.selectionSet);
58
+
if (node.directives && node.directives.length)
59
+
out += ' ' + mapJoin(node.directives, ' ', nodes.Directive);
60
+
return out !== 'query'
61
+
? out + ' ' + nodes.SelectionSet(node.selectionSet)
62
+
: 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(' ');
64
+
VariableDefinition(node: VariableDefinitionNode): string {
65
+
let out = nodes.Variable!(node.variable) + ': ' + _print(node.type);
66
+
if (node.defaultValue) out += ' = ' + _print(node.defaultValue);
67
+
if (node.directives && node.directives.length)
68
+
out += ' ' + mapJoin(node.directives, ' ', nodes.Directive);
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)'
71
+
Field(node: FieldNode): string {
72
+
let out = node.alias ? node.alias.value + ': ' + node.name.value : node.name.value;
73
+
if (node.arguments && node.arguments.length) {
74
+
const args = mapJoin(node.arguments, ', ', nodes.Argument);
75
+
if (out.length + args.length + 2 > MAX_LINE_LENGTH) {
79
+
mapJoin(node.arguments, LF, nodes.Argument) +
80
+
(LF = LF.slice(0, -2)) +
83
+
out += '(' + args + ')';
53
-
if (hasItems(node.directives)) out += ' ' + node.directives.map(nodes.Directive!).join(' ');
54
-
return node.selectionSet ? out + ' ' + nodes.SelectionSet!(node.selectionSet) : out;
86
+
if (node.directives && node.directives.length)
87
+
out += ' ' + mapJoin(node.directives, ' ', nodes.Directive);
88
+
if (node.selectionSet) out += ' ' + nodes.SelectionSet(node.selectionSet);
57
-
return node.block ? printBlockString(node.value) : printString(node.value);
91
+
StringValue(node: StringValueNode): string {
93
+
return printBlockString(node.value).replace(/\n/g, LF);
95
+
return printString(node.value);
59
-
BooleanValue(node) {
98
+
BooleanValue(node: BooleanValueNode): string {
101
+
NullValue(_node: NullValueNode): string {
104
+
IntValue(node: IntValueNode): string {
107
+
FloatValue(node: FloatValueNode): string {
110
+
EnumValue(node: EnumValueNode): string {
113
+
Name(node: NameNode): string {
116
+
Variable(node: VariableNode): string {
return '$' + node.name.value;
81
-
return '[' + node.values.map(print).join(', ') + ']';
119
+
ListValue(node: ListValueNode): string {
120
+
return '[' + mapJoin(node.values, ', ', _print) + ']';
84
-
return '{' + node.fields.map(nodes.ObjectField!).join(', ') + '}';
122
+
ObjectValue(node: ObjectValueNode): string {
123
+
return '{' + mapJoin(node.fields, ', ', nodes.ObjectField) + '}';
87
-
return node.name.value + ': ' + print(node.value);
125
+
ObjectField(node: ObjectFieldNode): string {
126
+
return node.name.value + ': ' + _print(node.value);
90
-
return hasItems(node.definitions) ? node.definitions.map(print).join('\n\n') : '';
128
+
Document(node: DocumentNode): string {
129
+
if (!node.definitions || !node.definitions.length) return '';
130
+
return mapJoin(node.definitions, '\n\n', _print);
92
-
SelectionSet(node) {
93
-
return '{\n ' + node.selections.map(print).join('\n').replace(/\n/g, '\n ') + '\n}';
132
+
SelectionSet(node: SelectionSetNode): string {
133
+
return '{' + (LF += ' ') + mapJoin(node.selections, LF, _print) + (LF = LF.slice(0, -2)) + '}';
96
-
return node.name.value + ': ' + print(node.value);
135
+
Argument(node: ArgumentNode): string {
136
+
return node.name.value + ': ' + _print(node.value);
98
-
FragmentSpread(node) {
138
+
FragmentSpread(node: FragmentSpreadNode): string {
let out = '...' + node.name.value;
100
-
if (hasItems(node.directives)) out += ' ' + node.directives.map(nodes.Directive!).join(' ');
140
+
if (node.directives && node.directives.length)
141
+
out += ' ' + mapJoin(node.directives, ' ', nodes.Directive);
103
-
InlineFragment(node) {
144
+
InlineFragment(node: InlineFragmentNode): string {
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);
147
+
if (node.directives && node.directives.length)
148
+
out += ' ' + mapJoin(node.directives, ' ', nodes.Directive);
149
+
out += ' ' + nodes.SelectionSet(node.selectionSet);
109
-
FragmentDefinition(node) {
152
+
FragmentDefinition(node: FragmentDefinitionNode): string {
let out = 'fragment ' + node.name.value;
out += ' on ' + node.typeCondition.name.value;
112
-
if (hasItems(node.directives)) out += ' ' + node.directives.map(nodes.Directive!).join(' ');
113
-
return out + ' ' + print(node.selectionSet);
155
+
if (node.directives && node.directives.length)
156
+
out += ' ' + mapJoin(node.directives, ' ', nodes.Directive);
157
+
return out + ' ' + nodes.SelectionSet(node.selectionSet);
159
+
Directive(node: DirectiveNode): string {
let out = '@' + node.name.value;
117
-
if (hasItems(node.arguments)) out += '(' + node.arguments.map(nodes.Argument!).join(', ') + ')';
161
+
if (node.arguments && node.arguments.length)
162
+
out += '(' + mapJoin(node.arguments, ', ', nodes.Argument) + ')';
165
+
NamedType(node: NamedTypeNode): string {
124
-
return '[' + print(node.type) + ']';
168
+
ListType(node: ListTypeNode): string {
169
+
return '[' + _print(node.type) + ']';
126
-
NonNullType(node) {
127
-
return print(node.type) + '!';
171
+
NonNullType(node: NonNullTypeNode): string {
172
+
return _print(node.type) + '!';
131
-
export function print(node: ASTNode): string {
132
-
return nodes[node.kind] ? (nodes as any)[node.kind]!(node) : '';
176
+
const _print = (node: ASTNode): string => nodes[node.kind](node);
178
+
function print(node: ASTNode): string {
180
+
return nodes[node.kind] ? nodes[node.kind](node) : '';
183
+
export { print, printString, printBlockString };