graphql 1.13.17 → 2.0.20

Sign up to get free protection for your applications and to get access to all the features.
Files changed (260) hide show
  1. checksums.yaml +4 -4
  2. data/lib/generators/graphql/install_generator.rb +1 -1
  3. data/lib/generators/graphql/relay.rb +3 -17
  4. data/lib/generators/graphql/templates/schema.erb +3 -0
  5. data/lib/graphql/analysis/ast/field_usage.rb +3 -1
  6. data/lib/graphql/analysis/ast/max_query_complexity.rb +0 -1
  7. data/lib/graphql/analysis/ast/query_complexity.rb +1 -1
  8. data/lib/graphql/analysis/ast/query_depth.rb +0 -1
  9. data/lib/graphql/analysis/ast/visitor.rb +43 -36
  10. data/lib/graphql/analysis/ast.rb +2 -12
  11. data/lib/graphql/analysis.rb +0 -7
  12. data/lib/graphql/backtrace/table.rb +2 -20
  13. data/lib/graphql/backtrace/tracer.rb +2 -3
  14. data/lib/graphql/backtrace.rb +2 -8
  15. data/lib/graphql/dataloader/null_dataloader.rb +3 -1
  16. data/lib/graphql/dataloader/source.rb +9 -0
  17. data/lib/graphql/dataloader.rb +4 -1
  18. data/lib/graphql/dig.rb +1 -1
  19. data/lib/graphql/execution/errors.rb +12 -82
  20. data/lib/graphql/execution/interpreter/resolve.rb +26 -0
  21. data/lib/graphql/execution/interpreter/runtime.rb +159 -120
  22. data/lib/graphql/execution/interpreter.rb +187 -78
  23. data/lib/graphql/execution/lazy.rb +7 -21
  24. data/lib/graphql/execution/lookahead.rb +44 -40
  25. data/lib/graphql/execution/multiplex.rb +3 -174
  26. data/lib/graphql/execution.rb +11 -4
  27. data/lib/graphql/introspection/directive_type.rb +2 -2
  28. data/lib/graphql/introspection/dynamic_fields.rb +3 -8
  29. data/lib/graphql/introspection/entry_points.rb +2 -15
  30. data/lib/graphql/introspection/field_type.rb +1 -1
  31. data/lib/graphql/introspection/schema_type.rb +2 -2
  32. data/lib/graphql/introspection/type_type.rb +13 -6
  33. data/lib/graphql/introspection.rb +4 -3
  34. data/lib/graphql/language/document_from_schema_definition.rb +18 -35
  35. data/lib/graphql/language/lexer.rb +216 -1488
  36. data/lib/graphql/language/nodes.rb +65 -39
  37. data/lib/graphql/language/parser.rb +376 -364
  38. data/lib/graphql/language/parser.y +49 -44
  39. data/lib/graphql/language/printer.rb +37 -21
  40. data/lib/graphql/language/visitor.rb +191 -83
  41. data/lib/graphql/pagination/active_record_relation_connection.rb +0 -8
  42. data/lib/graphql/pagination/array_connection.rb +4 -2
  43. data/lib/graphql/pagination/connection.rb +31 -4
  44. data/lib/graphql/pagination/connections.rb +3 -28
  45. data/lib/graphql/pagination/relation_connection.rb +2 -0
  46. data/lib/graphql/query/context.rb +155 -196
  47. data/lib/graphql/query/input_validation_result.rb +1 -1
  48. data/lib/graphql/query/null_context.rb +0 -3
  49. data/lib/graphql/query/validation_pipeline.rb +10 -34
  50. data/lib/graphql/query/variables.rb +7 -20
  51. data/lib/graphql/query.rb +32 -42
  52. data/lib/graphql/railtie.rb +0 -104
  53. data/lib/graphql/rake_task/validate.rb +1 -1
  54. data/lib/graphql/rake_task.rb +29 -1
  55. data/lib/graphql/relay/range_add.rb +9 -20
  56. data/lib/graphql/relay.rb +0 -15
  57. data/lib/graphql/schema/addition.rb +7 -9
  58. data/lib/graphql/schema/argument.rb +36 -43
  59. data/lib/graphql/schema/build_from_definition.rb +32 -18
  60. data/lib/graphql/schema/directive/one_of.rb +12 -0
  61. data/lib/graphql/schema/directive/transform.rb +1 -1
  62. data/lib/graphql/schema/directive.rb +12 -23
  63. data/lib/graphql/schema/enum.rb +28 -39
  64. data/lib/graphql/schema/enum_value.rb +5 -25
  65. data/lib/graphql/schema/field/connection_extension.rb +4 -0
  66. data/lib/graphql/schema/field.rb +237 -339
  67. data/lib/graphql/schema/input_object.rb +56 -67
  68. data/lib/graphql/schema/interface.rb +0 -35
  69. data/lib/graphql/schema/introspection_system.rb +3 -8
  70. data/lib/graphql/schema/late_bound_type.rb +8 -2
  71. data/lib/graphql/schema/list.rb +0 -6
  72. data/lib/graphql/schema/loader.rb +1 -2
  73. data/lib/graphql/schema/member/base_dsl_methods.rb +17 -19
  74. data/lib/graphql/schema/member/build_type.rb +5 -7
  75. data/lib/graphql/schema/member/has_arguments.rb +146 -55
  76. data/lib/graphql/schema/member/has_ast_node.rb +12 -0
  77. data/lib/graphql/schema/member/has_deprecation_reason.rb +3 -4
  78. data/lib/graphql/schema/member/has_directives.rb +81 -59
  79. data/lib/graphql/schema/member/has_fields.rb +17 -4
  80. data/lib/graphql/schema/member/has_interfaces.rb +49 -10
  81. data/lib/graphql/schema/member/has_validators.rb +31 -5
  82. data/lib/graphql/schema/member/relay_shortcuts.rb +47 -2
  83. data/lib/graphql/schema/member/type_system_helpers.rb +17 -0
  84. data/lib/graphql/schema/member/validates_input.rb +1 -1
  85. data/lib/graphql/schema/member.rb +0 -6
  86. data/lib/graphql/schema/mutation.rb +0 -9
  87. data/lib/graphql/schema/non_null.rb +1 -7
  88. data/lib/graphql/schema/object.rb +15 -52
  89. data/lib/graphql/schema/relay_classic_mutation.rb +53 -42
  90. data/lib/graphql/schema/resolver/has_payload_type.rb +20 -10
  91. data/lib/graphql/schema/resolver.rb +41 -42
  92. data/lib/graphql/schema/scalar.rb +7 -22
  93. data/lib/graphql/schema/subscription.rb +0 -7
  94. data/lib/graphql/schema/timeout.rb +24 -28
  95. data/lib/graphql/schema/type_membership.rb +3 -0
  96. data/lib/graphql/schema/union.rb +10 -17
  97. data/lib/graphql/schema/warden.rb +34 -8
  98. data/lib/graphql/schema/wrapper.rb +0 -5
  99. data/lib/graphql/schema.rb +241 -973
  100. data/lib/graphql/static_validation/all_rules.rb +1 -0
  101. data/lib/graphql/static_validation/base_visitor.rb +4 -21
  102. data/lib/graphql/static_validation/definition_dependencies.rb +7 -1
  103. data/lib/graphql/static_validation/error.rb +2 -2
  104. data/lib/graphql/static_validation/literal_validator.rb +19 -1
  105. data/lib/graphql/static_validation/rules/directives_are_defined.rb +11 -5
  106. data/lib/graphql/static_validation/rules/directives_are_in_valid_locations.rb +12 -12
  107. data/lib/graphql/static_validation/rules/one_of_input_objects_are_valid.rb +66 -0
  108. data/lib/graphql/static_validation/rules/one_of_input_objects_are_valid_error.rb +29 -0
  109. data/lib/graphql/static_validation/rules/unique_directives_per_location.rb +12 -6
  110. data/lib/graphql/static_validation/rules/variable_default_values_are_correctly_typed.rb +1 -1
  111. data/lib/graphql/static_validation/validator.rb +3 -25
  112. data/lib/graphql/static_validation.rb +0 -2
  113. data/lib/graphql/subscriptions/action_cable_subscriptions.rb +7 -1
  114. data/lib/graphql/subscriptions/default_subscription_resolve_extension.rb +38 -1
  115. data/lib/graphql/subscriptions/event.rb +3 -8
  116. data/lib/graphql/subscriptions/instrumentation.rb +0 -51
  117. data/lib/graphql/subscriptions.rb +32 -20
  118. data/lib/graphql/tracing/active_support_notifications_trace.rb +16 -0
  119. data/lib/graphql/tracing/appoptics_trace.rb +231 -0
  120. data/lib/graphql/tracing/appsignal_trace.rb +71 -0
  121. data/lib/graphql/tracing/data_dog_trace.rb +148 -0
  122. data/lib/graphql/tracing/data_dog_tracing.rb +2 -0
  123. data/lib/graphql/tracing/new_relic_trace.rb +75 -0
  124. data/lib/graphql/tracing/notifications_trace.rb +41 -0
  125. data/lib/graphql/tracing/platform_trace.rb +107 -0
  126. data/lib/graphql/tracing/platform_tracing.rb +26 -40
  127. data/lib/graphql/tracing/prometheus_trace.rb +89 -0
  128. data/lib/graphql/tracing/prometheus_tracing.rb +3 -3
  129. data/lib/graphql/tracing/scout_trace.rb +72 -0
  130. data/lib/graphql/tracing/statsd_trace.rb +56 -0
  131. data/lib/graphql/tracing.rb +136 -40
  132. data/lib/graphql/type_kinds.rb +6 -3
  133. data/lib/graphql/types/iso_8601_date.rb +4 -1
  134. data/lib/graphql/types/iso_8601_date_time.rb +4 -0
  135. data/lib/graphql/types/relay/base_connection.rb +16 -6
  136. data/lib/graphql/types/relay/connection_behaviors.rb +29 -27
  137. data/lib/graphql/types/relay/edge_behaviors.rb +16 -5
  138. data/lib/graphql/types/relay/node_behaviors.rb +12 -2
  139. data/lib/graphql/types/relay/page_info_behaviors.rb +7 -2
  140. data/lib/graphql/types/relay.rb +0 -3
  141. data/lib/graphql/types/string.rb +1 -1
  142. data/lib/graphql/version.rb +1 -1
  143. data/lib/graphql.rb +13 -74
  144. metadata +30 -133
  145. data/lib/graphql/analysis/analyze_query.rb +0 -98
  146. data/lib/graphql/analysis/field_usage.rb +0 -45
  147. data/lib/graphql/analysis/max_query_complexity.rb +0 -26
  148. data/lib/graphql/analysis/max_query_depth.rb +0 -26
  149. data/lib/graphql/analysis/query_complexity.rb +0 -88
  150. data/lib/graphql/analysis/query_depth.rb +0 -43
  151. data/lib/graphql/analysis/reducer_state.rb +0 -48
  152. data/lib/graphql/argument.rb +0 -131
  153. data/lib/graphql/authorization.rb +0 -82
  154. data/lib/graphql/backtrace/legacy_tracer.rb +0 -56
  155. data/lib/graphql/backwards_compatibility.rb +0 -61
  156. data/lib/graphql/base_type.rb +0 -232
  157. data/lib/graphql/boolean_type.rb +0 -2
  158. data/lib/graphql/compatibility/execution_specification/counter_schema.rb +0 -53
  159. data/lib/graphql/compatibility/execution_specification/specification_schema.rb +0 -200
  160. data/lib/graphql/compatibility/execution_specification.rb +0 -436
  161. data/lib/graphql/compatibility/lazy_execution_specification/lazy_schema.rb +0 -111
  162. data/lib/graphql/compatibility/lazy_execution_specification.rb +0 -215
  163. data/lib/graphql/compatibility/query_parser_specification/parse_error_specification.rb +0 -87
  164. data/lib/graphql/compatibility/query_parser_specification/query_assertions.rb +0 -79
  165. data/lib/graphql/compatibility/query_parser_specification.rb +0 -266
  166. data/lib/graphql/compatibility/schema_parser_specification.rb +0 -682
  167. data/lib/graphql/compatibility.rb +0 -5
  168. data/lib/graphql/define/assign_argument.rb +0 -12
  169. data/lib/graphql/define/assign_connection.rb +0 -13
  170. data/lib/graphql/define/assign_enum_value.rb +0 -18
  171. data/lib/graphql/define/assign_global_id_field.rb +0 -11
  172. data/lib/graphql/define/assign_mutation_function.rb +0 -34
  173. data/lib/graphql/define/assign_object_field.rb +0 -42
  174. data/lib/graphql/define/defined_object_proxy.rb +0 -53
  175. data/lib/graphql/define/instance_definable.rb +0 -255
  176. data/lib/graphql/define/no_definition_error.rb +0 -7
  177. data/lib/graphql/define/non_null_with_bang.rb +0 -16
  178. data/lib/graphql/define/type_definer.rb +0 -31
  179. data/lib/graphql/define.rb +0 -31
  180. data/lib/graphql/deprecated_dsl.rb +0 -55
  181. data/lib/graphql/directive/deprecated_directive.rb +0 -2
  182. data/lib/graphql/directive/include_directive.rb +0 -2
  183. data/lib/graphql/directive/skip_directive.rb +0 -2
  184. data/lib/graphql/directive.rb +0 -107
  185. data/lib/graphql/enum_type.rb +0 -133
  186. data/lib/graphql/execution/execute.rb +0 -333
  187. data/lib/graphql/execution/flatten.rb +0 -40
  188. data/lib/graphql/execution/instrumentation.rb +0 -92
  189. data/lib/graphql/execution/lazy/resolve.rb +0 -91
  190. data/lib/graphql/execution/typecast.rb +0 -50
  191. data/lib/graphql/field/resolve.rb +0 -59
  192. data/lib/graphql/field.rb +0 -226
  193. data/lib/graphql/float_type.rb +0 -2
  194. data/lib/graphql/function.rb +0 -128
  195. data/lib/graphql/id_type.rb +0 -2
  196. data/lib/graphql/input_object_type.rb +0 -138
  197. data/lib/graphql/int_type.rb +0 -2
  198. data/lib/graphql/interface_type.rb +0 -72
  199. data/lib/graphql/internal_representation/document.rb +0 -27
  200. data/lib/graphql/internal_representation/node.rb +0 -206
  201. data/lib/graphql/internal_representation/print.rb +0 -51
  202. data/lib/graphql/internal_representation/rewrite.rb +0 -184
  203. data/lib/graphql/internal_representation/scope.rb +0 -88
  204. data/lib/graphql/internal_representation/visit.rb +0 -36
  205. data/lib/graphql/internal_representation.rb +0 -7
  206. data/lib/graphql/language/lexer.rl +0 -260
  207. data/lib/graphql/list_type.rb +0 -80
  208. data/lib/graphql/non_null_type.rb +0 -71
  209. data/lib/graphql/object_type.rb +0 -130
  210. data/lib/graphql/query/arguments.rb +0 -189
  211. data/lib/graphql/query/arguments_cache.rb +0 -24
  212. data/lib/graphql/query/executor.rb +0 -52
  213. data/lib/graphql/query/literal_input.rb +0 -136
  214. data/lib/graphql/query/serial_execution/field_resolution.rb +0 -92
  215. data/lib/graphql/query/serial_execution/operation_resolution.rb +0 -19
  216. data/lib/graphql/query/serial_execution/selection_resolution.rb +0 -23
  217. data/lib/graphql/query/serial_execution/value_resolution.rb +0 -87
  218. data/lib/graphql/query/serial_execution.rb +0 -40
  219. data/lib/graphql/relay/array_connection.rb +0 -83
  220. data/lib/graphql/relay/base_connection.rb +0 -189
  221. data/lib/graphql/relay/connection_instrumentation.rb +0 -54
  222. data/lib/graphql/relay/connection_resolve.rb +0 -43
  223. data/lib/graphql/relay/connection_type.rb +0 -54
  224. data/lib/graphql/relay/edge.rb +0 -27
  225. data/lib/graphql/relay/edge_type.rb +0 -19
  226. data/lib/graphql/relay/edges_instrumentation.rb +0 -39
  227. data/lib/graphql/relay/global_id_resolve.rb +0 -17
  228. data/lib/graphql/relay/mongo_relation_connection.rb +0 -50
  229. data/lib/graphql/relay/mutation/instrumentation.rb +0 -23
  230. data/lib/graphql/relay/mutation/resolve.rb +0 -56
  231. data/lib/graphql/relay/mutation/result.rb +0 -38
  232. data/lib/graphql/relay/mutation.rb +0 -106
  233. data/lib/graphql/relay/node.rb +0 -39
  234. data/lib/graphql/relay/page_info.rb +0 -7
  235. data/lib/graphql/relay/relation_connection.rb +0 -188
  236. data/lib/graphql/relay/type_extensions.rb +0 -32
  237. data/lib/graphql/scalar_type.rb +0 -91
  238. data/lib/graphql/schema/catchall_middleware.rb +0 -35
  239. data/lib/graphql/schema/default_parse_error.rb +0 -10
  240. data/lib/graphql/schema/default_type_error.rb +0 -17
  241. data/lib/graphql/schema/member/accepts_definition.rb +0 -164
  242. data/lib/graphql/schema/member/cached_graphql_definition.rb +0 -58
  243. data/lib/graphql/schema/member/instrumentation.rb +0 -131
  244. data/lib/graphql/schema/middleware_chain.rb +0 -82
  245. data/lib/graphql/schema/possible_types.rb +0 -44
  246. data/lib/graphql/schema/rescue_middleware.rb +0 -60
  247. data/lib/graphql/schema/timeout_middleware.rb +0 -88
  248. data/lib/graphql/schema/traversal.rb +0 -228
  249. data/lib/graphql/schema/validation.rb +0 -313
  250. data/lib/graphql/static_validation/default_visitor.rb +0 -15
  251. data/lib/graphql/static_validation/no_validate_visitor.rb +0 -10
  252. data/lib/graphql/string_type.rb +0 -2
  253. data/lib/graphql/subscriptions/subscription_root.rb +0 -76
  254. data/lib/graphql/tracing/skylight_tracing.rb +0 -70
  255. data/lib/graphql/types/relay/default_relay.rb +0 -31
  256. data/lib/graphql/types/relay/node_field.rb +0 -24
  257. data/lib/graphql/types/relay/nodes_field.rb +0 -43
  258. data/lib/graphql/union_type.rb +0 -115
  259. data/lib/graphql/upgrader/member.rb +0 -937
  260. data/lib/graphql/upgrader/schema.rb +0 -38
@@ -11,10 +11,11 @@ module GraphQL
11
11
  def self.extended(cls)
12
12
  cls.extend(ArgumentClassAccessor)
13
13
  cls.include(ArgumentObjectLoader)
14
+ cls.extend(ClassConfigured)
14
15
  end
15
16
 
16
17
  # @see {GraphQL::Schema::Argument#initialize} for parameters
17
- # @return [GraphQL::Schema::Argument] An instance of {arguments_class}, created from `*args`
18
+ # @return [GraphQL::Schema::Argument] An instance of {argument_class}, created from `*args`
18
19
  def argument(*args, **kwargs, &block)
19
20
  kwargs[:owner] = self
20
21
  loads = kwargs[:loads]
@@ -78,24 +79,37 @@ module GraphQL
78
79
  # @return [GraphQL::Schema::Argument]
79
80
  def add_argument(arg_defn)
80
81
  @own_arguments ||= {}
81
- prev_defn = own_arguments[arg_defn.name]
82
+ prev_defn = @own_arguments[arg_defn.name]
82
83
  case prev_defn
83
84
  when nil
84
- own_arguments[arg_defn.name] = arg_defn
85
+ @own_arguments[arg_defn.name] = arg_defn
85
86
  when Array
86
87
  prev_defn << arg_defn
87
88
  when GraphQL::Schema::Argument
88
- own_arguments[arg_defn.name] = [prev_defn, arg_defn]
89
+ @own_arguments[arg_defn.name] = [prev_defn, arg_defn]
89
90
  else
90
91
  raise "Invariant: unexpected `@own_arguments[#{arg_defn.name.inspect}]`: #{prev_defn.inspect}"
91
92
  end
92
93
  arg_defn
93
94
  end
94
95
 
96
+ def remove_argument(arg_defn)
97
+ prev_defn = @own_arguments[arg_defn.name]
98
+ case prev_defn
99
+ when nil
100
+ # done
101
+ when Array
102
+ prev_defn.delete(arg_defn)
103
+ when GraphQL::Schema::Argument
104
+ @own_arguments.delete(arg_defn.name)
105
+ else
106
+ raise "Invariant: unexpected `@own_arguments[#{arg_defn.name.inspect}]`: #{prev_defn.inspect}"
107
+ end
108
+ nil
109
+ end
110
+
95
111
  # @return [Hash<String => GraphQL::Schema::Argument] Arguments defined on this thing, keyed by name. Includes inherited definitions
96
112
  def arguments(context = GraphQL::Query::NullContext)
97
- inherited_arguments = ((self.is_a?(Class) && superclass.respond_to?(:arguments)) ? superclass.arguments(context) : nil)
98
- # Local definitions override inherited ones
99
113
  if own_arguments.any?
100
114
  own_arguments_that_apply = {}
101
115
  own_arguments.each do |name, args_entry|
@@ -104,31 +118,107 @@ module GraphQL
104
118
  end
105
119
  end
106
120
  end
121
+ # might be nil if there are actually no arguments
122
+ own_arguments_that_apply || own_arguments
123
+ end
124
+
125
+ module ClassConfigured
126
+ def inherited(child_class)
127
+ super
128
+ child_class.extend(InheritedArguments)
129
+ end
107
130
 
108
- if inherited_arguments
109
- if own_arguments_that_apply
110
- inherited_arguments.merge(own_arguments_that_apply)
111
- else
112
- inherited_arguments
131
+ module InheritedArguments
132
+ def arguments(context = GraphQL::Query::NullContext)
133
+ own_arguments = super
134
+ inherited_arguments = superclass.arguments(context)
135
+
136
+ if own_arguments.any?
137
+ if inherited_arguments.any?
138
+ # Local definitions override inherited ones
139
+ inherited_arguments.merge(own_arguments)
140
+ else
141
+ own_arguments
142
+ end
143
+ else
144
+ inherited_arguments
145
+ end
146
+ end
147
+
148
+ def all_argument_definitions
149
+ all_defns = {}
150
+ ancestors.reverse_each do |ancestor|
151
+ if ancestor.respond_to?(:own_arguments)
152
+ all_defns.merge!(ancestor.own_arguments)
153
+ end
154
+ end
155
+ all_defns = all_defns.values
156
+ all_defns.flatten!
157
+ all_defns
158
+ end
159
+
160
+
161
+ def get_argument(argument_name, context = GraphQL::Query::NullContext)
162
+ warden = Warden.from_context(context)
163
+ for ancestor in ancestors
164
+ if ancestor.respond_to?(:own_arguments) &&
165
+ (a = ancestor.own_arguments[argument_name]) &&
166
+ (a = Warden.visible_entry?(:visible_argument?, a, context, warden))
167
+ return a
168
+ end
169
+ end
170
+ nil
113
171
  end
114
- else
115
- # might be nil if there are actually no arguments
116
- own_arguments_that_apply || own_arguments
117
172
  end
118
173
  end
119
174
 
120
- def all_argument_definitions
121
- if self.is_a?(Class)
122
- all_defns = {}
123
- ancestors.reverse_each do |ancestor|
124
- if ancestor.respond_to?(:own_arguments)
125
- all_defns.merge!(ancestor.own_arguments)
175
+ module FieldConfigured
176
+ def arguments(context = GraphQL::Query::NullContext)
177
+ own_arguments = super
178
+ if defined?(@resolver_class) && @resolver_class
179
+ inherited_arguments = @resolver_class.field_arguments(context)
180
+ if own_arguments.any?
181
+ if inherited_arguments.any?
182
+ inherited_arguments.merge(own_arguments)
183
+ else
184
+ own_arguments
185
+ end
186
+ else
187
+ inherited_arguments
126
188
  end
189
+ else
190
+ own_arguments
191
+ end
192
+ end
193
+
194
+ def all_argument_definitions
195
+ if defined?(@resolver_class) && @resolver_class
196
+ all_defns = {}
197
+ @resolver_class.all_field_argument_definitions.each do |arg_defn|
198
+ key = arg_defn.graphql_name
199
+ case (current_value = all_defns[key])
200
+ when nil
201
+ all_defns[key] = arg_defn
202
+ when Array
203
+ current_value << arg_defn
204
+ when GraphQL::Schema::Argument
205
+ all_defns[key] = [current_value, arg_defn]
206
+ else
207
+ raise "Invariant: Unexpected argument definition, #{current_value.class}: #{current_value.inspect}"
208
+ end
209
+ end
210
+ all_defns.merge!(own_arguments)
211
+ all_defns = all_defns.values
212
+ all_defns.flatten!
213
+ all_defns
214
+ else
215
+ super
127
216
  end
128
- else
129
- all_defns = own_arguments
130
217
  end
131
- all_defns = all_defns.values
218
+ end
219
+
220
+ def all_argument_definitions
221
+ all_defns = own_arguments.values
132
222
  all_defns.flatten!
133
223
  all_defns
134
224
  end
@@ -136,17 +226,11 @@ module GraphQL
136
226
  # @return [GraphQL::Schema::Argument, nil] Argument defined on this thing, fetched by name.
137
227
  def get_argument(argument_name, context = GraphQL::Query::NullContext)
138
228
  warden = Warden.from_context(context)
139
- if !self.is_a?(Class)
140
- a = own_arguments[argument_name]
141
- a && Warden.visible_entry?(:visible_argument?, a, context, warden)
229
+ if (arg_config = own_arguments[argument_name]) && (visible_arg = Warden.visible_entry?(:visible_argument?, arg_config, context, warden))
230
+ visible_arg
231
+ elsif defined?(@resolver_class) && @resolver_class
232
+ @resolver_class.get_field_argument(argument_name, context)
142
233
  else
143
- for ancestor in ancestors
144
- if ancestor.respond_to?(:own_arguments) &&
145
- (a = ancestor.own_arguments[argument_name]) &&
146
- (a = Warden.visible_entry?(:visible_argument?, a, context, warden))
147
- return a
148
- end
149
- end
150
234
  nil
151
235
  end
152
236
  end
@@ -167,7 +251,7 @@ module GraphQL
167
251
  # @return [Interpreter::Arguments, Execution::Lazy<Interpeter::Arguments>]
168
252
  def coerce_arguments(parent_object, values, context, &block)
169
253
  # Cache this hash to avoid re-merging it
170
- arg_defns = self.arguments(context)
254
+ arg_defns = context.warden.arguments(self)
171
255
  total_args_count = arg_defns.size
172
256
 
173
257
  finished_args = nil
@@ -181,7 +265,7 @@ module GraphQL
181
265
  argument_values = {}
182
266
  resolved_args_count = 0
183
267
  raised_error = false
184
- arg_defns.each do |arg_name, arg_defn|
268
+ arg_defns.each do |arg_defn|
185
269
  context.dataloader.append_job do
186
270
  begin
187
271
  arg_defn.coerce_into_values(parent_object, values, context, argument_values)
@@ -223,7 +307,12 @@ module GraphQL
223
307
  # but not for directives.
224
308
  # TODO apply static validations on schema definitions?
225
309
  def validate_directive_argument(arg_defn, value)
226
- if arg_defn.owner.is_a?(Class) && arg_defn.owner < GraphQL::Schema::Directive
310
+ # this is only implemented on directives.
311
+ nil
312
+ end
313
+
314
+ module HasDirectiveArguments
315
+ def validate_directive_argument(arg_defn, value)
227
316
  if value.nil? && arg_defn.type.non_null?
228
317
  raise ArgumentError, "#{arg_defn.path} is required, but no value was given"
229
318
  end
@@ -281,8 +370,14 @@ module GraphQL
281
370
  end
282
371
  # Double-check that the located object is actually of this type
283
372
  # (Don't want to allow arbitrary access to objects this way)
284
- resolved_application_object_type = context.schema.resolve_type(argument.loads, application_object, context)
285
- context.schema.after_lazy(resolved_application_object_type) do |application_object_type|
373
+ maybe_lazy_resolve_type = context.schema.resolve_type(argument.loads, application_object, context)
374
+ context.schema.after_lazy(maybe_lazy_resolve_type) do |resolve_type_result|
375
+ if resolve_type_result.is_a?(Array) && resolve_type_result.size == 2
376
+ application_object_type, application_object = resolve_type_result
377
+ else
378
+ application_object_type = resolve_type_result
379
+ # application_object is already assigned
380
+ end
286
381
  possible_object_types = context.warden.possible_types(argument.loads)
287
382
  if !possible_object_types.include?(application_object_type)
288
383
  err = GraphQL::LoadApplicationObjectFailedError.new(argument: argument, id: id, object: application_object)
@@ -291,26 +386,22 @@ module GraphQL
291
386
  # This object was loaded successfully
292
387
  # and resolved to the right type,
293
388
  # now apply the `.authorized?` class method if there is one
294
- if (class_based_type = application_object_type.type_class)
295
- context.schema.after_lazy(class_based_type.authorized?(application_object, context)) do |authed|
296
- if authed
297
- application_object
389
+ context.schema.after_lazy(application_object_type.authorized?(application_object, context)) do |authed|
390
+ if authed
391
+ application_object
392
+ else
393
+ err = GraphQL::UnauthorizedError.new(
394
+ object: application_object,
395
+ type: application_object_type,
396
+ context: context,
397
+ )
398
+ if self.respond_to?(:unauthorized_object)
399
+ err.set_backtrace(caller)
400
+ unauthorized_object(err)
298
401
  else
299
- err = GraphQL::UnauthorizedError.new(
300
- object: application_object,
301
- type: class_based_type,
302
- context: context,
303
- )
304
- if self.respond_to?(:unauthorized_object)
305
- err.set_backtrace(caller)
306
- unauthorized_object(err)
307
- else
308
- raise err
309
- end
402
+ raise err
310
403
  end
311
404
  end
312
- else
313
- application_object
314
405
  end
315
406
  end
316
407
  end
@@ -3,6 +3,16 @@ module GraphQL
3
3
  class Schema
4
4
  class Member
5
5
  module HasAstNode
6
+ def self.extended(child_cls)
7
+ super
8
+ child_cls.ast_node = nil
9
+ end
10
+
11
+ def inherited(child_cls)
12
+ super
13
+ child_cls.ast_node = nil
14
+ end
15
+
6
16
  # If this schema was parsed from a `.graphql` file (or other SDL),
7
17
  # this is the AST node that defined this part of the schema.
8
18
  def ast_node(new_ast_node = nil)
@@ -14,6 +24,8 @@ module GraphQL
14
24
  nil
15
25
  end
16
26
  end
27
+
28
+ attr_writer :ast_node
17
29
  end
18
30
  end
19
31
  end
@@ -5,17 +5,16 @@ module GraphQL
5
5
  class Member
6
6
  module HasDeprecationReason
7
7
  # @return [String, nil] Explains why this member was deprecated (if present, this will be marked deprecated in introspection)
8
- def deprecation_reason
9
- dir = self.directives.find { |d| d.is_a?(GraphQL::Schema::Directive::Deprecated) }
10
- dir && dir.arguments[:reason] # rubocop:disable Development/ContextIsPassedCop -- definition-related
11
- end
8
+ attr_reader :deprecation_reason
12
9
 
13
10
  # Set the deprecation reason for this member, or remove it by assigning `nil`
14
11
  # @param text [String, nil]
15
12
  def deprecation_reason=(text)
13
+ @deprecation_reason = text
16
14
  if text.nil?
17
15
  remove_directive(GraphQL::Schema::Directive::Deprecated)
18
16
  else
17
+ # This removes a previously-attached directive, if there is one:
19
18
  directive(GraphQL::Schema::Directive::Deprecated, reason: text)
20
19
  end
21
20
  end
@@ -4,6 +4,16 @@ module GraphQL
4
4
  class Schema
5
5
  class Member
6
6
  module HasDirectives
7
+ def self.extended(child_cls)
8
+ super
9
+ child_cls.module_eval { self.own_directives = nil }
10
+ end
11
+
12
+ def inherited(child_cls)
13
+ super
14
+ child_cls.own_directives = nil
15
+ end
16
+
7
17
  # Create an instance of `dir_class` for `self`, using `options`.
8
18
  #
9
19
  # It removes a previously-attached instance of `dir_class`, if there is one.
@@ -11,8 +21,7 @@ module GraphQL
11
21
  # @return [void]
12
22
  def directive(dir_class, **options)
13
23
  @own_directives ||= []
14
- remove_directive(dir_class)
15
- @own_directives << dir_class.new(self, **options)
24
+ HasDirectives.add_directive(self, @own_directives, dir_class, options)
16
25
  nil
17
26
  end
18
27
 
@@ -20,78 +29,91 @@ module GraphQL
20
29
  # @param dir_class [Class<GraphQL::Schema::Directive>]
21
30
  # @return [viod]
22
31
  def remove_directive(dir_class)
23
- @own_directives && @own_directives.reject! { |d| d.is_a?(dir_class) }
32
+ HasDirectives.remove_directive(@own_directives, dir_class)
24
33
  nil
25
34
  end
26
35
 
27
36
  NO_DIRECTIVES = [].freeze
28
37
 
29
38
  def directives
30
- case self
31
- when Class
32
- inherited_directives = if superclass.respond_to?(:directives)
33
- superclass.directives
34
- else
35
- NO_DIRECTIVES
36
- end
37
- if inherited_directives.any? && @own_directives
38
- dirs = []
39
- merge_directives(dirs, inherited_directives)
40
- merge_directives(dirs, @own_directives)
41
- dirs
42
- elsif @own_directives
43
- @own_directives
44
- elsif inherited_directives.any?
45
- inherited_directives
46
- else
47
- NO_DIRECTIVES
48
- end
49
- when Module
50
- dirs = nil
51
- self.ancestors.reverse_each do |ancestor|
52
- if ancestor.respond_to?(:own_directives) &&
53
- (anc_dirs = ancestor.own_directives).any?
39
+ HasDirectives.get_directives(self, @own_directives, :directives)
40
+ end
41
+
42
+ class << self
43
+ def add_directive(schema_member, directives, directive_class, directive_options)
44
+ remove_directive(directives, directive_class) unless directive_class.repeatable?
45
+ directives << directive_class.new(schema_member, **directive_options)
46
+ end
47
+
48
+ def remove_directive(directives, directive_class)
49
+ directives && directives.reject! { |d| d.is_a?(directive_class) }
50
+ end
51
+
52
+ def get_directives(schema_member, directives, directives_method)
53
+ case schema_member
54
+ when Class
55
+ inherited_directives = if schema_member.superclass.respond_to?(directives_method)
56
+ get_directives(schema_member.superclass, schema_member.superclass.public_send(directives_method), directives_method)
57
+ else
58
+ NO_DIRECTIVES
59
+ end
60
+ if inherited_directives.any? && directives
61
+ dirs = []
62
+ merge_directives(dirs, inherited_directives)
63
+ merge_directives(dirs, directives)
64
+ dirs
65
+ elsif directives
66
+ directives
67
+ elsif inherited_directives.any?
68
+ inherited_directives
69
+ else
70
+ NO_DIRECTIVES
71
+ end
72
+ when Module
73
+ dirs = nil
74
+ schema_member.ancestors.reverse_each do |ancestor|
75
+ if ancestor.respond_to?(:own_directives) &&
76
+ (anc_dirs = ancestor.own_directives).any?
77
+ dirs ||= []
78
+ merge_directives(dirs, anc_dirs)
79
+ end
80
+ end
81
+ if directives
54
82
  dirs ||= []
55
- merge_directives(dirs, anc_dirs)
83
+ merge_directives(dirs, directives)
56
84
  end
85
+ dirs || NO_DIRECTIVES
86
+ when HasDirectives
87
+ directives || NO_DIRECTIVES
88
+ else
89
+ raise "Invariant: how could #{schema_member} not be a Class, Module, or instance of HasDirectives?"
57
90
  end
58
- if own_directives
59
- dirs ||= []
60
- merge_directives(dirs, own_directives)
61
- end
62
- dirs || NO_DIRECTIVES
63
- when HasDirectives
64
- @own_directives || NO_DIRECTIVES
65
- else
66
- raise "Invariant: how could #{self} not be a Class, Module, or instance of HasDirectives?"
67
91
  end
68
- end
69
-
70
- protected
71
-
72
- def own_directives
73
- @own_directives
74
- end
75
92
 
76
- private
93
+ private
77
94
 
78
- # Modify `target` by adding items from `dirs` such that:
79
- # - Any name conflict is overriden by the incoming member of `dirs`
80
- # - Any other member of `dirs` is appended
81
- # @param target [Array<GraphQL::Schema::Directive>]
82
- # @param dirs [Array<GraphQL::Schema::Directive>]
83
- # @return [void]
84
- def merge_directives(target, dirs)
85
- dirs.each do |dir|
86
- if (idx = target.find_index { |d| d.graphql_name == dir.graphql_name })
87
- target.slice!(idx)
88
- target.insert(idx, dir)
89
- else
90
- target << dir
95
+ # Modify `target` by adding items from `dirs` such that:
96
+ # - Any name conflict is overriden by the incoming member of `dirs`
97
+ # - Any other member of `dirs` is appended
98
+ # @param target [Array<GraphQL::Schema::Directive>]
99
+ # @param dirs [Array<GraphQL::Schema::Directive>]
100
+ # @return [void]
101
+ def merge_directives(target, dirs)
102
+ dirs.each do |dir|
103
+ if (idx = target.find_index { |d| d.graphql_name == dir.graphql_name })
104
+ target.slice!(idx)
105
+ target.insert(idx, dir)
106
+ else
107
+ target << dir
108
+ end
91
109
  end
110
+ nil
92
111
  end
93
- nil
94
112
  end
113
+
114
+ protected
115
+
116
+ attr_accessor :own_directives
95
117
  end
96
118
  end
97
119
  end
@@ -72,7 +72,12 @@ module GraphQL
72
72
  def add_field(field_defn, method_conflict_warning: field_defn.method_conflict_warning?)
73
73
  # Check that `field_defn.original_name` equals `resolver_method` and `method_sym` --
74
74
  # that shows that no override value was given manually.
75
- if method_conflict_warning && CONFLICT_FIELD_NAMES.include?(field_defn.resolver_method) && field_defn.original_name == field_defn.resolver_method && field_defn.original_name == field_defn.method_sym
75
+ if method_conflict_warning &&
76
+ CONFLICT_FIELD_NAMES.include?(field_defn.resolver_method) &&
77
+ field_defn.original_name == field_defn.resolver_method &&
78
+ field_defn.original_name == field_defn.method_sym &&
79
+ field_defn.hash_key == NOT_CONFIGURED &&
80
+ field_defn.dig_keys.nil?
76
81
  warn(conflict_field_name_warning(field_defn))
77
82
  end
78
83
  prev_defn = own_fields[field_defn.name]
@@ -103,10 +108,10 @@ module GraphQL
103
108
  end
104
109
 
105
110
  def global_id_field(field_name, **kwargs)
106
- id_resolver = GraphQL::Relay::GlobalIdResolve.new(type: self)
111
+ type = self
107
112
  field field_name, "ID", **kwargs, null: false
108
113
  define_method(field_name) do
109
- id_resolver.call(object, {}, context)
114
+ context.schema.id_from_object(object, type, context)
110
115
  end
111
116
  end
112
117
 
@@ -129,12 +134,20 @@ module GraphQL
129
134
 
130
135
  private
131
136
 
137
+ def inherited(subclass)
138
+ super
139
+ subclass.class_eval do
140
+ @own_fields ||= nil
141
+ @field_class ||= nil
142
+ end
143
+ end
144
+
132
145
  # If `type` is an interface, and `self` has a type membership for `type`, then make sure it's visible.
133
146
  def visible_interface_implementation?(type, context, warden)
134
147
  if type.respond_to?(:kind) && type.kind.interface?
135
148
  implements_this_interface = false
136
149
  implementation_is_visible = false
137
- interface_type_memberships.each do |tm|
150
+ warden.interface_type_memberships(self, context).each do |tm|
138
151
  if tm.abstract_type == type
139
152
  implements_this_interface ||= true
140
153
  if warden.visible_type_membership?(tm, context)
@@ -23,8 +23,6 @@ module GraphQL
23
23
  int.interfaces.each do |next_interface|
24
24
  implements(next_interface)
25
25
  end
26
- elsif int.is_a?(GraphQL::InterfaceType)
27
- new_memberships << int.type_membership_class.new(int, self, **options)
28
26
  elsif int.is_a?(String) || int.is_a?(GraphQL::Schema::LateBoundType)
29
27
  if options.any?
30
28
  raise ArgumentError, "`implements(...)` doesn't support options with late-loaded types yet. Remove #{options} and open an issue to request this feature."
@@ -57,7 +55,38 @@ module GraphQL
57
55
  end
58
56
 
59
57
  def interface_type_memberships
60
- own_interface_type_memberships + ((self.is_a?(Class) && superclass.respond_to?(:interface_type_memberships)) ? superclass.interface_type_memberships : [])
58
+ own_interface_type_memberships
59
+ end
60
+
61
+ module ClassConfigured
62
+ # This combination of extended -> inherited -> extended
63
+ # means that the base class (`Schema::Object`) *won't*
64
+ # have the superclass-related code in `InheritedInterfaces`,
65
+ # but child classes of `Schema::Object` will have it.
66
+ # That way, we don't need a `superclass.respond_to?(...)` check.
67
+ def inherited(child_class)
68
+ super
69
+ child_class.extend(InheritedInterfaces)
70
+ end
71
+
72
+ module InheritedInterfaces
73
+ def interfaces(context = GraphQL::Query::NullContext)
74
+ visible_interfaces = super
75
+ visible_interfaces.concat(superclass.interfaces(context))
76
+ visible_interfaces.uniq!
77
+ visible_interfaces
78
+ end
79
+
80
+ def interface_type_memberships
81
+ own_tms = super
82
+ inherited_tms = superclass.interface_type_memberships
83
+ if inherited_tms.size > 0
84
+ own_tms + inherited_tms
85
+ else
86
+ own_tms
87
+ end
88
+ end
89
+ end
61
90
  end
62
91
 
63
92
  # param context [Query::Context] If omitted, skip filtering.
@@ -65,25 +94,35 @@ module GraphQL
65
94
  warden = Warden.from_context(context)
66
95
  visible_interfaces = []
67
96
  own_interface_type_memberships.each do |type_membership|
68
- # During initialization, `type_memberships` can hold late-bound types
69
97
  case type_membership
70
- when String, Schema::LateBoundType
71
- visible_interfaces << type_membership
72
98
  when Schema::TypeMembership
73
99
  if warden.visible_type_membership?(type_membership, context)
74
100
  visible_interfaces << type_membership.abstract_type
75
101
  end
102
+ when String, Schema::LateBoundType
103
+ # During initialization, `type_memberships` can hold late-bound types
104
+ visible_interfaces << type_membership
76
105
  else
77
106
  raise "Invariant: Unexpected type_membership #{type_membership.class}: #{type_membership.inspect}"
78
107
  end
79
108
  end
80
-
81
- if self.is_a?(Class) && superclass <= GraphQL::Schema::Object
82
- visible_interfaces.concat(superclass.interfaces(context))
83
- end
109
+ visible_interfaces.uniq!
84
110
 
85
111
  visible_interfaces
86
112
  end
113
+
114
+ private
115
+
116
+ def self.extended(child_class)
117
+ child_class.extend(ClassConfigured)
118
+ end
119
+
120
+ def inherited(subclass)
121
+ super
122
+ subclass.class_eval do
123
+ @own_interface_type_memberships ||= nil
124
+ end
125
+ end
87
126
  end
88
127
  end
89
128
  end