graphql 2.6.13 → 2.6.15

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.
Files changed (133) hide show
  1. checksums.yaml +4 -4
  2. data/lib/generators/graphql/templates/graphql_controller.erb +7 -1
  3. data/lib/graphql/analysis/max_query_complexity.rb +3 -2
  4. data/lib/graphql/analysis/max_query_depth.rb +2 -1
  5. data/lib/graphql/analysis/query_complexity.rb +2 -1
  6. data/lib/graphql/analysis/query_depth.rb +1 -1
  7. data/lib/graphql/analysis/visitor.rb +14 -2
  8. data/lib/graphql/analysis.rb +4 -5
  9. data/lib/graphql/analysis_error.rb +1 -1
  10. data/lib/graphql/coercion_error.rb +1 -1
  11. data/lib/graphql/current.rb +5 -1
  12. data/lib/graphql/dashboard.rb +1 -1
  13. data/lib/graphql/date_encoding_error.rb +3 -1
  14. data/lib/graphql/duration_encoding_error.rb +3 -1
  15. data/lib/graphql/execution/directive_checks.rb +2 -1
  16. data/lib/graphql/execution/errors.rb +18 -7
  17. data/lib/graphql/execution/field_resolve_step.rb +245 -159
  18. data/lib/graphql/execution/input_values.rb +162 -120
  19. data/lib/graphql/execution/interpreter/runtime.rb +9 -5
  20. data/lib/graphql/execution/lookahead.rb +19 -8
  21. data/lib/graphql/execution/next.rb +1 -0
  22. data/lib/graphql/execution/prepare_object_step.rb +37 -13
  23. data/lib/graphql/execution/runner.rb +19 -12
  24. data/lib/graphql/execution/selections_step.rb +1 -1
  25. data/lib/graphql/execution/variable_values.rb +248 -0
  26. data/lib/graphql/float_decoding_error.rb +3 -1
  27. data/lib/graphql/integer_decoding_error.rb +3 -1
  28. data/lib/graphql/introspection/base_object.rb +1 -1
  29. data/lib/graphql/introspection/directive_location_enum.rb +1 -1
  30. data/lib/graphql/introspection/directive_type.rb +3 -1
  31. data/lib/graphql/introspection/dynamic_fields.rb +3 -1
  32. data/lib/graphql/introspection/enum_value_type.rb +3 -1
  33. data/lib/graphql/introspection/field_type.rb +5 -1
  34. data/lib/graphql/introspection/type_kind_enum.rb +1 -1
  35. data/lib/graphql/introspection.rb +2 -1
  36. data/lib/graphql/language/comment.rb +2 -1
  37. data/lib/graphql/language/generation.rb +2 -1
  38. data/lib/graphql/language/lexer.rb +30 -22
  39. data/lib/graphql/language/parser.rb +15 -8
  40. data/lib/graphql/language.rb +1 -1
  41. data/lib/graphql/name_validator.rb +2 -1
  42. data/lib/graphql/pagination/relation_connection.rb +21 -10
  43. data/lib/graphql/pagination.rb +1 -1
  44. data/lib/graphql/query/fingerprint.rb +2 -1
  45. data/lib/graphql/query/partial.rb +5 -2
  46. data/lib/graphql/query.rb +8 -12
  47. data/lib/graphql/railtie.rb +1 -1
  48. data/lib/graphql/relay.rb +1 -1
  49. data/lib/graphql/rubocop.rb +1 -1
  50. data/lib/graphql/runtime_error.rb +1 -1
  51. data/lib/graphql/runtime_type_error.rb +1 -1
  52. data/lib/graphql/schema/addition.rb +55 -48
  53. data/lib/graphql/schema/always_visible.rb +3 -1
  54. data/lib/graphql/schema/argument.rb +14 -3
  55. data/lib/graphql/schema/base_64_encoder.rb +3 -1
  56. data/lib/graphql/schema/build_from_definition.rb +435 -173
  57. data/lib/graphql/schema/built_in_types.rb +2 -2
  58. data/lib/graphql/schema/directive/deprecated.rb +1 -1
  59. data/lib/graphql/schema/directive/include.rb +2 -1
  60. data/lib/graphql/schema/directive/skip.rb +2 -1
  61. data/lib/graphql/schema/directive/specified_by.rb +1 -1
  62. data/lib/graphql/schema/directive.rb +16 -0
  63. data/lib/graphql/schema/field/connection_extension.rb +1 -1
  64. data/lib/graphql/schema/field/scope_extension.rb +2 -1
  65. data/lib/graphql/schema/field.rb +4 -1
  66. data/lib/graphql/schema/find_inherited_value.rb +5 -1
  67. data/lib/graphql/schema/member/build_type.rb +5 -2
  68. data/lib/graphql/schema/member/graphql_type_names.rb +1 -1
  69. data/lib/graphql/schema/member/has_arguments.rb +0 -4
  70. data/lib/graphql/schema/member/has_directives.rb +27 -23
  71. data/lib/graphql/schema/member/has_interfaces.rb +0 -2
  72. data/lib/graphql/schema/member/has_path.rb +2 -1
  73. data/lib/graphql/schema/member/has_unresolved_type_error.rb +2 -1
  74. data/lib/graphql/schema/member.rb +3 -2
  75. data/lib/graphql/schema/mutation.rb +6 -1
  76. data/lib/graphql/schema/object.rb +11 -3
  77. data/lib/graphql/schema/resolver.rb +2 -2
  78. data/lib/graphql/schema/type_expression.rb +3 -1
  79. data/lib/graphql/schema/validator/all_validator.rb +6 -3
  80. data/lib/graphql/schema.rb +4 -2
  81. data/lib/graphql/static_validation/all_rules.rb +1 -1
  82. data/lib/graphql/static_validation/base_visitor.rb +125 -53
  83. data/lib/graphql/static_validation/definition_dependencies.rb +14 -2
  84. data/lib/graphql/static_validation/interpreter_visitor.rb +1 -1
  85. data/lib/graphql/static_validation/rules/argument_names_are_unique.rb +4 -1
  86. data/lib/graphql/static_validation/rules/directives_are_in_valid_locations.rb +2 -0
  87. data/lib/graphql/static_validation/rules/fields_are_defined_on_type.rb +8 -4
  88. data/lib/graphql/static_validation/rules/fragments_are_finite.rb +2 -1
  89. data/lib/graphql/static_validation/rules/fragments_are_named.rb +2 -1
  90. data/lib/graphql/static_validation/rules/fragments_are_named_error.rb +4 -1
  91. data/lib/graphql/static_validation/rules/fragments_are_on_composite_types.rb +6 -3
  92. data/lib/graphql/static_validation/rules/fragments_are_used.rb +2 -1
  93. data/lib/graphql/static_validation/rules/input_object_names_are_unique.rb +3 -1
  94. data/lib/graphql/static_validation/rules/mutation_root_exists.rb +3 -2
  95. data/lib/graphql/static_validation/rules/mutation_root_exists_error.rb +4 -1
  96. data/lib/graphql/static_validation/rules/no_definitions_are_present_error.rb +4 -1
  97. data/lib/graphql/static_validation/rules/not_single_subscription_error.rb +4 -1
  98. data/lib/graphql/static_validation/rules/one_of_input_objects_are_valid.rb +9 -2
  99. data/lib/graphql/static_validation/rules/query_root_exists.rb +3 -2
  100. data/lib/graphql/static_validation/rules/query_root_exists_error.rb +4 -1
  101. data/lib/graphql/static_validation/rules/required_input_object_attributes_are_present_error.rb +10 -4
  102. data/lib/graphql/static_validation/rules/subscription_root_exists_and_single_subscription_selection.rb +8 -6
  103. data/lib/graphql/static_validation/rules/subscription_root_exists_error.rb +4 -1
  104. data/lib/graphql/static_validation/rules/unique_directives_per_location.rb +5 -4
  105. data/lib/graphql/static_validation/rules/variable_default_values_are_correctly_typed.rb +2 -1
  106. data/lib/graphql/static_validation/rules/variable_names_are_unique.rb +2 -1
  107. data/lib/graphql/static_validation/rules/variable_usages_are_allowed.rb +1 -1
  108. data/lib/graphql/static_validation/rules/variables_are_input_types.rb +1 -1
  109. data/lib/graphql/static_validation/validation_context.rb +49 -14
  110. data/lib/graphql/static_validation/validation_timeout_error.rb +4 -1
  111. data/lib/graphql/static_validation.rb +1 -2
  112. data/lib/graphql/subscriptions/action_cable_subscriptions.rb +14 -13
  113. data/lib/graphql/testing.rb +1 -1
  114. data/lib/graphql/tracing/legacy_trace.rb +1 -1
  115. data/lib/graphql/tracing/null_trace.rb +1 -1
  116. data/lib/graphql/types/big_int.rb +4 -1
  117. data/lib/graphql/types/boolean.rb +3 -1
  118. data/lib/graphql/types/id.rb +3 -1
  119. data/lib/graphql/types/iso_8601_date.rb +1 -0
  120. data/lib/graphql/types/iso_8601_date_time.rb +6 -2
  121. data/lib/graphql/types/iso_8601_duration.rb +3 -0
  122. data/lib/graphql/types/relay/base_connection.rb +1 -1
  123. data/lib/graphql/types/relay/base_edge.rb +1 -1
  124. data/lib/graphql/types/relay/node.rb +1 -1
  125. data/lib/graphql/types/relay/page_info.rb +1 -1
  126. data/lib/graphql/types/relay/page_info_behaviors.rb +4 -1
  127. data/lib/graphql/types/relay.rb +1 -1
  128. data/lib/graphql/types/string.rb +1 -0
  129. data/lib/graphql/types.rb +1 -1
  130. data/lib/graphql/version.rb +2 -2
  131. metadata +3 -4
  132. data/lib/graphql/query/variables.rb +0 -99
  133. data/lib/graphql/static_validation/literal_validator.rb +0 -157
@@ -47,7 +47,7 @@ module GraphQL
47
47
  document
48
48
  end
49
49
  rescue SystemStackError
50
- raise GraphQL::ParseError.new("This query is too large to execute.", nil, nil, @query_str, filename: @filename)
50
+ raise GraphQL::ParseError.new("This query is too large to execute.", nil, nil, @graphql_str, filename: @filename)
51
51
  end
52
52
  end
53
53
 
@@ -89,8 +89,10 @@ module GraphQL
89
89
  la = []
90
90
  idx = 0 #: Integer?
91
91
  while idx
92
- idx = graphql_str.index("\n", idx)
92
+ idx = graphql_str.index(/[\r\n]/, idx)
93
93
  if idx
94
+ # Record the final byte so CRLF counts as one line break.
95
+ idx += 1 if graphql_str.getbyte(idx) == 13 && graphql_str.getbyte(idx + 1) == 10
94
96
  la << idx
95
97
  idx += 1
96
98
  end
@@ -172,7 +174,7 @@ module GraphQL
172
174
  var_type = self.type || raise_parse_error("Missing type definition for variable: $#{var_name}")
173
175
  default_value = if at?(:EQUALS)
174
176
  advance_token
175
- value
177
+ value(const: true)
176
178
  end
177
179
 
178
180
  directives = parse_directives #: untyped
@@ -370,7 +372,11 @@ module GraphQL
370
372
  input_fields_definition = parse_input_object_field_definitions
371
373
  InputObjectTypeDefinition.new(pos: loc, definition_pos: defn_loc, description: desc, name: name, directives: directives, fields: input_fields_definition, filename: @filename, source: self)
372
374
  else
373
- expect_one_of([:SCHEMA, :SCALAR, :TYPE, :ENUM, :INPUT, :UNION, :INTERFACE])
375
+ expected_tokens = [:SCHEMA, :SCALAR, :TYPE, :ENUM, :INPUT, :UNION, :INTERFACE, :DIRECTIVE]
376
+ if desc.nil?
377
+ expected_tokens.unshift(:QUERY, :MUTATION, :SUBSCRIPTION, :FRAGMENT, :LCURLY, :EXTEND, :STRING)
378
+ end
379
+ expect_one_of(expected_tokens)
374
380
  end
375
381
  end
376
382
  end
@@ -487,7 +493,7 @@ module GraphQL
487
493
  type = self.type
488
494
  default_value = if at?(:EQUALS)
489
495
  advance_token
490
- value
496
+ value(const: true)
491
497
  else
492
498
  nil
493
499
  end
@@ -711,7 +717,7 @@ module GraphQL
711
717
  token_value
712
718
  end
713
719
 
714
- def value
720
+ def value(const: false)
715
721
  case token_name
716
722
  when :INT
717
723
  expect_token_value(:INT).to_i
@@ -734,7 +740,7 @@ module GraphQL
734
740
  advance_token
735
741
  list = []
736
742
  while !at?(:RBRACKET)
737
- list << value
743
+ list << value(const: const)
738
744
  end
739
745
  expect_token(:RBRACKET)
740
746
  list
@@ -746,11 +752,12 @@ module GraphQL
746
752
  loc = pos #: untyped
747
753
  n = parse_name
748
754
  expect_token(:COLON)
749
- args << Argument.new(pos: loc, name: n, value: value, filename: @filename, source: self)
755
+ args << Argument.new(pos: loc, name: n, value: value(const: const), filename: @filename, source: self)
750
756
  end
751
757
  expect_token(:RCURLY)
752
758
  InputObject.new(pos: start, arguments: args, filename: @filename, source: self)
753
759
  when :VAR_SIGN
760
+ raise_parse_error("Unexpected variable in constant value") if const
754
761
  loc = pos
755
762
  advance_token
756
763
  VariableIdentifier.new(pos: loc, name: parse_name, filename: @filename, source: self)
@@ -59,7 +59,7 @@ module GraphQL
59
59
  if scanner.skip('"""')
60
60
  inside_triple_quoted_string = !inside_triple_quoted_string
61
61
  new_query_str && (new_query_str << scanner.matched)
62
- elsif scanner.skip(/(?:\\"|[^"\n\r])+/m)
62
+ elsif scanner.skip(inside_single_quoted_string ? /(?>\\[\\"]|[^"\n\r])+/ : /(?:\\"|[^"\n\r])+/m)
63
63
  new_query_str && (new_query_str << scanner.matched)
64
64
  elsif scanner.skip('"')
65
65
  new_query_str && (new_query_str << '"')
@@ -1,9 +1,10 @@
1
1
  # frozen_string_literal: true
2
- # typed: true
2
+ # typed: strict
3
3
  module GraphQL
4
4
  class NameValidator
5
5
  VALID_NAME_REGEX = /^[_a-zA-Z][_a-zA-Z0-9]*$/
6
6
 
7
+ #: (String | Symbol name) -> void
7
8
  def self.validate!(name)
8
9
  name = name.is_a?(String) ? name : name.to_s
9
10
  raise GraphQL::InvalidNameError.new(name, VALID_NAME_REGEX) unless name.match?(VALID_NAME_REGEX)
@@ -7,6 +7,10 @@ module GraphQL
7
7
  module Pagination
8
8
  # A generic class for working with database query objects.
9
9
  class RelationConnection < Pagination::Connection
10
+ # Database limits and offsets must fit in a signed 64-bit integer.
11
+ MAX_OFFSET = (2**63) - 1
12
+ private_constant :MAX_OFFSET
13
+
10
14
  def nodes
11
15
  load_nodes
12
16
  @nodes
@@ -87,7 +91,11 @@ module GraphQL
87
91
  # :call-seq:
88
92
  # relation_larger_than(Object relation, Integer _initial_offset, Integer size) -> bool
89
93
  def relation_larger_than(relation, _initial_offset, size)
90
- relation_count(set_limit(relation, size + 1)) == size + 1
94
+ if (limit = relation_limit(relation)) && limit <= size
95
+ false
96
+ else
97
+ relation_count(set_limit(relation, size + 1)) == size + 1
98
+ end
91
99
  end
92
100
 
93
101
  # **Parameters**
@@ -154,7 +162,8 @@ module GraphQL
154
162
  # offset_from_cursor(cursor) -> Integer
155
163
  def offset_from_cursor(cursor)
156
164
  offset = Integer(decode(cursor), 10, exception: false) #: Integer?
157
- if offset.nil? || offset <= 0
165
+ if offset.nil? || offset <= 0 || offset > MAX_OFFSET ||
166
+ offset + (relation_offset(items) || 0) > MAX_OFFSET
158
167
  raise GraphQL::ExecutionError, "Invalid cursor: #{cursor.inspect}"
159
168
  end
160
169
  offset
@@ -191,20 +200,21 @@ module GraphQL
191
200
 
192
201
  if after_offset
193
202
  next_offset += after_offset
203
+ relation_limit = [relation_limit - after_offset, 0].max if relation_limit
194
204
  end
195
205
 
196
206
  if before_offset && after_offset
197
207
  if after_offset < before_offset
198
208
  # Get the number of items between the two cursors
199
209
  space_between = before_offset - after_offset - 1
200
- relation_limit = space_between
210
+ relation_limit = [relation_limit, space_between].compact.min
201
211
  else
202
212
  # The cursors overextend one another to an empty set
203
213
  @sliced_nodes_null_relation = true
204
214
  end
205
215
  elsif before_offset
206
216
  # Use limit to cut off the tail of the relation
207
- relation_limit = before_offset - 1
217
+ relation_limit = [relation_limit, before_offset - 1].compact.min
208
218
  end
209
219
 
210
220
  @sliced_nodes_limit = relation_limit
@@ -222,13 +232,13 @@ module GraphQL
222
232
  if @sliced_nodes_null_relation
223
233
  paginated_nodes = null_relation(paginated_nodes)
224
234
  else
225
- if @sliced_nodes_limit
226
- paginated_nodes = set_limit(paginated_nodes, @sliced_nodes_limit)
227
- end
228
-
229
235
  if @sliced_nodes_offset
230
236
  paginated_nodes = set_offset(paginated_nodes, @sliced_nodes_offset)
231
237
  end
238
+
239
+ if @sliced_nodes_limit
240
+ paginated_nodes = set_limit(paginated_nodes, @sliced_nodes_limit)
241
+ end
232
242
  end
233
243
 
234
244
  paginated_nodes
@@ -330,10 +340,11 @@ module GraphQL
330
340
  end
331
341
 
332
342
  # Fetch one row past `first` so `has_next_page` needs no extra query.
333
- # Skips arrays (no query to save) and relations limited below `first` (no next page).
343
+ # Skips arrays (no query to save) and slices limited to `first` or fewer rows (no next page).
334
344
  def load_page
335
345
  page = limited_nodes
336
- return page.to_a if page.is_a?(Array) || !peek_next_page? || relation_limit(page) != first
346
+ return page.to_a if page.is_a?(Array) || !peek_next_page? || relation_limit(page) != first ||
347
+ (@sliced_nodes_limit && @sliced_nodes_limit <= first)
337
348
 
338
349
  rows = set_limit(page, first + 1).to_a
339
350
  @has_next_page = rows.size > first
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
- # typed: true
2
+ # typed: strict
3
3
  require "graphql/pagination/array_connection"
4
4
  require "graphql/pagination/active_record_relation_connection"
5
5
  require "graphql/pagination/connections"
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
- # typed: true
2
+ # typed: strict
3
3
 
4
4
  require 'digest/sha2'
5
5
 
@@ -20,6 +20,7 @@ module GraphQL
20
20
  #
21
21
  # :call-seq:
22
22
  # generate(input_str) -> String
23
+ #: (String input_str) -> String
23
24
  def self.generate(input_str)
24
25
  # Implemented to be:
25
26
  # - Short (and uniform) length
@@ -102,6 +102,10 @@ module GraphQL
102
102
  @query.variables
103
103
  end
104
104
 
105
+ def input_values
106
+ @query.input_values
107
+ end
108
+
105
109
  def fragments
106
110
  @query.fragments
107
111
  end
@@ -179,8 +183,7 @@ module GraphQL
179
183
  next_selections << sel
180
184
  end
181
185
  else
182
- raise "Unexpected selection in partial path: #{sel.class}, #{sel.inspect}"
183
- sel #: absurd
186
+ raise "Unexpected selection in partial path: #{sel.class}, #{sel.inspect}" #: absurd(sel)
184
187
  end
185
188
  end
186
189
  end
data/lib/graphql/query.rb CHANGED
@@ -47,7 +47,6 @@ module GraphQL
47
47
  autoload :NullContext, "graphql/query/null_context"
48
48
  autoload :Partial, "graphql/query/partial"
49
49
  autoload :Result, "graphql/query/result"
50
- autoload :Variables, "graphql/query/variables"
51
50
  autoload :InputValidationResult, "graphql/query/input_validation_result"
52
51
  autoload :VariableValidationError, "graphql/query/variable_validation_error"
53
52
  autoload :ValidationPipeline, "graphql/query/validation_pipeline"
@@ -236,6 +235,7 @@ module GraphQL
236
235
  @root_value = root_value
237
236
  @fragments = nil
238
237
  @operations = nil
238
+ @input_values = nil
239
239
  @finalizers = @top_level_finalizers = nil
240
240
  @validate = validate
241
241
  self.static_validator = static_validator if static_validator
@@ -332,6 +332,10 @@ module GraphQL
332
332
  end
333
333
  end
334
334
 
335
+ def input_values # :nodoc:
336
+ @input_values ||= Execution::InputValues.new(self)
337
+ end
338
+
335
339
  def result_values=(result_hash) # :nodoc:
336
340
  if @executed
337
341
  raise "Invariant: Can't reassign result"
@@ -424,20 +428,12 @@ module GraphQL
424
428
  #
425
429
  # **Returns**
426
430
  #
427
- # - `GraphQL::Query::Variables` — Variables to apply to this query
431
+ # - `GraphQL::Execution::VariableValues` — Variables to apply to this query
428
432
  #
429
433
  # :call-seq:
430
- # variables() -> GraphQL::Query::Variables
434
+ # variables() -> GraphQL::Execution::VariableValues
431
435
  def variables
432
- @variables ||= begin
433
- with_prepared_ast {
434
- GraphQL::Query::Variables.new(
435
- @context,
436
- @ast_variables,
437
- @provided_variables,
438
- )
439
- }
440
- end
436
+ @variables ||= with_prepared_ast { input_values.variable_values }
441
437
  end
442
438
 
443
439
  # A version of the given query string, with:
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
- # typed: true
2
+ # typed: strict
3
3
 
4
4
  module GraphQL
5
5
  # Support [GraphQL::Language::Cache](rdoc-ref:GraphQL::Language::Cache) and [GraphQL.eager_load!](rdoc-ref:GraphQL.eager_load!)
data/lib/graphql/relay.rb CHANGED
@@ -1,4 +1,4 @@
1
1
  # frozen_string_literal: true
2
- # typed: true
2
+ # typed: strict
3
3
 
4
4
  require 'graphql/relay/range_add'
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
- # typed: true
2
+ # typed: strict
3
3
 
4
4
  require "graphql/rubocop/graphql/default_null_true"
5
5
  require "graphql/rubocop/graphql/default_required_true"
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
- # typed: true
2
+ # typed: strict
3
3
  module GraphQL
4
4
  class RuntimeError < Error
5
5
  include GraphQL::Execution::Finalizer
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
- # typed: true
2
+ # typed: strict
3
3
  module GraphQL
4
4
  class RuntimeTypeError < GraphQL::Error
5
5
  end
@@ -11,9 +11,10 @@ module GraphQL
11
11
  @own_types = own_types
12
12
  @directives = Set.new
13
13
  @possible_types = {}
14
+ @possible_types_sets = {}
14
15
  @types = {}
15
16
  @union_memberships = {}
16
- @references = Hash.new { |h, k| h[k] = Set.new }
17
+ @references = {}.compare_by_identity
17
18
  @arguments_with_default_values = []
18
19
  add_type_and_traverse(new_types)
19
20
  end
@@ -21,7 +22,8 @@ module GraphQL
21
22
  private
22
23
 
23
24
  def references_to(thing, from:)
24
- @references[thing].add(from)
25
+ refs = @references[thing] ||= {}.compare_by_identity
26
+ refs[from] = true
25
27
  end
26
28
 
27
29
  def get_type(name)
@@ -42,19 +44,25 @@ module GraphQL
42
44
 
43
45
  def add_directives_from(owner)
44
46
  if !(dir_instances = owner.directives).empty?
45
- dirs = dir_instances.map(&:class)
46
- @directives.merge(dirs)
47
- add_type_and_traverse(dirs)
47
+ new_dirs = nil #: Array[singleton(GraphQL::Schema::Directive)]?
48
+ dir_instances.each do |dir|
49
+ dir_class = dir.class
50
+ if !@directives.include?(dir_class)
51
+ new_dirs ||= []
52
+ new_dirs << dir_class
53
+ end
54
+ end
55
+ if new_dirs
56
+ @directives.merge(new_dirs)
57
+ add_type_and_traverse(new_dirs)
58
+ end
48
59
  end
49
60
  end
50
61
 
51
62
  def add_type_and_traverse(new_types)
52
63
  late_types = []
53
- path = []
54
64
  new_types.each do |t|
55
- path.push(t.graphql_name)
56
- add_type(t, owner: nil, late_types: late_types, path: path)
57
- path.pop
65
+ add_type(t, owner: nil, late_types: late_types)
58
66
  end
59
67
  missed_late_types = 0
60
68
  while (late_type_vals = late_types.shift)
@@ -64,13 +72,13 @@ module GraphQL
64
72
  # Reset the counter, since we might succeed next go-round
65
73
  missed_late_types = 0
66
74
  update_type_owner(type_owner, type)
67
- add_type(type, owner: type_owner, late_types: late_types, path: [type.graphql_name])
75
+ add_type(type, owner: type_owner, late_types: late_types)
68
76
  elsif lt.is_a?(LateBoundType)
69
77
  if (type = get_type(lt.name))
70
78
  # Reset the counter, since we might succeed next go-round
71
79
  missed_late_types = 0
72
80
  update_type_owner(type_owner, type)
73
- add_type(type, owner: type_owner, late_types: late_types, path: [type.graphql_name])
81
+ add_type(type, owner: type_owner, late_types: late_types)
74
82
  else
75
83
  missed_late_types += 1
76
84
  # Add it back to the list, maybe we'll be able to resolve it later.
@@ -89,6 +97,15 @@ module GraphQL
89
97
  nil
90
98
  end
91
99
 
100
+ # The Set makes this fast for interfaces with many possible types. The Array keeps their order.
101
+ def add_possible_type(interface_type, object_type)
102
+ types_set = @possible_types_sets[interface_type] ||= Set.new
103
+ if types_set.add?(object_type)
104
+ pt = @possible_types[interface_type] ||= []
105
+ pt << object_type
106
+ end
107
+ end
108
+
92
109
  def update_type_owner(owner, type)
93
110
  case owner
94
111
  when Module
@@ -114,9 +131,8 @@ module GraphQL
114
131
  end
115
132
  implementing_type.implements(*new_interfaces)
116
133
  new_interfaces.each do |int|
117
- pt = @possible_types[int] ||= []
118
- if !pt.include?(owner) && owner.is_a?(Class)
119
- pt << owner
134
+ if owner.is_a?(Class)
135
+ add_possible_type(int, owner)
120
136
  end
121
137
  int.interfaces.each do |indirect_int|
122
138
  if indirect_int.is_a?(LateBoundType)
@@ -125,10 +141,7 @@ module GraphQL
125
141
  end
126
142
  elsif owner.is_a?(Class)
127
143
  # `implements` added this one, too, because `int` implements it
128
- indirect_pt = @possible_types[indirect_int] ||= []
129
- if !indirect_pt.include?(owner)
130
- indirect_pt << owner
131
- end
144
+ add_possible_type(indirect_int, owner)
132
145
  end
133
146
  end
134
147
  end
@@ -161,7 +174,7 @@ module GraphQL
161
174
  end
162
175
  end
163
176
 
164
- def add_type(type, owner:, late_types:, path:)
177
+ def add_type(type, owner:, late_types:)
165
178
  if type.is_a?(String) || type.is_a?(GraphQL::Schema::LateBoundType)
166
179
  late_types << [owner, type]
167
180
  return
@@ -181,9 +194,7 @@ module GraphQL
181
194
  if !arg_type.is_a?(GraphQL::Schema::LateBoundType)
182
195
  references_to(arg_type, from: arg)
183
196
  end
184
- path.push(arg.graphql_name)
185
- add_type(arg_type, owner: arg, late_types: late_types, path: path)
186
- path.pop
197
+ add_type(arg_type, owner: arg, late_types: late_types)
187
198
  if arg.default_value?
188
199
  @arguments_with_default_values << arg
189
200
  end
@@ -202,40 +213,45 @@ module GraphQL
202
213
  if type.kind.fields?
203
214
  type.all_field_definitions.each do |field|
204
215
  field.ensure_loaded
205
- name = field.graphql_name
206
216
  field_type = field.type.unwrap
207
- if !field_type.is_a?(GraphQL::Schema::LateBoundType)
217
+ if field_type.is_a?(GraphQL::Schema::LateBoundType)
218
+ add_type(field_type, owner: field, late_types: late_types)
219
+ else
208
220
  references_to(field_type, from: field)
221
+ if !@types[field_type.graphql_name].equal?(field_type)
222
+ add_type(field_type, owner: field, late_types: late_types)
223
+ end
209
224
  end
210
- path.push(name)
211
- add_type(field_type, owner: field, late_types: late_types, path: path)
212
225
  add_directives_from(field)
213
226
  field.all_argument_definitions.each do |arg|
214
227
  add_directives_from(arg)
215
228
  arg_type = arg.type.unwrap
216
- if !arg_type.is_a?(GraphQL::Schema::LateBoundType)
229
+ if arg_type.is_a?(GraphQL::Schema::LateBoundType)
230
+ add_type(arg_type, owner: arg, late_types: late_types)
231
+ else
217
232
  references_to(arg_type, from: arg)
233
+ if !@types[arg_type.graphql_name].equal?(arg_type)
234
+ add_type(arg_type, owner: arg, late_types: late_types)
235
+ end
218
236
  end
219
- path.push(arg.graphql_name)
220
- add_type(arg_type, owner: arg, late_types: late_types, path: path)
221
- path.pop
222
237
  if arg.default_value?
223
238
  @arguments_with_default_values << arg
224
239
  end
225
240
  end
226
- path.pop
227
241
  end
228
242
  end
229
243
  if type.kind.input_object?
230
244
  type.all_argument_definitions.each do |arg|
231
245
  add_directives_from(arg)
232
246
  arg_type = arg.type.unwrap
233
- if !arg_type.is_a?(GraphQL::Schema::LateBoundType)
247
+ if arg_type.is_a?(GraphQL::Schema::LateBoundType)
248
+ add_type(arg_type, owner: arg, late_types: late_types)
249
+ else
234
250
  references_to(arg_type, from: arg)
251
+ if !@types[arg_type.graphql_name].equal?(arg_type)
252
+ add_type(arg_type, owner: arg, late_types: late_types)
253
+ end
235
254
  end
236
- path.push(arg.graphql_name)
237
- add_type(arg_type, owner: arg, late_types: late_types, path: path)
238
- path.pop
239
255
  if arg.default_value?
240
256
  @arguments_with_default_values << arg
241
257
  end
@@ -243,18 +259,14 @@ module GraphQL
243
259
  end
244
260
  if type.kind.union?
245
261
  @possible_types[type] = type.all_possible_types
246
- path.push("possible_types")
247
262
  type.all_possible_types.each do |t|
248
- add_type(t, owner: type, late_types: late_types, path: path)
263
+ add_type(t, owner: type, late_types: late_types)
249
264
  end
250
- path.pop
251
265
  end
252
266
  if type.kind.interface?
253
- path.push("orphan_types")
254
267
  type.orphan_types.each do |t|
255
- add_type(t, owner: type, late_types: late_types, path: path)
268
+ add_type(t, owner: type, late_types: late_types)
256
269
  end
257
- path.pop
258
270
  end
259
271
  if type.kind.object?
260
272
  possible_types_for_this_name = @possible_types[type] ||= []
@@ -262,26 +274,21 @@ module GraphQL
262
274
  end
263
275
 
264
276
  if type.kind.object? || type.kind.interface?
265
- path.push("implements")
266
277
  type.interface_type_memberships.each do |interface_type_membership|
267
278
  case interface_type_membership
268
279
  when Schema::TypeMembership
269
280
  interface_type = interface_type_membership.abstract_type
270
281
  # We can get these now; we'll have to get late-bound types later
271
282
  if interface_type.is_a?(Module) && type.is_a?(Class)
272
- implementers = @possible_types[interface_type] ||= []
273
- if !implementers.include?(type)
274
- implementers << type
275
- end
283
+ add_possible_type(interface_type, type)
276
284
  end
277
285
  when String, Schema::LateBoundType
278
286
  interface_type = interface_type_membership
279
287
  else
280
288
  raise ArgumentError, "Invariant: unexpected type membership for #{type.graphql_name}: #{interface_type_membership.class} (#{interface_type_membership.inspect})"
281
289
  end
282
- add_type(interface_type, owner: type, late_types: late_types, path: path)
290
+ add_type(interface_type, owner: type, late_types: late_types)
283
291
  end
284
- path.pop
285
292
  end
286
293
 
287
294
  if type.kind.enum?
@@ -1,13 +1,15 @@
1
1
  # frozen_string_literal: true
2
- # typed: true
2
+ # typed: strict
3
3
  module GraphQL
4
4
  class Schema
5
5
  module AlwaysVisible
6
+ #: (singleton(GraphQL::Schema) schema, **untyped opts) -> void
6
7
  def self.use(schema, **opts)
7
8
  schema.use(GraphQL::Schema::Visibility, profiles: { nil => {} })
8
9
  schema.extend(self)
9
10
  end
10
11
 
12
+ #: (untyped _member, GraphQL::Query::Context _context) -> bool
11
13
  def visible?(_member, _context)
12
14
  true
13
15
  end
@@ -515,7 +515,7 @@ module GraphQL
515
515
  coerced_value = begin
516
516
  type.coerce_input(value, context)
517
517
  rescue StandardError => err
518
- context.schema.handle_or_reraise(context, err)
518
+ handle_or_reraise_arg_error(context, err)
519
519
  end
520
520
 
521
521
  # If this isn't lazy, then the block returns eagerly and assigns the result here
@@ -525,14 +525,14 @@ module GraphQL
525
525
  prepared_value = begin
526
526
  prepare_value(parent_object, resolved_coerced_value, context: context)
527
527
  rescue StandardError => err
528
- context.schema.handle_or_reraise(context, err)
528
+ handle_or_reraise_arg_error(context, err)
529
529
  end
530
530
 
531
531
  if loads && !from_resolver?
532
532
  loaded_value = begin
533
533
  load_and_authorize_value(owner, prepared_value, context)
534
534
  rescue StandardError => err
535
- context.schema.handle_or_reraise(context, err)
535
+ handle_or_reraise_arg_error(context, err)
536
536
  end
537
537
  end
538
538
 
@@ -549,6 +549,17 @@ module GraphQL
549
549
  end
550
550
  end
551
551
 
552
+ # When a `rescue_from` handler *returns* an error object instead of raising,
553
+ # it must not be used as this argument's value — raise it so that
554
+ # `coerce_arguments` short-circuits resolution with it.
555
+ def handle_or_reraise_arg_error(context, err) # :nodoc:
556
+ handled_err = context.schema.handle_or_reraise(context, err)
557
+ if handled_err.is_a?(GraphQL::ExecutionError) || handled_err.is_a?(GraphQL::UnauthorizedError)
558
+ raise handled_err
559
+ end
560
+ handled_err
561
+ end
562
+
552
563
  def load_and_authorize_value(load_method_owner, coerced_value, context)
553
564
  if coerced_value.nil?
554
565
  return nil
@@ -1,13 +1,15 @@
1
1
  # frozen_string_literal: true
2
- # typed: true
2
+ # typed: strict
3
3
  require "base64"
4
4
  module GraphQL
5
5
  class Schema
6
6
  module Base64Encoder # :nodoc:
7
+ #: (String unencoded_text, ?nonce: bool) -> String
7
8
  def self.encode(unencoded_text, nonce: false)
8
9
  Base64.urlsafe_encode64(unencoded_text, padding: false)
9
10
  end
10
11
 
12
+ #: (String encoded_text, ?nonce: bool) -> String
11
13
  def self.decode(encoded_text, nonce: false)
12
14
  # urlsafe_decode64 is for forward compatibility
13
15
  Base64.urlsafe_decode64(encoded_text)