graphql 2.0.13 → 2.3.10

Sign up to get free protection for your applications and to get access to all the features.
Files changed (228) hide show
  1. checksums.yaml +4 -4
  2. data/lib/generators/graphql/install/mutation_root_generator.rb +2 -2
  3. data/lib/generators/graphql/install/templates/base_mutation.erb +2 -0
  4. data/lib/generators/graphql/install/templates/mutation_type.erb +2 -0
  5. data/lib/generators/graphql/install_generator.rb +3 -0
  6. data/lib/generators/graphql/mutation_delete_generator.rb +1 -1
  7. data/lib/generators/graphql/mutation_update_generator.rb +1 -1
  8. data/lib/generators/graphql/relay.rb +18 -1
  9. data/lib/generators/graphql/templates/base_argument.erb +2 -0
  10. data/lib/generators/graphql/templates/base_connection.erb +2 -0
  11. data/lib/generators/graphql/templates/base_edge.erb +2 -0
  12. data/lib/generators/graphql/templates/base_enum.erb +2 -0
  13. data/lib/generators/graphql/templates/base_field.erb +2 -0
  14. data/lib/generators/graphql/templates/base_input_object.erb +2 -0
  15. data/lib/generators/graphql/templates/base_interface.erb +2 -0
  16. data/lib/generators/graphql/templates/base_object.erb +2 -0
  17. data/lib/generators/graphql/templates/base_resolver.erb +6 -0
  18. data/lib/generators/graphql/templates/base_scalar.erb +2 -0
  19. data/lib/generators/graphql/templates/base_union.erb +2 -0
  20. data/lib/generators/graphql/templates/graphql_controller.erb +2 -0
  21. data/lib/generators/graphql/templates/loader.erb +2 -0
  22. data/lib/generators/graphql/templates/mutation.erb +2 -0
  23. data/lib/generators/graphql/templates/node_type.erb +2 -0
  24. data/lib/generators/graphql/templates/query_type.erb +2 -0
  25. data/lib/generators/graphql/templates/schema.erb +8 -0
  26. data/lib/graphql/analysis/analyzer.rb +89 -0
  27. data/lib/graphql/analysis/field_usage.rb +82 -0
  28. data/lib/graphql/analysis/max_query_complexity.rb +20 -0
  29. data/lib/graphql/analysis/max_query_depth.rb +20 -0
  30. data/lib/graphql/analysis/query_complexity.rb +183 -0
  31. data/lib/graphql/analysis/query_depth.rb +58 -0
  32. data/lib/graphql/analysis/visitor.rb +283 -0
  33. data/lib/graphql/analysis.rb +92 -1
  34. data/lib/graphql/backtrace/inspect_result.rb +0 -12
  35. data/lib/graphql/backtrace/table.rb +2 -2
  36. data/lib/graphql/backtrace/trace.rb +93 -0
  37. data/lib/graphql/backtrace/tracer.rb +1 -1
  38. data/lib/graphql/backtrace.rb +2 -1
  39. data/lib/graphql/coercion_error.rb +1 -9
  40. data/lib/graphql/dataloader/async_dataloader.rb +88 -0
  41. data/lib/graphql/dataloader/null_dataloader.rb +1 -1
  42. data/lib/graphql/dataloader/request.rb +5 -0
  43. data/lib/graphql/dataloader/source.rb +89 -45
  44. data/lib/graphql/dataloader.rb +115 -142
  45. data/lib/graphql/duration_encoding_error.rb +16 -0
  46. data/lib/graphql/execution/interpreter/argument_value.rb +5 -1
  47. data/lib/graphql/execution/interpreter/arguments.rb +1 -1
  48. data/lib/graphql/execution/interpreter/arguments_cache.rb +33 -33
  49. data/lib/graphql/execution/interpreter/resolve.rb +19 -0
  50. data/lib/graphql/execution/interpreter/runtime/graphql_result.rb +175 -0
  51. data/lib/graphql/execution/interpreter/runtime.rb +331 -455
  52. data/lib/graphql/execution/interpreter.rb +125 -61
  53. data/lib/graphql/execution/lazy.rb +6 -12
  54. data/lib/graphql/execution/lookahead.rb +124 -46
  55. data/lib/graphql/execution/multiplex.rb +3 -117
  56. data/lib/graphql/execution.rb +0 -1
  57. data/lib/graphql/introspection/directive_type.rb +3 -3
  58. data/lib/graphql/introspection/dynamic_fields.rb +1 -1
  59. data/lib/graphql/introspection/entry_points.rb +11 -5
  60. data/lib/graphql/introspection/field_type.rb +2 -2
  61. data/lib/graphql/introspection/schema_type.rb +10 -13
  62. data/lib/graphql/introspection/type_type.rb +17 -10
  63. data/lib/graphql/introspection.rb +3 -2
  64. data/lib/graphql/language/block_string.rb +34 -18
  65. data/lib/graphql/language/definition_slice.rb +1 -1
  66. data/lib/graphql/language/document_from_schema_definition.rb +75 -59
  67. data/lib/graphql/language/lexer.rb +358 -1506
  68. data/lib/graphql/language/nodes.rb +166 -93
  69. data/lib/graphql/language/parser.rb +795 -1953
  70. data/lib/graphql/language/printer.rb +340 -160
  71. data/lib/graphql/language/sanitized_printer.rb +21 -23
  72. data/lib/graphql/language/static_visitor.rb +167 -0
  73. data/lib/graphql/language/visitor.rb +188 -141
  74. data/lib/graphql/language.rb +61 -1
  75. data/lib/graphql/load_application_object_failed_error.rb +5 -1
  76. data/lib/graphql/pagination/active_record_relation_connection.rb +0 -8
  77. data/lib/graphql/pagination/array_connection.rb +6 -6
  78. data/lib/graphql/pagination/connection.rb +33 -6
  79. data/lib/graphql/pagination/mongoid_relation_connection.rb +1 -2
  80. data/lib/graphql/query/context/scoped_context.rb +101 -0
  81. data/lib/graphql/query/context.rb +117 -112
  82. data/lib/graphql/query/null_context.rb +12 -25
  83. data/lib/graphql/query/validation_pipeline.rb +6 -5
  84. data/lib/graphql/query/variables.rb +3 -3
  85. data/lib/graphql/query.rb +86 -30
  86. data/lib/graphql/railtie.rb +9 -6
  87. data/lib/graphql/rake_task.rb +29 -11
  88. data/lib/graphql/rubocop/graphql/base_cop.rb +1 -1
  89. data/lib/graphql/schema/addition.rb +59 -23
  90. data/lib/graphql/schema/always_visible.rb +11 -0
  91. data/lib/graphql/schema/argument.rb +55 -26
  92. data/lib/graphql/schema/base_64_encoder.rb +3 -5
  93. data/lib/graphql/schema/build_from_definition.rb +56 -32
  94. data/lib/graphql/schema/directive/one_of.rb +24 -0
  95. data/lib/graphql/schema/directive/specified_by.rb +14 -0
  96. data/lib/graphql/schema/directive/transform.rb +1 -1
  97. data/lib/graphql/schema/directive.rb +15 -3
  98. data/lib/graphql/schema/enum.rb +35 -24
  99. data/lib/graphql/schema/enum_value.rb +2 -3
  100. data/lib/graphql/schema/field/connection_extension.rb +2 -16
  101. data/lib/graphql/schema/field/scope_extension.rb +8 -1
  102. data/lib/graphql/schema/field.rb +147 -107
  103. data/lib/graphql/schema/field_extension.rb +1 -4
  104. data/lib/graphql/schema/find_inherited_value.rb +2 -7
  105. data/lib/graphql/schema/has_single_input_argument.rb +158 -0
  106. data/lib/graphql/schema/input_object.rb +47 -11
  107. data/lib/graphql/schema/interface.rb +15 -21
  108. data/lib/graphql/schema/introspection_system.rb +7 -17
  109. data/lib/graphql/schema/late_bound_type.rb +10 -0
  110. data/lib/graphql/schema/list.rb +2 -2
  111. data/lib/graphql/schema/loader.rb +2 -3
  112. data/lib/graphql/schema/member/base_dsl_methods.rb +18 -14
  113. data/lib/graphql/schema/member/build_type.rb +11 -3
  114. data/lib/graphql/schema/member/has_arguments.rb +170 -130
  115. data/lib/graphql/schema/member/has_ast_node.rb +12 -0
  116. data/lib/graphql/schema/member/has_deprecation_reason.rb +3 -4
  117. data/lib/graphql/schema/member/has_directives.rb +81 -61
  118. data/lib/graphql/schema/member/has_fields.rb +100 -38
  119. data/lib/graphql/schema/member/has_interfaces.rb +65 -10
  120. data/lib/graphql/schema/member/has_unresolved_type_error.rb +5 -1
  121. data/lib/graphql/schema/member/has_validators.rb +32 -6
  122. data/lib/graphql/schema/member/relay_shortcuts.rb +19 -0
  123. data/lib/graphql/schema/member/scoped.rb +19 -0
  124. data/lib/graphql/schema/member/type_system_helpers.rb +16 -0
  125. data/lib/graphql/schema/member/validates_input.rb +3 -3
  126. data/lib/graphql/schema/mutation.rb +7 -0
  127. data/lib/graphql/schema/object.rb +16 -5
  128. data/lib/graphql/schema/printer.rb +11 -8
  129. data/lib/graphql/schema/relay_classic_mutation.rb +7 -129
  130. data/lib/graphql/schema/resolver/has_payload_type.rb +9 -9
  131. data/lib/graphql/schema/resolver.rb +47 -32
  132. data/lib/graphql/schema/scalar.rb +3 -3
  133. data/lib/graphql/schema/subscription.rb +11 -4
  134. data/lib/graphql/schema/subset.rb +397 -0
  135. data/lib/graphql/schema/timeout.rb +25 -29
  136. data/lib/graphql/schema/type_expression.rb +2 -2
  137. data/lib/graphql/schema/type_membership.rb +3 -0
  138. data/lib/graphql/schema/union.rb +11 -2
  139. data/lib/graphql/schema/unique_within_type.rb +1 -1
  140. data/lib/graphql/schema/validator/all_validator.rb +60 -0
  141. data/lib/graphql/schema/validator.rb +4 -2
  142. data/lib/graphql/schema/warden.rb +238 -93
  143. data/lib/graphql/schema.rb +498 -103
  144. data/lib/graphql/static_validation/all_rules.rb +2 -1
  145. data/lib/graphql/static_validation/base_visitor.rb +7 -6
  146. data/lib/graphql/static_validation/definition_dependencies.rb +7 -1
  147. data/lib/graphql/static_validation/literal_validator.rb +24 -7
  148. data/lib/graphql/static_validation/rules/argument_literals_are_compatible.rb +1 -1
  149. data/lib/graphql/static_validation/rules/arguments_are_defined.rb +1 -1
  150. data/lib/graphql/static_validation/rules/directives_are_defined.rb +1 -2
  151. data/lib/graphql/static_validation/rules/fields_are_defined_on_type.rb +1 -1
  152. data/lib/graphql/static_validation/rules/fields_have_appropriate_selections.rb +12 -4
  153. data/lib/graphql/static_validation/rules/fields_will_merge.rb +10 -10
  154. data/lib/graphql/static_validation/rules/fragment_spreads_are_possible.rb +3 -3
  155. data/lib/graphql/static_validation/rules/fragment_types_exist.rb +1 -1
  156. data/lib/graphql/static_validation/rules/fragments_are_on_composite_types.rb +1 -1
  157. data/lib/graphql/static_validation/rules/mutation_root_exists.rb +1 -1
  158. data/lib/graphql/static_validation/rules/one_of_input_objects_are_valid.rb +66 -0
  159. data/lib/graphql/static_validation/rules/one_of_input_objects_are_valid_error.rb +29 -0
  160. data/lib/graphql/static_validation/rules/query_root_exists.rb +1 -1
  161. data/lib/graphql/static_validation/rules/required_arguments_are_present.rb +4 -4
  162. data/lib/graphql/static_validation/rules/required_input_object_attributes_are_present.rb +5 -5
  163. data/lib/graphql/static_validation/rules/subscription_root_exists.rb +1 -1
  164. data/lib/graphql/static_validation/rules/variable_default_values_are_correctly_typed.rb +18 -27
  165. data/lib/graphql/static_validation/rules/variable_usages_are_allowed.rb +1 -1
  166. data/lib/graphql/static_validation/rules/variables_are_input_types.rb +1 -1
  167. data/lib/graphql/static_validation/validation_context.rb +5 -5
  168. data/lib/graphql/static_validation/validator.rb +4 -1
  169. data/lib/graphql/static_validation.rb +0 -1
  170. data/lib/graphql/subscriptions/action_cable_subscriptions.rb +11 -4
  171. data/lib/graphql/subscriptions/broadcast_analyzer.rb +11 -5
  172. data/lib/graphql/subscriptions/event.rb +11 -10
  173. data/lib/graphql/subscriptions/serialize.rb +2 -0
  174. data/lib/graphql/subscriptions.rb +20 -13
  175. data/lib/graphql/testing/helpers.rb +151 -0
  176. data/lib/graphql/testing.rb +2 -0
  177. data/lib/graphql/tracing/active_support_notifications_trace.rb +16 -0
  178. data/lib/graphql/tracing/appoptics_trace.rb +251 -0
  179. data/lib/graphql/tracing/appoptics_tracing.rb +2 -2
  180. data/lib/graphql/tracing/appsignal_trace.rb +77 -0
  181. data/lib/graphql/tracing/data_dog_trace.rb +183 -0
  182. data/lib/graphql/tracing/data_dog_tracing.rb +9 -21
  183. data/lib/graphql/{execution/instrumentation.rb → tracing/legacy_hooks_trace.rb} +10 -28
  184. data/lib/graphql/tracing/legacy_trace.rb +69 -0
  185. data/lib/graphql/tracing/new_relic_trace.rb +75 -0
  186. data/lib/graphql/tracing/notifications_trace.rb +45 -0
  187. data/lib/graphql/tracing/platform_trace.rb +118 -0
  188. data/lib/graphql/tracing/platform_tracing.rb +17 -3
  189. data/lib/graphql/tracing/{prometheus_tracing → prometheus_trace}/graphql_collector.rb +4 -2
  190. data/lib/graphql/tracing/prometheus_trace.rb +89 -0
  191. data/lib/graphql/tracing/prometheus_tracing.rb +3 -3
  192. data/lib/graphql/tracing/scout_trace.rb +72 -0
  193. data/lib/graphql/tracing/sentry_trace.rb +112 -0
  194. data/lib/graphql/tracing/statsd_trace.rb +56 -0
  195. data/lib/graphql/tracing/trace.rb +76 -0
  196. data/lib/graphql/tracing.rb +20 -40
  197. data/lib/graphql/type_kinds.rb +7 -4
  198. data/lib/graphql/types/iso_8601_duration.rb +77 -0
  199. data/lib/graphql/types/relay/base_connection.rb +1 -1
  200. data/lib/graphql/types/relay/connection_behaviors.rb +68 -6
  201. data/lib/graphql/types/relay/edge_behaviors.rb +33 -5
  202. data/lib/graphql/types/relay/node_behaviors.rb +8 -2
  203. data/lib/graphql/types/relay/page_info_behaviors.rb +11 -2
  204. data/lib/graphql/types/relay.rb +0 -1
  205. data/lib/graphql/types/string.rb +1 -1
  206. data/lib/graphql/types.rb +1 -0
  207. data/lib/graphql/version.rb +1 -1
  208. data/lib/graphql.rb +27 -20
  209. data/readme.md +13 -3
  210. metadata +96 -47
  211. data/lib/graphql/analysis/ast/analyzer.rb +0 -84
  212. data/lib/graphql/analysis/ast/field_usage.rb +0 -57
  213. data/lib/graphql/analysis/ast/max_query_complexity.rb +0 -22
  214. data/lib/graphql/analysis/ast/max_query_depth.rb +0 -22
  215. data/lib/graphql/analysis/ast/query_complexity.rb +0 -230
  216. data/lib/graphql/analysis/ast/query_depth.rb +0 -55
  217. data/lib/graphql/analysis/ast/visitor.rb +0 -269
  218. data/lib/graphql/analysis/ast.rb +0 -81
  219. data/lib/graphql/deprecation.rb +0 -9
  220. data/lib/graphql/filter.rb +0 -53
  221. data/lib/graphql/language/lexer.rl +0 -280
  222. data/lib/graphql/language/parser.y +0 -554
  223. data/lib/graphql/language/token.rb +0 -34
  224. data/lib/graphql/schema/base_64_bp.rb +0 -26
  225. data/lib/graphql/schema/invalid_type_error.rb +0 -7
  226. data/lib/graphql/static_validation/type_stack.rb +0 -216
  227. data/lib/graphql/subscriptions/instrumentation.rb +0 -28
  228. data/lib/graphql/types/relay/default_relay.rb +0 -21
@@ -2,6 +2,7 @@
2
2
  module GraphQL
3
3
  module Language
4
4
  module Nodes
5
+ NONE = GraphQL::EmptyObjects::EMPTY_ARRAY
5
6
  # {AbstractNode} is the base class for all nodes in a GraphQL AST.
6
7
  #
7
8
  # It provides some APIs for working with ASTs:
@@ -9,32 +10,39 @@ module GraphQL
9
10
  # - `scalars` returns all scalar (Ruby) values attached to this one. Used for comparing nodes.
10
11
  # - `to_query_string` turns an AST node into a GraphQL string
11
12
  class AbstractNode
13
+
12
14
  module DefinitionNode
13
15
  # This AST node's {#line} returns the first line, which may be the description.
14
16
  # @return [Integer] The first line of the definition (not the description)
15
17
  attr_reader :definition_line
16
18
 
17
- def initialize(options = {})
18
- @definition_line = options.delete(:definition_line)
19
- super(options)
19
+ def initialize(definition_line: nil, **_rest)
20
+ @definition_line = definition_line
21
+ super(**_rest)
20
22
  end
21
- end
22
23
 
23
- attr_reader :line, :col, :filename
24
+ def marshal_dump
25
+ super << @definition_line
26
+ end
24
27
 
25
- # Initialize a node by extracting its position,
26
- # then calling the class's `initialize_node` method.
27
- # @param options [Hash] Initial attributes for this node
28
- def initialize(options = {})
29
- if options.key?(:position_source)
30
- position_source = options.delete(:position_source)
31
- @line = position_source.line
32
- @col = position_source.col
28
+ def marshal_load(values)
29
+ @definition_line = values.pop
30
+ super
33
31
  end
32
+ end
33
+
34
+ attr_reader :filename
34
35
 
35
- @filename = options.delete(:filename)
36
+ def line
37
+ @line ||= @source.line_at(@pos)
38
+ end
36
39
 
37
- initialize_node(**options)
40
+ def col
41
+ @col ||= @source.column_at(@pos)
42
+ end
43
+
44
+ def definition_line
45
+ @definition_line ||= (@source && @definition_pos) ? @source.line_at(@definition_pos) : nil
38
46
  end
39
47
 
40
48
  # Value equality
@@ -46,7 +54,7 @@ module GraphQL
46
54
  other.children == self.children
47
55
  end
48
56
 
49
- NO_CHILDREN = [].freeze
57
+ NO_CHILDREN = GraphQL::EmptyObjects::EMPTY_ARRAY
50
58
 
51
59
  # @return [Array<GraphQL::Language::Nodes::AbstractNode>] all nodes in the tree below this one
52
60
  def children
@@ -141,18 +149,26 @@ module GraphQL
141
149
  .gsub(/([a-z])([A-Z])/,'\1_\2') # insert underscores
142
150
  .downcase # remove caps
143
151
 
144
- child_class.module_eval <<-RUBY
152
+ child_class.module_eval <<-RUBY, __FILE__, __LINE__
145
153
  def visit_method
146
154
  :on_#{name_underscored}
147
155
  end
148
156
 
149
157
  class << self
150
158
  attr_accessor :children_method_name
159
+
160
+ def visit_method
161
+ :on_#{name_underscored}
162
+ end
151
163
  end
152
164
  self.children_method_name = :#{name_underscored}s
153
165
  RUBY
154
166
  end
155
167
 
168
+ def children_of_type
169
+ @children_methods
170
+ end
171
+
156
172
  private
157
173
 
158
174
  # Name accessors which return lists of nodes,
@@ -183,8 +199,8 @@ module GraphQL
183
199
  module_eval <<-RUBY, __FILE__, __LINE__
184
200
  # Singular method: create a node with these options
185
201
  # and return a new `self` which includes that node in this list.
186
- def merge_#{method_name.to_s.sub(/s$/, "")}(node_opts)
187
- merge(#{method_name}: #{method_name} + [#{node_type.name}.new(node_opts)])
202
+ def merge_#{method_name.to_s.sub(/s$/, "")}(**node_opts)
203
+ merge(#{method_name}: #{method_name} + [#{node_type.name}.new(**node_opts)])
188
204
  end
189
205
  RUBY
190
206
  end
@@ -213,13 +229,14 @@ module GraphQL
213
229
  end
214
230
 
215
231
  if defined?(@scalar_methods)
216
- if !method_defined?(:initialize_node)
217
- generate_initialize_node
232
+ if !@initialize_was_generated
233
+ @initialize_was_generated = true
234
+ generate_initialize
218
235
  else
219
236
  # This method was defined manually
220
237
  end
221
238
  else
222
- raise "Can't generate_initialize_node because scalar_methods wasn't called; call it before children_methods"
239
+ raise "Can't generate_initialize because scalar_methods wasn't called; call it before children_methods"
223
240
  end
224
241
  end
225
242
 
@@ -248,7 +265,17 @@ module GraphQL
248
265
  end
249
266
  end
250
267
 
251
- def generate_initialize_node
268
+ DEFAULT_INITIALIZE_OPTIONS = [
269
+ "line: nil",
270
+ "col: nil",
271
+ "pos: nil",
272
+ "filename: nil",
273
+ "source: nil",
274
+ ]
275
+
276
+ def generate_initialize
277
+ return if method_defined?(:marshal_load, false) # checking for `:initialize` doesn't work right
278
+
252
279
  scalar_method_names = @scalar_methods
253
280
  # TODO: These probably should be scalar methods, but `types` returns an array
254
281
  [:types, :description].each do |extra_method|
@@ -257,22 +284,54 @@ module GraphQL
257
284
  end
258
285
  end
259
286
 
260
- all_method_names = scalar_method_names + @children_methods.keys
287
+ children_method_names = @children_methods.keys
288
+
289
+ all_method_names = scalar_method_names + children_method_names
261
290
  if all_method_names.include?(:alias)
262
291
  # Rather than complicating this special case,
263
292
  # let it be overridden (in field)
264
293
  return
265
294
  else
266
295
  arguments = scalar_method_names.map { |m| "#{m}: nil"} +
267
- @children_methods.keys.map { |m| "#{m}: NO_CHILDREN" }
296
+ children_method_names.map { |m| "#{m}: NO_CHILDREN" } +
297
+ DEFAULT_INITIALIZE_OPTIONS
268
298
 
269
299
  assignments = scalar_method_names.map { |m| "@#{m} = #{m}"} +
270
- @children_methods.keys.map { |m| "@#{m} = #{m}.freeze" }
300
+ children_method_names.map { |m| "@#{m} = #{m}.freeze" }
301
+
302
+ if name.end_with?("Definition") && name != "FragmentDefinition"
303
+ arguments << "definition_pos: nil"
304
+ assignments << "@definition_pos = definition_pos"
305
+ end
306
+
307
+ keywords = scalar_method_names.map { |m| "#{m}: #{m}"} +
308
+ children_method_names.map { |m| "#{m}: #{m}" }
271
309
 
272
310
  module_eval <<-RUBY, __FILE__, __LINE__
273
- def initialize_node #{arguments.join(", ")}
311
+ def initialize(#{arguments.join(", ")})
312
+ @line = line
313
+ @col = col
314
+ @pos = pos
315
+ @filename = filename
316
+ @source = source
274
317
  #{assignments.join("\n")}
275
318
  end
319
+
320
+ def self.from_a(filename, line, col, #{all_method_names.join(", ")})
321
+ self.new(filename: filename, line: line, col: col, #{keywords.join(", ")})
322
+ end
323
+
324
+ def marshal_dump
325
+ [
326
+ line, col, # use methods here to force them to be calculated
327
+ @filename,
328
+ #{all_method_names.map { |n| "@#{n}," }.join}
329
+ ]
330
+ end
331
+
332
+ def marshal_load(values)
333
+ @line, @col, @filename #{all_method_names.map { |n| ", @#{n}"}.join} = values
334
+ end
276
335
  RUBY
277
336
  end
278
337
  end
@@ -300,10 +359,10 @@ module GraphQL
300
359
  # @return [String] the key for this argument
301
360
 
302
361
  # @!attribute value
303
- # @return [String, Float, Integer, Boolean, Array, InputObject] The value passed for this key
362
+ # @return [String, Float, Integer, Boolean, Array, InputObject, VariableIdentifier] The value passed for this key
304
363
 
305
364
  def children
306
- @children ||= Array(value).flatten.select { |v| v.is_a?(AbstractNode) }
365
+ @children ||= Array(value).flatten.tap { _1.select! { |v| v.is_a?(AbstractNode) } }
307
366
  end
308
367
  end
309
368
 
@@ -316,42 +375,13 @@ module GraphQL
316
375
  end
317
376
 
318
377
  class DirectiveDefinition < AbstractNode
319
- include DefinitionNode
320
378
  attr_reader :description
321
379
  scalar_methods :name, :repeatable
322
380
  children_methods(
323
- locations: Nodes::DirectiveLocation,
324
381
  arguments: Nodes::Argument,
382
+ locations: Nodes::DirectiveLocation,
325
383
  )
326
- end
327
-
328
- # This is the AST root for normal queries
329
- #
330
- # @example Deriving a document by parsing a string
331
- # document = GraphQL.parse(query_string)
332
- #
333
- # @example Creating a string from a document
334
- # document.to_query_string
335
- # # { ... }
336
- #
337
- # @example Creating a custom string from a document
338
- # class VariableScrubber < GraphQL::Language::Printer
339
- # def print_argument(arg)
340
- # "#{arg.name}: <HIDDEN>"
341
- # end
342
- # end
343
- #
344
- # document.to_query_string(printer: VariableScrubber.new)
345
- #
346
- class Document < AbstractNode
347
- scalar_methods false
348
- children_methods(definitions: nil)
349
- # @!attribute definitions
350
- # @return [Array<OperationDefinition, FragmentDefinition>] top-level GraphQL units: operations or fragments
351
-
352
- def slice_definition(name)
353
- GraphQL::Language::DefinitionSlice.slice(self, name)
354
- end
384
+ self.children_method_name = :definitions
355
385
  end
356
386
 
357
387
  # An enum value. The string is available as {#name}.
@@ -364,7 +394,31 @@ module GraphQL
364
394
 
365
395
  # A single selection in a GraphQL query.
366
396
  class Field < AbstractNode
367
- NONE = [].freeze
397
+ def initialize(name: nil, arguments: NONE, directives: NONE, selections: NONE, field_alias: nil, line: nil, col: nil, pos: nil, filename: nil, source: nil)
398
+ @name = name
399
+ @arguments = arguments || NONE
400
+ @directives = directives || NONE
401
+ @selections = selections || NONE
402
+ # oops, alias is a keyword:
403
+ @alias = field_alias
404
+ @line = line
405
+ @col = col
406
+ @pos = pos
407
+ @filename = filename
408
+ @source = source
409
+ end
410
+
411
+ def self.from_a(filename, line, col, field_alias, name, arguments, directives, selections) # rubocop:disable Metrics/ParameterLists
412
+ self.new(filename: filename, line: line, col: col, field_alias: field_alias, name: name, arguments: arguments, directives: directives, selections: selections)
413
+ end
414
+
415
+ def marshal_dump
416
+ [line, col, @filename, @name, @arguments, @directives, @selections, @alias]
417
+ end
418
+
419
+ def marshal_load(values)
420
+ @line, @col, @filename, @name, @arguments, @directives, @selections, @alias = values
421
+ end
368
422
 
369
423
  scalar_methods :name, :alias
370
424
  children_methods({
@@ -373,34 +427,34 @@ module GraphQL
373
427
  directives: GraphQL::Language::Nodes::Directive,
374
428
  })
375
429
 
376
- # @!attribute selections
377
- # @return [Array<Nodes::Field>] Selections on this object (or empty array if this is a scalar field)
378
-
379
- def initialize_node(attributes)
380
- @name = attributes[:name]
381
- @arguments = attributes[:arguments] || NONE
382
- @directives = attributes[:directives] || NONE
383
- @selections = attributes[:selections] || NONE
384
- # oops, alias is a keyword:
385
- @alias = attributes[:alias]
386
- end
387
-
388
430
  # Override this because default is `:fields`
389
431
  self.children_method_name = :selections
390
432
  end
391
433
 
392
434
  # A reusable fragment, defined at document-level.
393
435
  class FragmentDefinition < AbstractNode
394
- # @!attribute name
395
- # @return [String] the identifier for this fragment, which may be applied with `...#{name}`
396
-
397
- # @!attribute type
398
- # @return [String] the type condition for this fragment (name of type which it may apply to)
399
- def initialize_node(name: nil, type: nil, directives: [], selections: [])
436
+ def initialize(name: nil, type: nil, directives: NONE, selections: NONE, filename: nil, pos: nil, source: nil, line: nil, col: nil)
400
437
  @name = name
401
438
  @type = type
402
439
  @directives = directives
403
440
  @selections = selections
441
+ @filename = filename
442
+ @pos = pos
443
+ @source = source
444
+ @line = line
445
+ @col = col
446
+ end
447
+
448
+ def self.from_a(filename, line, col, name, type, directives, selections)
449
+ self.new(filename: filename, line: line, col: col, name: name, type: type, directives: directives, selections: selections)
450
+ end
451
+
452
+ def marshal_dump
453
+ [line, col, @filename, @name, @type, @directives, @selections]
454
+ end
455
+
456
+ def marshal_load(values)
457
+ @line, @col, @filename, @name, @type, @directives, @selections = values
404
458
  end
405
459
 
406
460
  scalar_methods :name, :type
@@ -427,8 +481,8 @@ module GraphQL
427
481
  class InlineFragment < AbstractNode
428
482
  scalar_methods :type
429
483
  children_methods({
430
- selections: GraphQL::Language::Nodes::Field,
431
484
  directives: GraphQL::Language::Nodes::Directive,
485
+ selections: GraphQL::Language::Nodes::Field,
432
486
  })
433
487
 
434
488
  self.children_method_name = :selections
@@ -487,7 +541,7 @@ module GraphQL
487
541
  # An operation-level query variable
488
542
  class VariableDefinition < AbstractNode
489
543
  scalar_methods :name, :type, :default_value
490
- children_methods false
544
+ children_methods(directives: Directive)
491
545
  # @!attribute default_value
492
546
  # @return [String, Integer, Float, Boolean, Array, NullValue] A Ruby value to use if no other value is provided
493
547
 
@@ -507,8 +561,8 @@ module GraphQL
507
561
  scalar_methods :operation_type, :name
508
562
  children_methods({
509
563
  variables: GraphQL::Language::Nodes::VariableDefinition,
510
- selections: GraphQL::Language::Nodes::Field,
511
564
  directives: GraphQL::Language::Nodes::Directive,
565
+ selections: GraphQL::Language::Nodes::Field,
512
566
  })
513
567
 
514
568
  # @!attribute variables
@@ -526,6 +580,35 @@ module GraphQL
526
580
  self.children_method_name = :definitions
527
581
  end
528
582
 
583
+ # This is the AST root for normal queries
584
+ #
585
+ # @example Deriving a document by parsing a string
586
+ # document = GraphQL.parse(query_string)
587
+ #
588
+ # @example Creating a string from a document
589
+ # document.to_query_string
590
+ # # { ... }
591
+ #
592
+ # @example Creating a custom string from a document
593
+ # class VariableScrubber < GraphQL::Language::Printer
594
+ # def print_argument(arg)
595
+ # print_string("#{arg.name}: <HIDDEN>")
596
+ # end
597
+ # end
598
+ #
599
+ # document.to_query_string(printer: VariableScrubber.new)
600
+ #
601
+ class Document < AbstractNode
602
+ scalar_methods false
603
+ children_methods(definitions: nil)
604
+ # @!attribute definitions
605
+ # @return [Array<OperationDefinition, FragmentDefinition>] top-level GraphQL units: operations or fragments
606
+
607
+ def slice_definition(name)
608
+ GraphQL::Language::DefinitionSlice.slice(self, name)
609
+ end
610
+ end
611
+
529
612
  # A type name, used for variable definitions
530
613
  class TypeName < NameOnlyNode
531
614
  end
@@ -536,7 +619,6 @@ module GraphQL
536
619
  end
537
620
 
538
621
  class SchemaDefinition < AbstractNode
539
- include DefinitionNode
540
622
  scalar_methods :query, :mutation, :subscription
541
623
  children_methods({
542
624
  directives: GraphQL::Language::Nodes::Directive,
@@ -553,7 +635,6 @@ module GraphQL
553
635
  end
554
636
 
555
637
  class ScalarTypeDefinition < AbstractNode
556
- include DefinitionNode
557
638
  attr_reader :description
558
639
  scalar_methods :name
559
640
  children_methods({
@@ -571,7 +652,6 @@ module GraphQL
571
652
  end
572
653
 
573
654
  class InputValueDefinition < AbstractNode
574
- include DefinitionNode
575
655
  attr_reader :description
576
656
  scalar_methods :name, :type, :default_value
577
657
  children_methods({
@@ -581,12 +661,11 @@ module GraphQL
581
661
  end
582
662
 
583
663
  class FieldDefinition < AbstractNode
584
- include DefinitionNode
585
664
  attr_reader :description
586
665
  scalar_methods :name, :type
587
666
  children_methods({
588
- directives: GraphQL::Language::Nodes::Directive,
589
667
  arguments: GraphQL::Language::Nodes::InputValueDefinition,
668
+ directives: GraphQL::Language::Nodes::Directive,
590
669
  })
591
670
  self.children_method_name = :fields
592
671
 
@@ -602,7 +681,6 @@ module GraphQL
602
681
  end
603
682
 
604
683
  class ObjectTypeDefinition < AbstractNode
605
- include DefinitionNode
606
684
  attr_reader :description
607
685
  scalar_methods :name, :interfaces
608
686
  children_methods({
@@ -622,7 +700,6 @@ module GraphQL
622
700
  end
623
701
 
624
702
  class InterfaceTypeDefinition < AbstractNode
625
- include DefinitionNode
626
703
  attr_reader :description
627
704
  scalar_methods :name
628
705
  children_methods({
@@ -644,7 +721,6 @@ module GraphQL
644
721
  end
645
722
 
646
723
  class UnionTypeDefinition < AbstractNode
647
- include DefinitionNode
648
724
  attr_reader :description, :types
649
725
  scalar_methods :name
650
726
  children_methods({
@@ -663,7 +739,6 @@ module GraphQL
663
739
  end
664
740
 
665
741
  class EnumValueDefinition < AbstractNode
666
- include DefinitionNode
667
742
  attr_reader :description
668
743
  scalar_methods :name
669
744
  children_methods({
@@ -673,7 +748,6 @@ module GraphQL
673
748
  end
674
749
 
675
750
  class EnumTypeDefinition < AbstractNode
676
- include DefinitionNode
677
751
  attr_reader :description
678
752
  scalar_methods :name
679
753
  children_methods({
@@ -693,7 +767,6 @@ module GraphQL
693
767
  end
694
768
 
695
769
  class InputObjectTypeDefinition < AbstractNode
696
- include DefinitionNode
697
770
  attr_reader :description
698
771
  scalar_methods :name
699
772
  children_methods({