/
printer.rb
377 lines (330 loc) · 12.3 KB
/
printer.rb
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
# frozen_string_literal: true
module GraphQL
module Language
class Printer
# Turn an arbitrary AST node back into a string.
#
# @example Turning a document into a query string
# document = GraphQL.parse(query_string)
# GraphQL::Language::Printer.new.print(document)
# # => "{ ... }"
#
#
# @example Building a custom printer
#
# class MyPrinter < GraphQL::Language::Printer
# def print_argument(arg)
# "#{arg.name}: <HIDDEN>"
# end
# end
#
# MyPrinter.new.print(document)
# # => "mutation { pay(creditCard: <HIDDEN>) { success } }"
#
#
# @param indent [String] Whitespace to add to the printed node
# @return [String] Valid GraphQL for `node`
def print(node, indent: "")
print_node(node, indent: indent)
end
protected
def print_document(document)
document.definitions.map { |d| print_node(d) }.join("\n\n")
end
def print_argument(argument)
"#{argument.name}: #{print_node(argument.value)}".dup
end
def print_directive(directive)
out = "@#{directive.name}".dup
if directive.arguments.any?
out << "(#{directive.arguments.map { |a| print_argument(a) }.join(", ")})"
end
out
end
def print_enum(enum)
"#{enum.name}".dup
end
def print_null_value
"null".dup
end
def print_field(field, indent: "")
out = "#{indent}".dup
out << "#{field.alias}: " if field.alias
out << "#{field.name}"
out << "(#{field.arguments.map { |a| print_argument(a) }.join(", ")})" if field.arguments.any?
out << print_directives(field.directives)
out << print_selections(field.selections, indent: indent)
out
end
def print_fragment_definition(fragment_def, indent: "")
out = "#{indent}fragment #{fragment_def.name}".dup
if fragment_def.type
out << " on #{print_node(fragment_def.type)}"
end
out << print_directives(fragment_def.directives)
out << print_selections(fragment_def.selections, indent: indent)
out
end
def print_fragment_spread(fragment_spread, indent: "")
out = "#{indent}...#{fragment_spread.name}".dup
out << print_directives(fragment_spread.directives)
out
end
def print_inline_fragment(inline_fragment, indent: "")
out = "#{indent}...".dup
if inline_fragment.type
out << " on #{print_node(inline_fragment.type)}"
end
out << print_directives(inline_fragment.directives)
out << print_selections(inline_fragment.selections, indent: indent)
out
end
def print_input_object(input_object)
"{#{input_object.arguments.map { |a| print_argument(a) }.join(", ")}}"
end
def print_list_type(list_type)
"[#{print_node(list_type.of_type)}]".dup
end
def print_non_null_type(non_null_type)
"#{print_node(non_null_type.of_type)}!".dup
end
def print_operation_definition(operation_definition, indent: "")
out = "#{indent}#{operation_definition.operation_type}".dup
out << " #{operation_definition.name}" if operation_definition.name
if operation_definition.variables.any?
out << "(#{operation_definition.variables.map { |v| print_variable_definition(v) }.join(", ")})"
end
out << print_directives(operation_definition.directives)
out << print_selections(operation_definition.selections, indent: indent)
out
end
def print_type_name(type_name)
"#{type_name.name}".dup
end
def print_variable_definition(variable_definition)
out = "$#{variable_definition.name}: #{print_node(variable_definition.type)}".dup
out << " = #{print_node(variable_definition.default_value)}" unless variable_definition.default_value.nil?
out
end
def print_variable_identifier(variable_identifier)
"$#{variable_identifier.name}".dup
end
def print_schema_definition(schema)
if (schema.query.nil? || schema.query == 'Query') &&
(schema.mutation.nil? || schema.mutation == 'Mutation') &&
(schema.subscription.nil? || schema.subscription == 'Subscription') &&
(schema.directives.empty?)
return
end
out = "schema".dup
if schema.directives.any?
schema.directives.each do |dir|
out << "\n "
out << print_node(dir)
end
out << "\n{"
else
out << " {\n"
end
out << " query: #{schema.query}\n" if schema.query
out << " mutation: #{schema.mutation}\n" if schema.mutation
out << " subscription: #{schema.subscription}\n" if schema.subscription
out << "}"
end
def print_scalar_type_definition(scalar_type)
out = print_description(scalar_type)
out << "scalar #{scalar_type.name}"
out << print_directives(scalar_type.directives)
end
def print_object_type_definition(object_type)
out = print_description(object_type)
out << "type #{object_type.name}"
out << print_implements(object_type) unless object_type.interfaces.empty?
out << print_directives(object_type.directives)
out << print_field_definitions(object_type.fields)
end
def print_implements(type)
" implements #{type.interfaces.map(&:name).join(" & ")}"
end
def print_input_value_definition(input_value)
out = "#{input_value.name}: #{print_node(input_value.type)}".dup
out << " = #{print_node(input_value.default_value)}" unless input_value.default_value.nil?
out << print_directives(input_value.directives)
end
def print_arguments(arguments, indent: "")
if arguments.all?{ |arg| !arg.description }
return "(#{arguments.map{ |arg| print_input_value_definition(arg) }.join(", ")})"
end
out = "(\n".dup
out << arguments.map.with_index{ |arg, i|
"#{print_description(arg, indent: " " + indent, first_in_block: i == 0)} #{indent}"\
"#{print_input_value_definition(arg)}"
}.join("\n")
out << "\n#{indent})"
end
def print_field_definition(field)
out = field.name.dup
unless field.arguments.empty?
out << print_arguments(field.arguments, indent: " ")
end
out << ": #{print_node(field.type)}"
out << print_directives(field.directives)
end
def print_interface_type_definition(interface_type)
out = print_description(interface_type)
out << "interface #{interface_type.name}"
out << print_implements(interface_type) if interface_type.interfaces.any?
out << print_directives(interface_type.directives)
out << print_field_definitions(interface_type.fields)
end
def print_union_type_definition(union_type)
out = print_description(union_type)
out << "union #{union_type.name}"
out << print_directives(union_type.directives)
out << " = " + union_type.types.map(&:name).join(" | ")
end
def print_enum_type_definition(enum_type)
out = print_description(enum_type)
out << "enum #{enum_type.name}#{print_directives(enum_type.directives)} {\n"
enum_type.values.each.with_index do |value, i|
out << print_description(value, indent: ' ', first_in_block: i == 0)
out << print_enum_value_definition(value)
end
out << "}"
end
def print_enum_value_definition(enum_value)
out = " #{enum_value.name}".dup
out << print_directives(enum_value.directives)
out << "\n"
end
def print_input_object_type_definition(input_object_type)
out = print_description(input_object_type)
out << "input #{input_object_type.name}"
out << print_directives(input_object_type.directives)
if !input_object_type.fields.empty?
out << " {\n"
input_object_type.fields.each.with_index do |field, i|
out << print_description(field, indent: ' ', first_in_block: i == 0)
out << " #{print_input_value_definition(field)}\n"
end
out << "}"
end
out
end
def print_directive_definition(directive)
out = print_description(directive)
out << "directive @#{directive.name}"
if directive.arguments.any?
out << print_arguments(directive.arguments)
end
if directive.repeatable
out << " repeatable"
end
out << " on #{directive.locations.map(&:name).join(' | ')}"
end
def print_description(node, indent: "", first_in_block: true)
return ''.dup unless node.description
description = indent != '' && !first_in_block ? "\n".dup : "".dup
description << GraphQL::Language::BlockString.print(node.description, indent: indent)
end
def print_field_definitions(fields)
if fields.empty?
""
else
out = " {\n".dup
fields.each.with_index do |field, i|
out << print_description(field, indent: ' ', first_in_block: i == 0)
out << " #{print_field_definition(field)}\n"
end
out << "}"
end
end
def print_directives(directives)
if directives.any?
directives.map { |d| " #{print_directive(d)}" }.join
else
""
end
end
def print_selections(selections, indent: "")
if selections.any?
out = " {\n".dup
selections.each do |selection|
out << print_node(selection, indent: indent + " ") << "\n"
end
out << "#{indent}}"
else
""
end
end
def print_node(node, indent: "")
case node
when Nodes::Document
print_document(node)
when Nodes::Argument
print_argument(node)
when Nodes::Directive
print_directive(node)
when Nodes::Enum
print_enum(node)
when Nodes::NullValue
print_null_value
when Nodes::Field
print_field(node, indent: indent)
when Nodes::FragmentDefinition
print_fragment_definition(node, indent: indent)
when Nodes::FragmentSpread
print_fragment_spread(node, indent: indent)
when Nodes::InlineFragment
print_inline_fragment(node, indent: indent)
when Nodes::InputObject
print_input_object(node)
when Nodes::ListType
print_list_type(node)
when Nodes::NonNullType
print_non_null_type(node)
when Nodes::OperationDefinition
print_operation_definition(node, indent: indent)
when Nodes::TypeName
print_type_name(node)
when Nodes::VariableDefinition
print_variable_definition(node)
when Nodes::VariableIdentifier
print_variable_identifier(node)
when Nodes::SchemaDefinition
print_schema_definition(node)
when Nodes::ScalarTypeDefinition
print_scalar_type_definition(node)
when Nodes::ObjectTypeDefinition
print_object_type_definition(node)
when Nodes::InputValueDefinition
print_input_value_definition(node)
when Nodes::FieldDefinition
print_field_definition(node)
when Nodes::InterfaceTypeDefinition
print_interface_type_definition(node)
when Nodes::UnionTypeDefinition
print_union_type_definition(node)
when Nodes::EnumTypeDefinition
print_enum_type_definition(node)
when Nodes::EnumValueDefinition
print_enum_value_definition(node)
when Nodes::InputObjectTypeDefinition
print_input_object_type_definition(node)
when Nodes::DirectiveDefinition
print_directive_definition(node)
when FalseClass, Float, Integer, NilClass, String, TrueClass, Symbol
GraphQL::Language.serialize(node)
when Array
"[#{node.map { |v| print_node(v) }.join(", ")}]".dup
when Hash
"{#{node.map { |k, v| "#{k}: #{print_node(v)}" }.join(", ")}}".dup
else
GraphQL::Language.serialize(node.to_s)
end
end
private
attr_reader :node
end
end
end