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
@@ -40,7 +40,7 @@ module GraphQL
40
40
  case node
41
41
  when FalseClass, Float, Integer, String, TrueClass
42
42
  if @current_argument && redact_argument_value?(@current_argument, node)
43
- redacted_argument_value(@current_argument)
43
+ print_string(redacted_argument_value(@current_argument))
44
44
  else
45
45
  super
46
46
  end
@@ -51,9 +51,8 @@ module GraphQL
51
51
  @current_input_type = @current_input_type.of_type if @current_input_type.non_null?
52
52
  end
53
53
 
54
- res = super
54
+ super
55
55
  @current_input_type = old_input_type
56
- res
57
56
  else
58
57
  super
59
58
  end
@@ -89,11 +88,12 @@ module GraphQL
89
88
  else
90
89
  argument.value
91
90
  end
92
- res = "#{argument.name}: #{print_node(argument_value)}".dup
91
+
92
+ print_string("#{argument.name}: ")
93
+ print_node(argument_value)
93
94
 
94
95
  @current_input_type = old_input_type
95
96
  @current_argument = old_current_argument
96
- res
97
97
  end
98
98
 
99
99
  def coerce_argument_value_to_list?(type, value)
@@ -113,12 +113,11 @@ module GraphQL
113
113
  end
114
114
 
115
115
  def print_field(field, indent: "")
116
- @current_field = query.get_field(@current_type, field.name)
116
+ @current_field = query.types.field(@current_type, field.name)
117
117
  old_type = @current_type
118
118
  @current_type = @current_field.type.unwrap
119
- res = super
119
+ super
120
120
  @current_type = old_type
121
- res
122
121
  end
123
122
 
124
123
  def print_inline_fragment(inline_fragment, indent: "")
@@ -128,31 +127,26 @@ module GraphQL
128
127
  @current_type = query.get_type(inline_fragment.type.name)
129
128
  end
130
129
 
131
- res = super
130
+ super
132
131
 
133
132
  @current_type = old_type
134
-
135
- res
136
133
  end
137
134
 
138
135
  def print_fragment_definition(fragment_def, indent: "")
139
136
  old_type = @current_type
140
137
  @current_type = query.get_type(fragment_def.type.name)
141
138
 
142
- res = super
139
+ super
143
140
 
144
141
  @current_type = old_type
145
-
146
- res
147
142
  end
148
143
 
149
144
  def print_directive(directive)
150
145
  @current_directive = query.schema.directives[directive.name]
151
146
 
152
- res = super
147
+ super
153
148
 
154
149
  @current_directive = nil
155
- res
156
150
  end
157
151
 
158
152
  # Print the operation definition but do not include the variable
@@ -162,16 +156,15 @@ module GraphQL
162
156
  @current_type = query.schema.public_send(operation_definition.operation_type)
163
157
 
164
158
  if @inline_variables
165
- out = "#{indent}#{operation_definition.operation_type}".dup
166
- out << " #{operation_definition.name}" if operation_definition.name
167
- out << print_directives(operation_definition.directives)
168
- out << print_selections(operation_definition.selections, indent: indent)
159
+ print_string("#{indent}#{operation_definition.operation_type}")
160
+ print_string(" #{operation_definition.name}") if operation_definition.name
161
+ print_directives(operation_definition.directives)
162
+ print_selections(operation_definition.selections, indent: indent)
169
163
  else
170
- out = super
164
+ super
171
165
  end
172
166
 
173
167
  @current_type = old_type
174
- out
175
168
  end
176
169
 
177
170
  private
@@ -210,7 +203,12 @@ module GraphQL
210
203
  [value].map { |v| value_to_ast(v, type.of_type) }
211
204
  end
212
205
  when "ENUM"
213
- GraphQL::Language::Nodes::Enum.new(name: value)
206
+ if value.is_a?(GraphQL::Language::Nodes::Enum)
207
+ # if it was a default value, it's already wrapped
208
+ value
209
+ else
210
+ GraphQL::Language::Nodes::Enum.new(name: value)
211
+ end
214
212
  else
215
213
  value
216
214
  end
@@ -0,0 +1,167 @@
1
+ # frozen_string_literal: true
2
+ module GraphQL
3
+ module Language
4
+ # Like `GraphQL::Language::Visitor` except it doesn't support
5
+ # making changes to the document -- only visiting it as-is.
6
+ class StaticVisitor
7
+ def initialize(document)
8
+ @document = document
9
+ end
10
+
11
+ # Visit `document` and all children
12
+ # @return [void]
13
+ def visit
14
+ # `@document` may be any kind of node:
15
+ visit_method = @document.visit_method
16
+ result = public_send(visit_method, @document, nil)
17
+ @result = if result.is_a?(Array)
18
+ result.first
19
+ else
20
+ # The node wasn't modified
21
+ @document
22
+ end
23
+ end
24
+
25
+ # We don't use `alias` here because it breaks `super`
26
+ def self.make_visit_methods(ast_node_class)
27
+ node_method = ast_node_class.visit_method
28
+ children_of_type = ast_node_class.children_of_type
29
+ child_visit_method = :"#{node_method}_children"
30
+
31
+ class_eval(<<-RUBY, __FILE__, __LINE__ + 1)
32
+ # The default implementation for visiting an AST node.
33
+ # It doesn't _do_ anything, but it continues to visiting the node's children.
34
+ # To customize this hook, override one of its make_visit_methods (or the base method?)
35
+ # in your subclasses.
36
+ #
37
+ # @param node [GraphQL::Language::Nodes::AbstractNode] the node being visited
38
+ # @param parent [GraphQL::Language::Nodes::AbstractNode, nil] the previously-visited node, or `nil` if this is the root node.
39
+ # @return [void]
40
+ def #{node_method}(node, parent)
41
+ #{
42
+ if method_defined?(child_visit_method)
43
+ "#{child_visit_method}(node)"
44
+ elsif children_of_type
45
+ children_of_type.map do |child_accessor, child_class|
46
+ "node.#{child_accessor}.each do |child_node|
47
+ #{child_class.visit_method}(child_node, node)
48
+ end"
49
+ end.join("\n")
50
+ else
51
+ ""
52
+ end
53
+ }
54
+ end
55
+ RUBY
56
+ end
57
+
58
+ def on_document_children(document_node)
59
+ document_node.children.each do |child_node|
60
+ visit_method = child_node.visit_method
61
+ public_send(visit_method, child_node, document_node)
62
+ end
63
+ end
64
+
65
+ def on_field_children(new_node)
66
+ new_node.arguments.each do |arg_node| # rubocop:disable Development/ContextIsPassedCop
67
+ on_argument(arg_node, new_node)
68
+ end
69
+ visit_directives(new_node)
70
+ visit_selections(new_node)
71
+ end
72
+
73
+ def visit_directives(new_node)
74
+ new_node.directives.each do |dir_node|
75
+ on_directive(dir_node, new_node)
76
+ end
77
+ end
78
+
79
+ def visit_selections(new_node)
80
+ new_node.selections.each do |selection|
81
+ case selection
82
+ when GraphQL::Language::Nodes::Field
83
+ on_field(selection, new_node)
84
+ when GraphQL::Language::Nodes::InlineFragment
85
+ on_inline_fragment(selection, new_node)
86
+ when GraphQL::Language::Nodes::FragmentSpread
87
+ on_fragment_spread(selection, new_node)
88
+ else
89
+ raise ArgumentError, "Invariant: unexpected field selection #{selection.class} (#{selection.inspect})"
90
+ end
91
+ end
92
+ end
93
+
94
+ def on_fragment_definition_children(new_node)
95
+ visit_directives(new_node)
96
+ visit_selections(new_node)
97
+ end
98
+
99
+ alias :on_inline_fragment_children :on_fragment_definition_children
100
+
101
+ def on_operation_definition_children(new_node)
102
+ new_node.variables.each do |arg_node|
103
+ on_variable_definition(arg_node, new_node)
104
+ end
105
+ visit_directives(new_node)
106
+ visit_selections(new_node)
107
+ end
108
+
109
+ def on_argument_children(new_node)
110
+ new_node.children.each do |value_node|
111
+ case value_node
112
+ when Language::Nodes::VariableIdentifier
113
+ on_variable_identifier(value_node, new_node)
114
+ when Language::Nodes::InputObject
115
+ on_input_object(value_node, new_node)
116
+ when Language::Nodes::Enum
117
+ on_enum(value_node, new_node)
118
+ when Language::Nodes::NullValue
119
+ on_null_value(value_node, new_node)
120
+ else
121
+ raise ArgumentError, "Invariant: unexpected argument value node #{value_node.class} (#{value_node.inspect})"
122
+ end
123
+ end
124
+ end
125
+
126
+ [
127
+ Language::Nodes::Argument,
128
+ Language::Nodes::Directive,
129
+ Language::Nodes::DirectiveDefinition,
130
+ Language::Nodes::DirectiveLocation,
131
+ Language::Nodes::Document,
132
+ Language::Nodes::Enum,
133
+ Language::Nodes::EnumTypeDefinition,
134
+ Language::Nodes::EnumTypeExtension,
135
+ Language::Nodes::EnumValueDefinition,
136
+ Language::Nodes::Field,
137
+ Language::Nodes::FieldDefinition,
138
+ Language::Nodes::FragmentDefinition,
139
+ Language::Nodes::FragmentSpread,
140
+ Language::Nodes::InlineFragment,
141
+ Language::Nodes::InputObject,
142
+ Language::Nodes::InputObjectTypeDefinition,
143
+ Language::Nodes::InputObjectTypeExtension,
144
+ Language::Nodes::InputValueDefinition,
145
+ Language::Nodes::InterfaceTypeDefinition,
146
+ Language::Nodes::InterfaceTypeExtension,
147
+ Language::Nodes::ListType,
148
+ Language::Nodes::NonNullType,
149
+ Language::Nodes::NullValue,
150
+ Language::Nodes::ObjectTypeDefinition,
151
+ Language::Nodes::ObjectTypeExtension,
152
+ Language::Nodes::OperationDefinition,
153
+ Language::Nodes::ScalarTypeDefinition,
154
+ Language::Nodes::ScalarTypeExtension,
155
+ Language::Nodes::SchemaDefinition,
156
+ Language::Nodes::SchemaExtension,
157
+ Language::Nodes::TypeName,
158
+ Language::Nodes::UnionTypeDefinition,
159
+ Language::Nodes::UnionTypeExtension,
160
+ Language::Nodes::VariableDefinition,
161
+ Language::Nodes::VariableIdentifier,
162
+ ].each do |ast_node_class|
163
+ make_visit_methods(ast_node_class)
164
+ end
165
+ end
166
+ end
167
+ end
@@ -30,12 +30,9 @@ module GraphQL
30
30
  # # Check the result
31
31
  # visitor.count
32
32
  # # => 3
33
+ #
34
+ # @see GraphQL::Language::StaticVisitor for a faster visitor that doesn't support modifying the document
33
35
  class Visitor
34
- # If any hook returns this value, the {Visitor} stops visiting this
35
- # node right away
36
- # @deprecated Use `super` to continue the visit; or don't call it to halt.
37
- SKIP = :_skip
38
-
39
36
  class DeleteNode; end
40
37
 
41
38
  # When this is returned from a visitor method,
@@ -44,28 +41,18 @@ module GraphQL
44
41
 
45
42
  def initialize(document)
46
43
  @document = document
47
- @visitors = {}
48
44
  @result = nil
49
45
  end
50
46
 
51
47
  # @return [GraphQL::Language::Nodes::Document] The document with any modifications applied
52
48
  attr_reader :result
53
49
 
54
- # Get a {NodeVisitor} for `node_class`
55
- # @param node_class [Class] The node class that you want to listen to
56
- # @return [NodeVisitor]
57
- #
58
- # @example Run a hook whenever you enter a new Field
59
- # visitor[GraphQL::Language::Nodes::Field] << ->(node, parent) { p "Here's a field" }
60
- # @deprecated see `on_` methods, like {#on_field}
61
- def [](node_class)
62
- @visitors[node_class] ||= NodeVisitor.new
63
- end
64
-
65
- # Visit `document` and all children, applying hooks as you go
50
+ # Visit `document` and all children
66
51
  # @return [void]
67
52
  def visit
68
- result = on_node_with_modifications(@document, nil)
53
+ # `@document` may be any kind of node:
54
+ visit_method = :"#{@document.visit_method}_with_modifications"
55
+ result = public_send(visit_method, @document, nil)
69
56
  @result = if result.is_a?(Array)
70
57
  result.first
71
58
  else
@@ -74,104 +61,204 @@ module GraphQL
74
61
  end
75
62
  end
76
63
 
77
- # Call the user-defined handler for `node`.
78
- def visit_node(node, parent)
79
- public_send(node.visit_method, node, parent)
80
- end
64
+ # We don't use `alias` here because it breaks `super`
65
+ def self.make_visit_methods(ast_node_class)
66
+ node_method = ast_node_class.visit_method
67
+ children_of_type = ast_node_class.children_of_type
68
+ child_visit_method = :"#{node_method}_children"
81
69
 
82
- # The default implementation for visiting an AST node.
83
- # It doesn't _do_ anything, but it continues to visiting the node's children.
84
- # To customize this hook, override one of its make_visit_methodes (or the base method?)
85
- # in your subclasses.
86
- #
87
- # For compatibility, it calls hook procs, too.
88
- # @param node [GraphQL::Language::Nodes::AbstractNode] the node being visited
89
- # @param parent [GraphQL::Language::Nodes::AbstractNode, nil] the previously-visited node, or `nil` if this is the root node.
90
- # @return [Array, nil] If there were modifications, it returns an array of new nodes, otherwise, it returns `nil`.
91
- def on_abstract_node(node, parent)
92
- if node.equal?(DELETE_NODE)
93
- # This might be passed to `super(DELETE_NODE, ...)`
94
- # by a user hook, don't want to keep visiting in that case.
95
- nil
96
- else
97
- # Run hooks if there are any
98
- new_node = node
99
- no_hooks = !@visitors.key?(node.class)
100
- if no_hooks || begin_visit(new_node, parent)
101
- node.children.each do |child_node|
102
- new_child_and_node = on_node_with_modifications(child_node, new_node)
103
- # Reassign `node` in case the child hook makes a modification
104
- if new_child_and_node.is_a?(Array)
105
- new_node = new_child_and_node[1]
70
+ class_eval(<<-RUBY, __FILE__, __LINE__ + 1)
71
+ # The default implementation for visiting an AST node.
72
+ # It doesn't _do_ anything, but it continues to visiting the node's children.
73
+ # To customize this hook, override one of its make_visit_methods (or the base method?)
74
+ # in your subclasses.
75
+ #
76
+ # @param node [GraphQL::Language::Nodes::AbstractNode] the node being visited
77
+ # @param parent [GraphQL::Language::Nodes::AbstractNode, nil] the previously-visited node, or `nil` if this is the root node.
78
+ # @return [Array, nil] If there were modifications, it returns an array of new nodes, otherwise, it returns `nil`.
79
+ def #{node_method}(node, parent)
80
+ if node.equal?(DELETE_NODE)
81
+ # This might be passed to `super(DELETE_NODE, ...)`
82
+ # by a user hook, don't want to keep visiting in that case.
83
+ [node, parent]
84
+ else
85
+ new_node = node
86
+ #{
87
+ if method_defined?(child_visit_method)
88
+ "new_node = #{child_visit_method}(new_node)"
89
+ elsif children_of_type
90
+ children_of_type.map do |child_accessor, child_class|
91
+ "node.#{child_accessor}.each do |child_node|
92
+ new_child_and_node = #{child_class.visit_method}_with_modifications(child_node, new_node)
93
+ # Reassign `node` in case the child hook makes a modification
94
+ if new_child_and_node.is_a?(Array)
95
+ new_node = new_child_and_node[1]
96
+ end
97
+ end"
98
+ end.join("\n")
99
+ else
100
+ ""
101
+ end
102
+ }
103
+
104
+ if new_node.equal?(node)
105
+ [node, parent]
106
+ else
107
+ [new_node, parent]
106
108
  end
107
109
  end
108
110
  end
109
- end_visit(new_node, parent) unless no_hooks
110
111
 
111
- if new_node.equal?(node)
112
- nil
112
+ def #{node_method}_with_modifications(node, parent)
113
+ new_node_and_new_parent = #{node_method}(node, parent)
114
+ apply_modifications(node, parent, new_node_and_new_parent)
115
+ end
116
+ RUBY
117
+ end
118
+
119
+ def on_document_children(document_node)
120
+ new_node = document_node
121
+ document_node.children.each do |child_node|
122
+ visit_method = :"#{child_node.visit_method}_with_modifications"
123
+ new_child_and_node = public_send(visit_method, child_node, new_node)
124
+ # Reassign `node` in case the child hook makes a modification
125
+ if new_child_and_node.is_a?(Array)
126
+ new_node = new_child_and_node[1]
127
+ end
128
+ end
129
+ new_node
130
+ end
131
+
132
+ def on_field_children(new_node)
133
+ new_node.arguments.each do |arg_node| # rubocop:disable Development/ContextIsPassedCop
134
+ new_child_and_node = on_argument_with_modifications(arg_node, new_node)
135
+ # Reassign `node` in case the child hook makes a modification
136
+ if new_child_and_node.is_a?(Array)
137
+ new_node = new_child_and_node[1]
138
+ end
139
+ end
140
+ new_node = visit_directives(new_node)
141
+ new_node = visit_selections(new_node)
142
+ new_node
143
+ end
144
+
145
+ def visit_directives(new_node)
146
+ new_node.directives.each do |dir_node|
147
+ new_child_and_node = on_directive_with_modifications(dir_node, new_node)
148
+ # Reassign `node` in case the child hook makes a modification
149
+ if new_child_and_node.is_a?(Array)
150
+ new_node = new_child_and_node[1]
151
+ end
152
+ end
153
+ new_node
154
+ end
155
+
156
+ def visit_selections(new_node)
157
+ new_node.selections.each do |selection|
158
+ new_child_and_node = case selection
159
+ when GraphQL::Language::Nodes::Field
160
+ on_field_with_modifications(selection, new_node)
161
+ when GraphQL::Language::Nodes::InlineFragment
162
+ on_inline_fragment_with_modifications(selection, new_node)
163
+ when GraphQL::Language::Nodes::FragmentSpread
164
+ on_fragment_spread_with_modifications(selection, new_node)
113
165
  else
114
- [new_node, parent]
166
+ raise ArgumentError, "Invariant: unexpected field selection #{selection.class} (#{selection.inspect})"
167
+ end
168
+ # Reassign `node` in case the child hook makes a modification
169
+ if new_child_and_node.is_a?(Array)
170
+ new_node = new_child_and_node[1]
115
171
  end
116
172
  end
173
+ new_node
117
174
  end
118
175
 
119
- # We don't use `alias` here because it breaks `super`
120
- def self.make_visit_method(node_method)
121
- class_eval(<<-RUBY, __FILE__, __LINE__ + 1)
122
- def #{node_method}(node, parent)
123
- child_mod = on_abstract_node(node, parent)
124
- # If visiting the children returned changes, continue passing those.
125
- child_mod || [node, parent]
176
+ def on_fragment_definition_children(new_node)
177
+ new_node = visit_directives(new_node)
178
+ new_node = visit_selections(new_node)
179
+ new_node
180
+ end
181
+
182
+ alias :on_inline_fragment_children :on_fragment_definition_children
183
+
184
+ def on_operation_definition_children(new_node)
185
+ new_node.variables.each do |arg_node|
186
+ new_child_and_node = on_variable_definition_with_modifications(arg_node, new_node)
187
+ # Reassign `node` in case the child hook makes a modification
188
+ if new_child_and_node.is_a?(Array)
189
+ new_node = new_child_and_node[1]
126
190
  end
127
- RUBY
191
+ end
192
+ new_node = visit_directives(new_node)
193
+ new_node = visit_selections(new_node)
194
+ new_node
128
195
  end
129
196
 
130
- make_visit_method :on_argument
131
- make_visit_method :on_directive
132
- make_visit_method :on_directive_definition
133
- make_visit_method :on_directive_location
134
- make_visit_method :on_document
135
- make_visit_method :on_enum
136
- make_visit_method :on_enum_type_definition
137
- make_visit_method :on_enum_type_extension
138
- make_visit_method :on_enum_value_definition
139
- make_visit_method :on_field
140
- make_visit_method :on_field_definition
141
- make_visit_method :on_fragment_definition
142
- make_visit_method :on_fragment_spread
143
- make_visit_method :on_inline_fragment
144
- make_visit_method :on_input_object
145
- make_visit_method :on_input_object_type_definition
146
- make_visit_method :on_input_object_type_extension
147
- make_visit_method :on_input_value_definition
148
- make_visit_method :on_interface_type_definition
149
- make_visit_method :on_interface_type_extension
150
- make_visit_method :on_list_type
151
- make_visit_method :on_non_null_type
152
- make_visit_method :on_null_value
153
- make_visit_method :on_object_type_definition
154
- make_visit_method :on_object_type_extension
155
- make_visit_method :on_operation_definition
156
- make_visit_method :on_scalar_type_definition
157
- make_visit_method :on_scalar_type_extension
158
- make_visit_method :on_schema_definition
159
- make_visit_method :on_schema_extension
160
- make_visit_method :on_type_name
161
- make_visit_method :on_union_type_definition
162
- make_visit_method :on_union_type_extension
163
- make_visit_method :on_variable_definition
164
- make_visit_method :on_variable_identifier
197
+ def on_argument_children(new_node)
198
+ new_node.children.each do |value_node|
199
+ new_child_and_node = case value_node
200
+ when Language::Nodes::VariableIdentifier
201
+ on_variable_identifier_with_modifications(value_node, new_node)
202
+ when Language::Nodes::InputObject
203
+ on_input_object_with_modifications(value_node, new_node)
204
+ when Language::Nodes::Enum
205
+ on_enum_with_modifications(value_node, new_node)
206
+ when Language::Nodes::NullValue
207
+ on_null_value_with_modifications(value_node, new_node)
208
+ else
209
+ raise ArgumentError, "Invariant: unexpected argument value node #{value_node.class} (#{value_node.inspect})"
210
+ end
211
+ # Reassign `node` in case the child hook makes a modification
212
+ if new_child_and_node.is_a?(Array)
213
+ new_node = new_child_and_node[1]
214
+ end
215
+ end
216
+ new_node
217
+ end
218
+
219
+ [
220
+ Language::Nodes::Argument,
221
+ Language::Nodes::Directive,
222
+ Language::Nodes::DirectiveDefinition,
223
+ Language::Nodes::DirectiveLocation,
224
+ Language::Nodes::Document,
225
+ Language::Nodes::Enum,
226
+ Language::Nodes::EnumTypeDefinition,
227
+ Language::Nodes::EnumTypeExtension,
228
+ Language::Nodes::EnumValueDefinition,
229
+ Language::Nodes::Field,
230
+ Language::Nodes::FieldDefinition,
231
+ Language::Nodes::FragmentDefinition,
232
+ Language::Nodes::FragmentSpread,
233
+ Language::Nodes::InlineFragment,
234
+ Language::Nodes::InputObject,
235
+ Language::Nodes::InputObjectTypeDefinition,
236
+ Language::Nodes::InputObjectTypeExtension,
237
+ Language::Nodes::InputValueDefinition,
238
+ Language::Nodes::InterfaceTypeDefinition,
239
+ Language::Nodes::InterfaceTypeExtension,
240
+ Language::Nodes::ListType,
241
+ Language::Nodes::NonNullType,
242
+ Language::Nodes::NullValue,
243
+ Language::Nodes::ObjectTypeDefinition,
244
+ Language::Nodes::ObjectTypeExtension,
245
+ Language::Nodes::OperationDefinition,
246
+ Language::Nodes::ScalarTypeDefinition,
247
+ Language::Nodes::ScalarTypeExtension,
248
+ Language::Nodes::SchemaDefinition,
249
+ Language::Nodes::SchemaExtension,
250
+ Language::Nodes::TypeName,
251
+ Language::Nodes::UnionTypeDefinition,
252
+ Language::Nodes::UnionTypeExtension,
253
+ Language::Nodes::VariableDefinition,
254
+ Language::Nodes::VariableIdentifier,
255
+ ].each do |ast_node_class|
256
+ make_visit_methods(ast_node_class)
257
+ end
165
258
 
166
259
  private
167
260
 
168
- # Run the hooks for `node`, and if the hooks return a copy of `node`,
169
- # copy `parent` so that it contains the copy of that node as a child,
170
- # then return the copies
171
- # If a non-array value is returned, consuming functions should ignore
172
- # said value
173
- def on_node_with_modifications(node, parent)
174
- new_node_and_new_parent = visit_node(node, parent)
261
+ def apply_modifications(node, parent, new_node_and_new_parent)
175
262
  if new_node_and_new_parent.is_a?(Array)
176
263
  new_node = new_node_and_new_parent[0]
177
264
  new_parent = new_node_and_new_parent[1]
@@ -197,46 +284,6 @@ module GraphQL
197
284
  new_node_and_new_parent
198
285
  end
199
286
  end
200
-
201
- def begin_visit(node, parent)
202
- node_visitor = self[node.class]
203
- self.class.apply_hooks(node_visitor.enter, node, parent)
204
- end
205
-
206
- # Should global `leave` visitors come first or last?
207
- def end_visit(node, parent)
208
- node_visitor = self[node.class]
209
- self.class.apply_hooks(node_visitor.leave, node, parent)
210
- end
211
-
212
- # If one of the visitors returns SKIP, stop visiting this node
213
- def self.apply_hooks(hooks, node, parent)
214
- hooks.each do |proc|
215
- return false if proc.call(node, parent) == SKIP
216
- end
217
- true
218
- end
219
-
220
- # Collect `enter` and `leave` hooks for classes in {GraphQL::Language::Nodes}
221
- #
222
- # Access {NodeVisitor}s via {GraphQL::Language::Visitor#[]}
223
- class NodeVisitor
224
- # @return [Array<Proc>] Hooks to call when entering a node of this type
225
- attr_reader :enter
226
- # @return [Array<Proc>] Hooks to call when leaving a node of this type
227
- attr_reader :leave
228
-
229
- def initialize
230
- @enter = []
231
- @leave = []
232
- end
233
-
234
- # Shorthand to add a hook to the {#enter} array
235
- # @param hook [Proc] A hook to add
236
- def <<(hook)
237
- enter << hook
238
- end
239
- end
240
287
  end
241
288
  end
242
289
  end