graphql 1.11.6 → 1.13.19

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of graphql might be problematic. Click here for more details.

Files changed (293) hide show
  1. checksums.yaml +4 -4
  2. data/lib/generators/graphql/core.rb +3 -8
  3. data/lib/generators/graphql/enum_generator.rb +4 -10
  4. data/lib/generators/graphql/field_extractor.rb +31 -0
  5. data/lib/generators/graphql/input_generator.rb +50 -0
  6. data/lib/generators/graphql/install/mutation_root_generator.rb +34 -0
  7. data/lib/generators/graphql/install_generator.rb +17 -7
  8. data/lib/generators/graphql/interface_generator.rb +7 -7
  9. data/lib/generators/graphql/loader_generator.rb +1 -0
  10. data/lib/generators/graphql/mutation_create_generator.rb +22 -0
  11. data/lib/generators/graphql/mutation_delete_generator.rb +22 -0
  12. data/lib/generators/graphql/mutation_generator.rb +6 -30
  13. data/lib/generators/graphql/mutation_update_generator.rb +22 -0
  14. data/lib/generators/graphql/object_generator.rb +12 -38
  15. data/lib/generators/graphql/orm_mutations_base.rb +40 -0
  16. data/lib/generators/graphql/relay.rb +63 -0
  17. data/lib/generators/graphql/relay_generator.rb +21 -0
  18. data/lib/generators/graphql/scalar_generator.rb +4 -2
  19. data/lib/generators/graphql/templates/base_connection.erb +8 -0
  20. data/lib/generators/graphql/templates/base_edge.erb +8 -0
  21. data/lib/generators/graphql/templates/enum.erb +5 -1
  22. data/lib/generators/graphql/templates/graphql_controller.erb +2 -2
  23. data/lib/generators/graphql/templates/input.erb +9 -0
  24. data/lib/generators/graphql/templates/interface.erb +4 -2
  25. data/lib/generators/graphql/templates/mutation.erb +1 -1
  26. data/lib/generators/graphql/templates/mutation_create.erb +20 -0
  27. data/lib/generators/graphql/templates/mutation_delete.erb +20 -0
  28. data/lib/generators/graphql/templates/mutation_update.erb +21 -0
  29. data/lib/generators/graphql/templates/node_type.erb +9 -0
  30. data/lib/generators/graphql/templates/object.erb +5 -3
  31. data/lib/generators/graphql/templates/query_type.erb +1 -3
  32. data/lib/generators/graphql/templates/scalar.erb +3 -1
  33. data/lib/generators/graphql/templates/schema.erb +19 -34
  34. data/lib/generators/graphql/templates/union.erb +4 -2
  35. data/lib/generators/graphql/type_generator.rb +47 -10
  36. data/lib/generators/graphql/union_generator.rb +5 -5
  37. data/lib/graphql/analysis/analyze_query.rb +7 -0
  38. data/lib/graphql/analysis/ast/field_usage.rb +28 -1
  39. data/lib/graphql/analysis/ast/query_complexity.rb +10 -14
  40. data/lib/graphql/analysis/ast/visitor.rb +14 -5
  41. data/lib/graphql/analysis/ast.rb +11 -2
  42. data/lib/graphql/argument.rb +1 -1
  43. data/lib/graphql/backtrace/inspect_result.rb +0 -1
  44. data/lib/graphql/backtrace/legacy_tracer.rb +56 -0
  45. data/lib/graphql/backtrace/table.rb +34 -3
  46. data/lib/graphql/backtrace/traced_error.rb +0 -1
  47. data/lib/graphql/backtrace/tracer.rb +40 -10
  48. data/lib/graphql/backtrace.rb +28 -19
  49. data/lib/graphql/backwards_compatibility.rb +2 -1
  50. data/lib/graphql/base_type.rb +6 -4
  51. data/lib/graphql/boolean_type.rb +1 -1
  52. data/lib/graphql/compatibility/execution_specification.rb +1 -0
  53. data/lib/graphql/compatibility/lazy_execution_specification.rb +2 -0
  54. data/lib/graphql/compatibility/query_parser_specification.rb +2 -0
  55. data/lib/graphql/compatibility/schema_parser_specification.rb +2 -0
  56. data/lib/graphql/dataloader/null_dataloader.rb +22 -0
  57. data/lib/graphql/dataloader/request.rb +19 -0
  58. data/lib/graphql/dataloader/request_all.rb +19 -0
  59. data/lib/graphql/dataloader/source.rb +155 -0
  60. data/lib/graphql/dataloader.rb +308 -0
  61. data/lib/graphql/date_encoding_error.rb +16 -0
  62. data/lib/graphql/define/assign_global_id_field.rb +1 -1
  63. data/lib/graphql/define/instance_definable.rb +48 -3
  64. data/lib/graphql/define/type_definer.rb +5 -5
  65. data/lib/graphql/deprecated_dsl.rb +18 -5
  66. data/lib/graphql/deprecation.rb +9 -0
  67. data/lib/graphql/directive/deprecated_directive.rb +1 -1
  68. data/lib/graphql/directive/include_directive.rb +1 -1
  69. data/lib/graphql/directive/skip_directive.rb +1 -1
  70. data/lib/graphql/directive.rb +1 -5
  71. data/lib/graphql/enum_type.rb +9 -3
  72. data/lib/graphql/execution/errors.rb +110 -7
  73. data/lib/graphql/execution/execute.rb +8 -1
  74. data/lib/graphql/execution/interpreter/arguments.rb +57 -5
  75. data/lib/graphql/execution/interpreter/arguments_cache.rb +49 -15
  76. data/lib/graphql/execution/interpreter/handles_raw_value.rb +0 -7
  77. data/lib/graphql/execution/interpreter/resolve.rb +37 -25
  78. data/lib/graphql/execution/interpreter/runtime.rb +670 -294
  79. data/lib/graphql/execution/interpreter.rb +16 -16
  80. data/lib/graphql/execution/lazy.rb +5 -1
  81. data/lib/graphql/execution/lookahead.rb +2 -2
  82. data/lib/graphql/execution/multiplex.rb +39 -23
  83. data/lib/graphql/field.rb +1 -1
  84. data/lib/graphql/float_type.rb +1 -1
  85. data/lib/graphql/function.rb +4 -0
  86. data/lib/graphql/id_type.rb +1 -1
  87. data/lib/graphql/input_object_type.rb +3 -1
  88. data/lib/graphql/int_type.rb +1 -1
  89. data/lib/graphql/integer_decoding_error.rb +17 -0
  90. data/lib/graphql/integer_encoding_error.rb +18 -2
  91. data/lib/graphql/interface_type.rb +4 -2
  92. data/lib/graphql/internal_representation/document.rb +2 -2
  93. data/lib/graphql/internal_representation/rewrite.rb +1 -1
  94. data/lib/graphql/introspection/directive_location_enum.rb +2 -2
  95. data/lib/graphql/introspection/directive_type.rb +11 -5
  96. data/lib/graphql/introspection/entry_points.rb +2 -2
  97. data/lib/graphql/introspection/enum_value_type.rb +2 -2
  98. data/lib/graphql/introspection/field_type.rb +3 -3
  99. data/lib/graphql/introspection/input_value_type.rb +10 -4
  100. data/lib/graphql/introspection/schema_type.rb +10 -5
  101. data/lib/graphql/introspection/type_type.rb +18 -12
  102. data/lib/graphql/introspection.rb +5 -2
  103. data/lib/graphql/invalid_null_error.rb +1 -1
  104. data/lib/graphql/language/block_string.rb +2 -6
  105. data/lib/graphql/language/cache.rb +37 -0
  106. data/lib/graphql/language/document_from_schema_definition.rb +60 -26
  107. data/lib/graphql/language/lexer.rb +50 -28
  108. data/lib/graphql/language/lexer.rl +2 -4
  109. data/lib/graphql/language/nodes.rb +14 -4
  110. data/lib/graphql/language/parser.rb +856 -825
  111. data/lib/graphql/language/parser.y +28 -11
  112. data/lib/graphql/language/printer.rb +10 -1
  113. data/lib/graphql/language/sanitized_printer.rb +5 -5
  114. data/lib/graphql/language/token.rb +0 -4
  115. data/lib/graphql/language.rb +1 -0
  116. data/lib/graphql/name_validator.rb +0 -4
  117. data/lib/graphql/object_type.rb +4 -4
  118. data/lib/graphql/pagination/active_record_relation_connection.rb +47 -3
  119. data/lib/graphql/pagination/connection.rb +19 -1
  120. data/lib/graphql/pagination/connections.rb +45 -30
  121. data/lib/graphql/pagination/relation_connection.rb +69 -28
  122. data/lib/graphql/parse_error.rb +0 -1
  123. data/lib/graphql/query/arguments.rb +2 -2
  124. data/lib/graphql/query/arguments_cache.rb +1 -2
  125. data/lib/graphql/query/context.rb +22 -4
  126. data/lib/graphql/query/executor.rb +0 -1
  127. data/lib/graphql/query/input_validation_result.rb +9 -0
  128. data/lib/graphql/query/literal_input.rb +1 -1
  129. data/lib/graphql/query/null_context.rb +21 -9
  130. data/lib/graphql/query/serial_execution/field_resolution.rb +1 -1
  131. data/lib/graphql/query/serial_execution.rb +1 -0
  132. data/lib/graphql/query/validation_pipeline.rb +3 -4
  133. data/lib/graphql/query/variable_validation_error.rb +3 -3
  134. data/lib/graphql/query/variables.rb +35 -4
  135. data/lib/graphql/query.rb +20 -8
  136. data/lib/graphql/railtie.rb +9 -1
  137. data/lib/graphql/rake_task.rb +3 -0
  138. data/lib/graphql/relay/array_connection.rb +2 -2
  139. data/lib/graphql/relay/base_connection.rb +7 -0
  140. data/lib/graphql/relay/connection_instrumentation.rb +4 -4
  141. data/lib/graphql/relay/connection_type.rb +16 -3
  142. data/lib/graphql/relay/edges_instrumentation.rb +0 -1
  143. data/lib/graphql/relay/global_id_resolve.rb +1 -2
  144. data/lib/graphql/relay/mutation.rb +2 -1
  145. data/lib/graphql/relay/node.rb +3 -0
  146. data/lib/graphql/relay/page_info.rb +1 -1
  147. data/lib/graphql/relay/range_add.rb +14 -5
  148. data/lib/graphql/relay/type_extensions.rb +2 -0
  149. data/lib/graphql/rubocop/graphql/base_cop.rb +36 -0
  150. data/lib/graphql/rubocop/graphql/default_null_true.rb +43 -0
  151. data/lib/graphql/rubocop/graphql/default_required_true.rb +43 -0
  152. data/lib/graphql/rubocop.rb +4 -0
  153. data/lib/graphql/scalar_type.rb +3 -1
  154. data/lib/graphql/schema/addition.rb +247 -0
  155. data/lib/graphql/schema/argument.rb +177 -21
  156. data/lib/graphql/schema/build_from_definition.rb +150 -55
  157. data/lib/graphql/schema/default_type_error.rb +2 -0
  158. data/lib/graphql/schema/directive/feature.rb +1 -1
  159. data/lib/graphql/schema/directive/flagged.rb +57 -0
  160. data/lib/graphql/schema/directive/include.rb +1 -1
  161. data/lib/graphql/schema/directive/skip.rb +1 -1
  162. data/lib/graphql/schema/directive/transform.rb +14 -2
  163. data/lib/graphql/schema/directive.rb +103 -4
  164. data/lib/graphql/schema/enum.rb +72 -11
  165. data/lib/graphql/schema/enum_value.rb +18 -6
  166. data/lib/graphql/schema/field/connection_extension.rb +4 -2
  167. data/lib/graphql/schema/field/scope_extension.rb +1 -1
  168. data/lib/graphql/schema/field.rb +332 -111
  169. data/lib/graphql/schema/field_extension.rb +89 -2
  170. data/lib/graphql/schema/find_inherited_value.rb +4 -1
  171. data/lib/graphql/schema/finder.rb +5 -5
  172. data/lib/graphql/schema/input_object.rb +79 -55
  173. data/lib/graphql/schema/interface.rb +12 -20
  174. data/lib/graphql/schema/introspection_system.rb +1 -1
  175. data/lib/graphql/schema/list.rb +21 -4
  176. data/lib/graphql/schema/loader.rb +11 -0
  177. data/lib/graphql/schema/member/accepts_definition.rb +15 -3
  178. data/lib/graphql/schema/member/base_dsl_methods.rb +5 -16
  179. data/lib/graphql/schema/member/build_type.rb +4 -7
  180. data/lib/graphql/schema/member/cached_graphql_definition.rb +29 -2
  181. data/lib/graphql/schema/member/has_arguments.rb +166 -74
  182. data/lib/graphql/schema/member/has_deprecation_reason.rb +25 -0
  183. data/lib/graphql/schema/member/has_directives.rb +98 -0
  184. data/lib/graphql/schema/member/has_fields.rb +77 -22
  185. data/lib/graphql/schema/member/has_interfaces.rb +100 -0
  186. data/lib/graphql/schema/member/has_validators.rb +31 -0
  187. data/lib/graphql/schema/member/instrumentation.rb +0 -1
  188. data/lib/graphql/schema/member/type_system_helpers.rb +1 -1
  189. data/lib/graphql/schema/member/validates_input.rb +2 -2
  190. data/lib/graphql/schema/member.rb +5 -0
  191. data/lib/graphql/schema/middleware_chain.rb +1 -1
  192. data/lib/graphql/schema/non_null.rb +9 -3
  193. data/lib/graphql/schema/object.rb +40 -80
  194. data/lib/graphql/schema/printer.rb +16 -20
  195. data/lib/graphql/schema/relay_classic_mutation.rb +38 -4
  196. data/lib/graphql/schema/resolver/has_payload_type.rb +29 -2
  197. data/lib/graphql/schema/resolver.rb +110 -64
  198. data/lib/graphql/schema/scalar.rb +18 -2
  199. data/lib/graphql/schema/subscription.rb +55 -9
  200. data/lib/graphql/schema/timeout_middleware.rb +3 -1
  201. data/lib/graphql/schema/traversal.rb +1 -1
  202. data/lib/graphql/schema/type_expression.rb +1 -1
  203. data/lib/graphql/schema/type_membership.rb +18 -4
  204. data/lib/graphql/schema/union.rb +8 -1
  205. data/lib/graphql/schema/validation.rb +4 -2
  206. data/lib/graphql/schema/validator/allow_blank_validator.rb +29 -0
  207. data/lib/graphql/schema/validator/allow_null_validator.rb +26 -0
  208. data/lib/graphql/schema/validator/exclusion_validator.rb +33 -0
  209. data/lib/graphql/schema/validator/format_validator.rb +48 -0
  210. data/lib/graphql/schema/validator/inclusion_validator.rb +35 -0
  211. data/lib/graphql/schema/validator/length_validator.rb +59 -0
  212. data/lib/graphql/schema/validator/numericality_validator.rb +82 -0
  213. data/lib/graphql/schema/validator/required_validator.rb +82 -0
  214. data/lib/graphql/schema/validator.rb +171 -0
  215. data/lib/graphql/schema/warden.rb +126 -53
  216. data/lib/graphql/schema.rb +262 -281
  217. data/lib/graphql/static_validation/all_rules.rb +2 -0
  218. data/lib/graphql/static_validation/base_visitor.rb +9 -6
  219. data/lib/graphql/static_validation/definition_dependencies.rb +0 -1
  220. data/lib/graphql/static_validation/error.rb +3 -1
  221. data/lib/graphql/static_validation/literal_validator.rb +1 -1
  222. data/lib/graphql/static_validation/rules/argument_literals_are_compatible.rb +4 -2
  223. data/lib/graphql/static_validation/rules/argument_literals_are_compatible_error.rb +6 -2
  224. data/lib/graphql/static_validation/rules/arguments_are_defined.rb +3 -2
  225. data/lib/graphql/static_validation/rules/arguments_are_defined_error.rb +4 -2
  226. data/lib/graphql/static_validation/rules/directives_are_defined.rb +1 -1
  227. data/lib/graphql/static_validation/rules/directives_are_in_valid_locations.rb +1 -1
  228. data/lib/graphql/static_validation/rules/fields_will_merge.rb +90 -47
  229. data/lib/graphql/static_validation/rules/fields_will_merge_error.rb +25 -4
  230. data/lib/graphql/static_validation/rules/fragments_are_finite.rb +2 -2
  231. data/lib/graphql/static_validation/rules/input_object_names_are_unique.rb +30 -0
  232. data/lib/graphql/static_validation/rules/input_object_names_are_unique_error.rb +30 -0
  233. data/lib/graphql/static_validation/rules/query_root_exists.rb +17 -0
  234. data/lib/graphql/static_validation/rules/query_root_exists_error.rb +26 -0
  235. data/lib/graphql/static_validation/rules/required_arguments_are_present.rb +4 -2
  236. data/lib/graphql/static_validation/rules/required_input_object_attributes_are_present.rb +5 -5
  237. data/lib/graphql/static_validation/rules/unique_directives_per_location.rb +1 -1
  238. data/lib/graphql/static_validation/rules/variable_usages_are_allowed.rb +14 -8
  239. data/lib/graphql/static_validation/validation_context.rb +12 -2
  240. data/lib/graphql/static_validation/validation_timeout_error.rb +25 -0
  241. data/lib/graphql/static_validation/validator.rb +41 -10
  242. data/lib/graphql/static_validation.rb +1 -0
  243. data/lib/graphql/string_encoding_error.rb +13 -3
  244. data/lib/graphql/string_type.rb +1 -1
  245. data/lib/graphql/subscriptions/action_cable_subscriptions.rb +39 -8
  246. data/lib/graphql/subscriptions/broadcast_analyzer.rb +0 -3
  247. data/lib/graphql/subscriptions/event.rb +68 -32
  248. data/lib/graphql/subscriptions/instrumentation.rb +0 -1
  249. data/lib/graphql/subscriptions/serialize.rb +34 -5
  250. data/lib/graphql/subscriptions/subscription_root.rb +1 -1
  251. data/lib/graphql/subscriptions.rb +34 -39
  252. data/lib/graphql/tracing/active_support_notifications_tracing.rb +8 -21
  253. data/lib/graphql/tracing/appoptics_tracing.rb +3 -1
  254. data/lib/graphql/tracing/appsignal_tracing.rb +15 -0
  255. data/lib/graphql/tracing/data_dog_tracing.rb +24 -2
  256. data/lib/graphql/tracing/notifications_tracing.rb +59 -0
  257. data/lib/graphql/tracing/platform_tracing.rb +24 -12
  258. data/lib/graphql/tracing/prometheus_tracing/graphql_collector.rb +4 -1
  259. data/lib/graphql/tracing/skylight_tracing.rb +1 -1
  260. data/lib/graphql/tracing.rb +2 -2
  261. data/lib/graphql/types/big_int.rb +5 -1
  262. data/lib/graphql/types/int.rb +10 -3
  263. data/lib/graphql/types/iso_8601_date.rb +13 -5
  264. data/lib/graphql/types/iso_8601_date_time.rb +8 -1
  265. data/lib/graphql/types/relay/base_connection.rb +6 -91
  266. data/lib/graphql/types/relay/base_edge.rb +2 -34
  267. data/lib/graphql/types/relay/connection_behaviors.rb +174 -0
  268. data/lib/graphql/types/relay/default_relay.rb +31 -0
  269. data/lib/graphql/types/relay/edge_behaviors.rb +64 -0
  270. data/lib/graphql/types/relay/has_node_field.rb +41 -0
  271. data/lib/graphql/types/relay/has_nodes_field.rb +41 -0
  272. data/lib/graphql/types/relay/node.rb +2 -4
  273. data/lib/graphql/types/relay/node_behaviors.rb +15 -0
  274. data/lib/graphql/types/relay/node_field.rb +3 -22
  275. data/lib/graphql/types/relay/nodes_field.rb +16 -18
  276. data/lib/graphql/types/relay/page_info.rb +2 -14
  277. data/lib/graphql/types/relay/page_info_behaviors.rb +25 -0
  278. data/lib/graphql/types/relay.rb +11 -3
  279. data/lib/graphql/types/string.rb +8 -2
  280. data/lib/graphql/unauthorized_error.rb +1 -1
  281. data/lib/graphql/union_type.rb +3 -1
  282. data/lib/graphql/upgrader/member.rb +1 -0
  283. data/lib/graphql/upgrader/schema.rb +1 -0
  284. data/lib/graphql/version.rb +1 -1
  285. data/lib/graphql.rb +68 -37
  286. data/readme.md +3 -6
  287. metadata +83 -113
  288. data/lib/graphql/execution/interpreter/hash_response.rb +0 -46
  289. data/lib/graphql/types/relay/base_field.rb +0 -22
  290. data/lib/graphql/types/relay/base_interface.rb +0 -29
  291. data/lib/graphql/types/relay/base_object.rb +0 -26
  292. /data/lib/generators/graphql/{templates → install/templates}/base_mutation.erb +0 -0
  293. /data/lib/generators/graphql/{templates → install/templates}/mutation_type.erb +0 -0
@@ -0,0 +1,36 @@
1
+ # frozen_string_literal: true
2
+ require "rubocop"
3
+
4
+ module GraphQL
5
+ module Rubocop
6
+ module GraphQL
7
+ class BaseCop < RuboCop::Cop::Base
8
+ extend RuboCop::Cop::AutoCorrector
9
+
10
+ # Return the source of `send_node`, but without the keyword argument represented by `pair_node`
11
+ def source_without_keyword_argument(send_node, pair_node)
12
+ # work back to the preceeding comma
13
+ first_pos = pair_node.location.expression.begin_pos
14
+ end_pos = pair_node.location.expression.end_pos
15
+ node_source = send_node.source_range.source
16
+ node_first_pos = send_node.location.expression.begin_pos
17
+
18
+ relative_first_pos = first_pos - node_first_pos
19
+ relative_last_pos = end_pos - node_first_pos
20
+
21
+ begin_removal_pos = relative_first_pos
22
+ while node_source[begin_removal_pos] != ","
23
+ begin_removal_pos -= 1
24
+ if begin_removal_pos < 1
25
+ raise "Invariant: somehow backtracked to beginning of node looking for a comma (node source: #{node_source.inspect})"
26
+ end
27
+ end
28
+
29
+ end_removal_pos = relative_last_pos
30
+ cleaned_node_source = node_source[0...begin_removal_pos] + node_source[end_removal_pos..-1]
31
+ cleaned_node_source
32
+ end
33
+ end
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,43 @@
1
+ # frozen_string_literal: true
2
+ require_relative "base_cop"
3
+
4
+ module GraphQL
5
+ module Rubocop
6
+ module GraphQL
7
+ # Identify (and auto-correct) any field configuration which duplicates
8
+ # the default `null: true` property.
9
+ #
10
+ # `null: true` is default because nullable fields can always be converted
11
+ # to non-null fields (`null: false`) without a breaking change. (The opposite change, from `null: false`
12
+ # to `null: true`, change.)
13
+ #
14
+ # @example
15
+ # # Both of these define `name: String` in GraphQL:
16
+ #
17
+ # # bad
18
+ # field :name, String, null: true
19
+ #
20
+ # # good
21
+ # field :name, String
22
+ #
23
+ class DefaultNullTrue < BaseCop
24
+ MSG = "`null: true` is the default and can be removed."
25
+
26
+ def_node_matcher :field_config_with_null_true?, <<-Pattern
27
+ (
28
+ send nil? :field ... (hash $(pair (sym :null) (true)) ...)
29
+ )
30
+ Pattern
31
+
32
+ def on_send(node)
33
+ field_config_with_null_true?(node) do |null_config|
34
+ add_offense(null_config) do |corrector|
35
+ cleaned_node_source = source_without_keyword_argument(node, null_config)
36
+ corrector.replace(node.source_range, cleaned_node_source)
37
+ end
38
+ end
39
+ end
40
+ end
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,43 @@
1
+ # frozen_string_literal: true
2
+ require_relative "./base_cop"
3
+
4
+ module GraphQL
5
+ module Rubocop
6
+ module GraphQL
7
+ # Identify (and auto-correct) any argument configuration which duplicates
8
+ # the default `required: true` property.
9
+ #
10
+ # `required: true` is default because required arguments can always be converted
11
+ # to optional arguments (`required: false`) without a breaking change. (The opposite change, from `required: false`
12
+ # to `required: true`, change.)
13
+ #
14
+ # @example
15
+ # # Both of these define `id: ID!` in GraphQL:
16
+ #
17
+ # # bad
18
+ # argument :id, ID, required: true
19
+ #
20
+ # # good
21
+ # argument :id, ID
22
+ #
23
+ class DefaultRequiredTrue < BaseCop
24
+ MSG = "`required: true` is the default and can be removed."
25
+
26
+ def_node_matcher :argument_config_with_required_true?, <<-Pattern
27
+ (
28
+ send {nil? _} :argument ... (hash <$(pair (sym :required) (true)) ...>)
29
+ )
30
+ Pattern
31
+
32
+ def on_send(node)
33
+ argument_config_with_required_true?(node) do |required_config|
34
+ add_offense(required_config) do |corrector|
35
+ cleaned_node_source = source_without_keyword_argument(node, required_config)
36
+ corrector.replace(node, cleaned_node_source)
37
+ end
38
+ end
39
+ end
40
+ end
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,4 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "graphql/rubocop/graphql/default_null_true"
4
+ require "graphql/rubocop/graphql/default_required_true"
@@ -2,7 +2,9 @@
2
2
  module GraphQL
3
3
  # @api deprecated
4
4
  class ScalarType < GraphQL::BaseType
5
- accepts_definitions :coerce, :coerce_input, :coerce_result
5
+ extend Define::InstanceDefinable::DeprecatedDefine
6
+
7
+ deprecated_accepts_definitions :coerce, :coerce_input, :coerce_result
6
8
  ensure_defined :coerce_non_null_input, :coerce_result
7
9
 
8
10
  module NoOpCoerce
@@ -0,0 +1,247 @@
1
+ # frozen_string_literal: true
2
+
3
+ module GraphQL
4
+ class Schema
5
+ class Addition
6
+ attr_reader :directives, :possible_types, :types, :union_memberships, :references, :arguments_with_default_values
7
+
8
+ def initialize(schema:, own_types:, new_types:)
9
+ @schema = schema
10
+ @own_types = own_types
11
+ @directives = Set.new
12
+ @possible_types = {}
13
+ @types = {}
14
+ @union_memberships = {}
15
+ @references = Hash.new { |h, k| h[k] = [] }
16
+ @arguments_with_default_values = []
17
+ add_type_and_traverse(new_types)
18
+ end
19
+
20
+ private
21
+
22
+ def references_to(thing, from:)
23
+ @references[thing] << from
24
+ end
25
+
26
+ def get_type(name)
27
+ local_type = @types[name]
28
+ # This isn't really sophisticated, but
29
+ # I think it's good enough to support the current usage of LateBoundTypes
30
+ if local_type.is_a?(Array)
31
+ local_type = local_type.first
32
+ end
33
+ local_type || @schema.get_type(name)
34
+ end
35
+
36
+ # Lookup using `own_types` here because it's ok to override
37
+ # inherited types by name
38
+ def get_local_type(name)
39
+ @types[name] || @own_types[name]
40
+ end
41
+
42
+ def add_directives_from(owner)
43
+ dirs = owner.directives.map(&:class)
44
+ @directives.merge(dirs)
45
+ add_type_and_traverse(dirs)
46
+ end
47
+
48
+ def add_type_and_traverse(new_types)
49
+ late_types = []
50
+ new_types.each { |t| add_type(t, owner: nil, late_types: late_types, path: [t.graphql_name]) }
51
+ missed_late_types = 0
52
+ while (late_type_vals = late_types.shift)
53
+ type_owner, lt = late_type_vals
54
+ if lt.is_a?(String)
55
+ type = Member::BuildType.constantize(lt)
56
+ # Reset the counter, since we might succeed next go-round
57
+ missed_late_types = 0
58
+ update_type_owner(type_owner, type)
59
+ add_type(type, owner: type_owner, late_types: late_types, path: [type.graphql_name])
60
+ elsif lt.is_a?(LateBoundType)
61
+ if (type = get_type(lt.name))
62
+ # Reset the counter, since we might succeed next go-round
63
+ missed_late_types = 0
64
+ update_type_owner(type_owner, type)
65
+ add_type(type, owner: type_owner, late_types: late_types, path: [type.graphql_name])
66
+ else
67
+ missed_late_types += 1
68
+ # Add it back to the list, maybe we'll be able to resolve it later.
69
+ late_types << [type_owner, lt]
70
+ if missed_late_types == late_types.size
71
+ # We've looked at all of them and haven't resolved one.
72
+ raise UnresolvedLateBoundTypeError.new(type: lt)
73
+ else
74
+ # Try the next one
75
+ end
76
+ end
77
+ else
78
+ raise ArgumentError, "Unexpected late type: #{lt.inspect}"
79
+ end
80
+ end
81
+ nil
82
+ end
83
+
84
+ def update_type_owner(owner, type)
85
+ case owner
86
+ when Module
87
+ if owner.kind.union?
88
+ # It's a union with possible_types
89
+ # Replace the item by class name
90
+ owner.assign_type_membership_object_type(type)
91
+ @possible_types[owner.graphql_name] = owner.possible_types
92
+ elsif type.kind.interface? && (owner.kind.object? || owner.kind.interface?)
93
+ new_interfaces = []
94
+ owner.interfaces.each do |int_t|
95
+ if int_t.is_a?(String) && int_t == type.graphql_name
96
+ new_interfaces << type
97
+ elsif int_t.is_a?(LateBoundType) && int_t.graphql_name == type.graphql_name
98
+ new_interfaces << type
99
+ else
100
+ # Don't re-add proper interface definitions,
101
+ # they were probably already added, maybe with options.
102
+ end
103
+ end
104
+ owner.implements(*new_interfaces)
105
+ new_interfaces.each do |int|
106
+ pt = @possible_types[int.graphql_name] ||= []
107
+ if !pt.include?(owner) && owner.is_a?(Class)
108
+ pt << owner
109
+ end
110
+ end
111
+ end
112
+ when nil
113
+ # It's a root type
114
+ @types[type.graphql_name] = type
115
+ when GraphQL::Schema::Field, GraphQL::Schema::Argument
116
+ orig_type = owner.type
117
+ # Apply list/non-null wrapper as needed
118
+ if orig_type.respond_to?(:of_type)
119
+ transforms = []
120
+ while (orig_type.respond_to?(:of_type))
121
+ if orig_type.kind.non_null?
122
+ transforms << :to_non_null_type
123
+ elsif orig_type.kind.list?
124
+ transforms << :to_list_type
125
+ else
126
+ raise "Invariant: :of_type isn't non-null or list"
127
+ end
128
+ orig_type = orig_type.of_type
129
+ end
130
+ transforms.reverse_each { |t| type = type.public_send(t) }
131
+ end
132
+ owner.type = type
133
+ else
134
+ raise "Unexpected update: #{owner.inspect} #{type.inspect}"
135
+ end
136
+ end
137
+
138
+ def add_type(type, owner:, late_types:, path:)
139
+ if type.respond_to?(:metadata) && type.metadata.is_a?(Hash)
140
+ type_class = type.metadata[:type_class]
141
+ if type_class.nil?
142
+ raise ArgumentError, "Can't add legacy type: #{type} (#{type.class})"
143
+ else
144
+ type = type_class
145
+ end
146
+ elsif type.is_a?(String) || type.is_a?(GraphQL::Schema::LateBoundType)
147
+ late_types << [owner, type]
148
+ return
149
+ end
150
+
151
+ if owner.is_a?(Class) && owner < GraphQL::Schema::Union
152
+ um = @union_memberships[type.graphql_name] ||= []
153
+ um << owner
154
+ end
155
+
156
+ if (prev_type = get_local_type(type.graphql_name)) && prev_type == type
157
+ # No need to re-visit
158
+ elsif type.is_a?(Class) && type < GraphQL::Schema::Directive
159
+ @directives << type
160
+ type.all_argument_definitions.each do |arg|
161
+ arg_type = arg.type.unwrap
162
+ references_to(arg_type, from: arg)
163
+ add_type(arg_type, owner: arg, late_types: late_types, path: path + [arg.graphql_name])
164
+ if arg.default_value?
165
+ @arguments_with_default_values << arg
166
+ end
167
+ end
168
+ else
169
+ prev_type = @types[type.graphql_name]
170
+ if prev_type.nil?
171
+ @types[type.graphql_name] = type
172
+ elsif prev_type.is_a?(Array)
173
+ prev_type << type
174
+ else
175
+ @types[type.graphql_name] = [prev_type, type]
176
+ end
177
+
178
+ add_directives_from(type)
179
+ if type.kind.fields?
180
+ type.all_field_definitions.each do |field|
181
+ name = field.graphql_name
182
+ field_type = field.type.unwrap
183
+ references_to(field_type, from: field)
184
+ field_path = path + [name]
185
+ add_type(field_type, owner: field, late_types: late_types, path: field_path)
186
+ add_directives_from(field)
187
+ field.all_argument_definitions.each do |arg|
188
+ add_directives_from(arg)
189
+ arg_type = arg.type.unwrap
190
+ references_to(arg_type, from: arg)
191
+ add_type(arg_type, owner: arg, late_types: late_types, path: field_path + [arg.graphql_name])
192
+ if arg.default_value?
193
+ @arguments_with_default_values << arg
194
+ end
195
+ end
196
+ end
197
+ end
198
+ if type.kind.input_object?
199
+ type.all_argument_definitions.each do |arg|
200
+ add_directives_from(arg)
201
+ arg_type = arg.type.unwrap
202
+ references_to(arg_type, from: arg)
203
+ add_type(arg_type, owner: arg, late_types: late_types, path: path + [arg.graphql_name])
204
+ if arg.default_value?
205
+ @arguments_with_default_values << arg
206
+ end
207
+ end
208
+ end
209
+ if type.kind.union?
210
+ @possible_types[type.graphql_name] = type.all_possible_types
211
+ type.all_possible_types.each do |t|
212
+ add_type(t, owner: type, late_types: late_types, path: path + ["possible_types"])
213
+ end
214
+ end
215
+ if type.kind.interface?
216
+ type.orphan_types.each do |t|
217
+ add_type(t, owner: type, late_types: late_types, path: path + ["orphan_types"])
218
+ end
219
+ end
220
+ if type.kind.object?
221
+ possible_types_for_this_name = @possible_types[type.graphql_name] ||= []
222
+ possible_types_for_this_name << type
223
+ end
224
+
225
+ if type.kind.object? || type.kind.interface?
226
+ type.interface_type_memberships.each do |interface_type_membership|
227
+ case interface_type_membership
228
+ when Schema::TypeMembership
229
+ interface_type = interface_type_membership.abstract_type
230
+ # We can get these now; we'll have to get late-bound types later
231
+ if interface_type.is_a?(Module) && type.is_a?(Class)
232
+ implementers = @possible_types[interface_type.graphql_name] ||= []
233
+ implementers << type
234
+ end
235
+ when String, Schema::LateBoundType
236
+ interface_type = interface_type_membership
237
+ else
238
+ raise ArgumentError, "Invariant: unexpected type membership for #{type.graphql_name}: #{interface_type_membership.class} (#{interface_type_membership.inspect})"
239
+ end
240
+ add_type(interface_type, owner: type, late_types: late_types, path: path + ["implements"])
241
+ end
242
+ end
243
+ end
244
+ end
245
+ end
246
+ end
247
+ end
@@ -2,14 +2,14 @@
2
2
  module GraphQL
3
3
  class Schema
4
4
  class Argument
5
- if !String.method_defined?(:-@)
6
- using GraphQL::StringDedupBackport
7
- end
8
-
9
5
  include GraphQL::Schema::Member::CachedGraphQLDefinition
10
6
  include GraphQL::Schema::Member::AcceptsDefinition
11
7
  include GraphQL::Schema::Member::HasPath
12
8
  include GraphQL::Schema::Member::HasAstNode
9
+ include GraphQL::Schema::Member::HasDirectives
10
+ include GraphQL::Schema::Member::HasDeprecationReason
11
+ include GraphQL::Schema::Member::HasValidators
12
+ include GraphQL::Schema::FindInheritedValue::EmptyObjects
13
13
 
14
14
  NO_DEFAULT = :__no_default__
15
15
 
@@ -37,7 +37,7 @@ module GraphQL
37
37
  # @param arg_name [Symbol]
38
38
  # @param type_expr
39
39
  # @param desc [String]
40
- # @param required [Boolean] if true, this argument is non-null; if false, this argument is nullable
40
+ # @param required [Boolean, :nullable] if true, this argument is non-null; if false, this argument is nullable. If `:nullable`, then the argument must be provided, though it may be `null`.
41
41
  # @param description [String]
42
42
  # @param default_value [Object]
43
43
  # @param as [Symbol] Override the keyword name when passed to a method
@@ -45,14 +45,24 @@ module GraphQL
45
45
  # @param camelize [Boolean] if true, the name will be camelized when building the schema
46
46
  # @param from_resolver [Boolean] if true, a Resolver class defined this argument
47
47
  # @param method_access [Boolean] If false, don't build method access on legacy {Query::Arguments} instances.
48
+ # @param directives [Hash{Class => Hash}]
48
49
  # @param deprecation_reason [String]
49
- def initialize(arg_name = nil, type_expr = nil, desc = nil, required:, type: nil, name: nil, loads: nil, description: nil, ast_node: nil, default_value: NO_DEFAULT, as: nil, from_resolver: false, camelize: true, prepare: nil, method_access: true, owner:, deprecation_reason: nil, &definition_block)
50
+ # @param validates [Hash, nil] Options for building validators, if any should be applied
51
+ # @param replace_null_with_default [Boolean] if `true`, incoming values of `null` will be replaced with the configured `default_value`
52
+ def initialize(arg_name = nil, type_expr = nil, desc = nil, required: true, type: nil, name: nil, loads: nil, description: nil, ast_node: nil, default_value: NO_DEFAULT, as: nil, from_resolver: false, camelize: true, prepare: nil, method_access: true, owner:, validates: nil, directives: nil, deprecation_reason: nil, replace_null_with_default: false, &definition_block)
50
53
  arg_name ||= name
51
54
  @name = -(camelize ? Member::BuildType.camelize(arg_name.to_s) : arg_name.to_s)
52
55
  @type_expr = type_expr || type
53
56
  @description = desc || description
54
- @null = !required
57
+ @null = required != true
55
58
  @default_value = default_value
59
+ if replace_null_with_default
60
+ if !default_value?
61
+ raise ArgumentError, "`replace_null_with_default: true` requires a default value, please provide one with `default_value: ...`"
62
+ end
63
+ @replace_null_with_default = true
64
+ end
65
+
56
66
  @owner = owner
57
67
  @as = as
58
68
  @loads = loads
@@ -63,6 +73,17 @@ module GraphQL
63
73
  @method_access = method_access
64
74
  self.deprecation_reason = deprecation_reason
65
75
 
76
+ if directives
77
+ directives.each do |dir_class, dir_options|
78
+ directive(dir_class, **dir_options)
79
+ end
80
+ end
81
+
82
+ self.validates(validates)
83
+ if required == :nullable
84
+ self.owner.validates(required: { argument: arg_name })
85
+ end
86
+
66
87
  if definition_block
67
88
  if definition_block.arity == 1
68
89
  instance_exec(self, &definition_block)
@@ -72,6 +93,10 @@ module GraphQL
72
93
  end
73
94
  end
74
95
 
96
+ def inspect
97
+ "#<#{self.class} #{path}: #{type.to_type_signature}#{description ? " @description=#{description.inspect}" : ""}>"
98
+ end
99
+
75
100
  # @return [Object] the value used when the client doesn't provide a value for this argument
76
101
  attr_reader :default_value
77
102
 
@@ -80,6 +105,10 @@ module GraphQL
80
105
  @default_value != NO_DEFAULT
81
106
  end
82
107
 
108
+ def replace_null_with_default?
109
+ @replace_null_with_default
110
+ end
111
+
83
112
  attr_writer :description
84
113
 
85
114
  # @return [String] Documentation for this argument
@@ -94,14 +123,16 @@ module GraphQL
94
123
  # @return [String] Deprecation reason for this argument
95
124
  def deprecation_reason(text = nil)
96
125
  if text
97
- validate_deprecated_or_optional(null: @null, deprecation_reason: text)
98
- @deprecation_reason = text
126
+ self.deprecation_reason = text
99
127
  else
100
- @deprecation_reason
128
+ super()
101
129
  end
102
130
  end
103
131
 
104
- alias_method :deprecation_reason=, :deprecation_reason
132
+ def deprecation_reason=(new_reason)
133
+ validate_deprecated_or_optional(null: @null, deprecation_reason: new_reason)
134
+ super
135
+ end
105
136
 
106
137
  def visible?(context)
107
138
  true
@@ -131,20 +162,15 @@ module GraphQL
131
162
  end
132
163
  end
133
164
  elsif as_type.kind.input_object?
134
- as_type.arguments.each do |_name, input_obj_arg|
135
- input_obj_arg = input_obj_arg.type_class
136
- # TODO: this skips input objects whose values were alread replaced with application objects.
137
- # See: https://github.com/rmosolgo/graphql-ruby/issues/2633
138
- if value.respond_to?(:key?) && value.key?(input_obj_arg.keyword) && !input_obj_arg.authorized?(obj, value[input_obj_arg.keyword], ctx)
139
- return false
140
- end
141
- end
165
+ return as_type.authorized?(obj, value, ctx)
142
166
  end
143
167
  # None of the early-return conditions were activated,
144
168
  # so this is authorized.
145
169
  true
146
170
  end
147
171
 
172
+ prepend Schema::Member::CachedGraphQLDefinition::DeprecatedToGraphQL
173
+
148
174
  def to_graphql
149
175
  argument = GraphQL::Argument.new
150
176
  argument.name = @name
@@ -157,8 +183,8 @@ module GraphQL
157
183
  if NO_DEFAULT != @default_value
158
184
  argument.default_value = @default_value
159
185
  end
160
- if @deprecation_reason
161
- argument.deprecation_reason = @deprecation_reason
186
+ if self.deprecation_reason
187
+ argument.deprecation_reason = self.deprecation_reason
162
188
  end
163
189
  argument
164
190
  end
@@ -199,6 +225,8 @@ module GraphQL
199
225
  value = value.prepare
200
226
  end
201
227
 
228
+ Schema::Validator.validate!(validators, obj, context, value)
229
+
202
230
  if @prepare.nil?
203
231
  value
204
232
  elsif @prepare.is_a?(String) || @prepare.is_a?(Symbol)
@@ -218,6 +246,134 @@ module GraphQL
218
246
  end
219
247
  end
220
248
 
249
+ # @api private
250
+ def coerce_into_values(parent_object, values, context, argument_values)
251
+ arg_name = graphql_name
252
+ arg_key = keyword
253
+ default_used = false
254
+
255
+ if values.key?(arg_name)
256
+ value = values[arg_name]
257
+ elsif values.key?(arg_key)
258
+ value = values[arg_key]
259
+ elsif default_value?
260
+ value = default_value
261
+ default_used = true
262
+ else
263
+ # no value at all
264
+ owner.validate_directive_argument(self, nil)
265
+ return
266
+ end
267
+
268
+ if value.nil? && replace_null_with_default?
269
+ value = default_value
270
+ default_used = true
271
+ end
272
+
273
+ loaded_value = nil
274
+ coerced_value = context.schema.error_handler.with_error_handling(context) do
275
+ type.coerce_input(value, context)
276
+ end
277
+
278
+ # If this isn't lazy, then the block returns eagerly and assigns the result here
279
+ # If it _is_ lazy, then we write the lazy to the hash, then update it later
280
+ argument_values[arg_key] = context.schema.after_lazy(coerced_value) do |resolved_coerced_value|
281
+ if loads && !from_resolver?
282
+ loaded_value = context.query.with_error_handling do
283
+ load_and_authorize_value(owner, coerced_value, context)
284
+ end
285
+ end
286
+
287
+ maybe_loaded_value = loaded_value || resolved_coerced_value
288
+ context.schema.after_lazy(maybe_loaded_value) do |resolved_loaded_value|
289
+ owner.validate_directive_argument(self, resolved_loaded_value)
290
+ prepared_value = context.schema.error_handler.with_error_handling(context) do
291
+ prepare_value(parent_object, resolved_loaded_value, context: context)
292
+ end
293
+
294
+ # TODO code smell to access such a deeply-nested constant in a distant module
295
+ argument_values[arg_key] = GraphQL::Execution::Interpreter::ArgumentValue.new(
296
+ value: prepared_value,
297
+ definition: self,
298
+ default_used: default_used,
299
+ )
300
+ end
301
+ end
302
+ end
303
+
304
+ def load_and_authorize_value(load_method_owner, coerced_value, context)
305
+ if coerced_value.nil?
306
+ return nil
307
+ end
308
+ arg_load_method = "load_#{keyword}"
309
+ if load_method_owner.respond_to?(arg_load_method)
310
+ custom_loaded_value = if load_method_owner.is_a?(Class)
311
+ load_method_owner.public_send(arg_load_method, coerced_value, context)
312
+ else
313
+ load_method_owner.public_send(arg_load_method, coerced_value)
314
+ end
315
+ context.schema.after_lazy(custom_loaded_value) do |custom_value|
316
+ if loads
317
+ if type.list?
318
+ loaded_values = custom_value.each_with_index.map { |custom_val, idx|
319
+ id = coerced_value[idx]
320
+ load_method_owner.authorize_application_object(self, id, context, custom_val)
321
+ }
322
+ context.schema.after_any_lazies(loaded_values, &:itself)
323
+ else
324
+ load_method_owner.authorize_application_object(self, coerced_value, context, custom_loaded_value)
325
+ end
326
+ else
327
+ custom_value
328
+ end
329
+ end
330
+ elsif loads
331
+ if type.list?
332
+ loaded_values = coerced_value.map { |val| load_method_owner.load_and_authorize_application_object(self, val, context) }
333
+ context.schema.after_any_lazies(loaded_values, &:itself)
334
+ else
335
+ load_method_owner.load_and_authorize_application_object(self, coerced_value, context)
336
+ end
337
+ else
338
+ coerced_value
339
+ end
340
+ end
341
+
342
+ # @api private
343
+ def validate_default_value
344
+ coerced_default_value = begin
345
+ # This is weird, but we should accept single-item default values for list-type arguments.
346
+ # If we used `coerce_isolated_input` below, it would do this for us, but it's not really
347
+ # the right thing here because we expect default values in application format (Ruby values)
348
+ # not GraphQL format (scalar values).
349
+ #
350
+ # But I don't think Schema::List#coerce_result should apply wrapping to single-item lists.
351
+ prepped_default_value = if default_value.nil?
352
+ nil
353
+ elsif (type.kind.list? || (type.kind.non_null? && type.of_type.list?)) && !default_value.respond_to?(:map)
354
+ [default_value]
355
+ else
356
+ default_value
357
+ end
358
+
359
+ type.coerce_isolated_result(prepped_default_value) unless prepped_default_value.nil?
360
+ rescue GraphQL::Schema::Enum::UnresolvedValueError
361
+ # It raises this, which is helpful at runtime, but not here...
362
+ default_value
363
+ end
364
+ res = type.valid_isolated_input?(coerced_default_value)
365
+ if !res
366
+ raise InvalidDefaultValueError.new(self)
367
+ end
368
+ end
369
+
370
+ class InvalidDefaultValueError < GraphQL::Error
371
+ def initialize(argument)
372
+ message = "`#{argument.path}` has an invalid default value: `#{argument.default_value.inspect}` isn't accepted by `#{argument.type.to_type_signature}`; update the default value or the argument type."
373
+ super(message)
374
+ end
375
+ end
376
+
221
377
  private
222
378
 
223
379
  def validate_input_type(input_type)