graphql 2.0.27 → 2.1.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (82) hide show
  1. checksums.yaml +4 -4
  2. data/lib/generators/graphql/install/templates/base_mutation.erb +2 -0
  3. data/lib/generators/graphql/install/templates/mutation_type.erb +2 -0
  4. data/lib/generators/graphql/templates/base_argument.erb +2 -0
  5. data/lib/generators/graphql/templates/base_connection.erb +2 -0
  6. data/lib/generators/graphql/templates/base_edge.erb +2 -0
  7. data/lib/generators/graphql/templates/base_enum.erb +2 -0
  8. data/lib/generators/graphql/templates/base_field.erb +2 -0
  9. data/lib/generators/graphql/templates/base_input_object.erb +2 -0
  10. data/lib/generators/graphql/templates/base_interface.erb +2 -0
  11. data/lib/generators/graphql/templates/base_object.erb +2 -0
  12. data/lib/generators/graphql/templates/base_scalar.erb +2 -0
  13. data/lib/generators/graphql/templates/base_union.erb +2 -0
  14. data/lib/generators/graphql/templates/graphql_controller.erb +2 -0
  15. data/lib/generators/graphql/templates/loader.erb +2 -0
  16. data/lib/generators/graphql/templates/mutation.erb +2 -0
  17. data/lib/generators/graphql/templates/node_type.erb +2 -0
  18. data/lib/generators/graphql/templates/query_type.erb +2 -0
  19. data/lib/generators/graphql/templates/schema.erb +2 -0
  20. data/lib/graphql/analysis/ast/analyzer.rb +7 -0
  21. data/lib/graphql/analysis/ast/visitor.rb +2 -2
  22. data/lib/graphql/analysis/ast.rb +15 -11
  23. data/lib/graphql/dataloader/source.rb +7 -0
  24. data/lib/graphql/dataloader.rb +9 -0
  25. data/lib/graphql/execution/interpreter/runtime/graphql_result.rb +170 -0
  26. data/lib/graphql/execution/interpreter/runtime.rb +95 -254
  27. data/lib/graphql/execution/interpreter.rb +0 -6
  28. data/lib/graphql/execution/lookahead.rb +1 -1
  29. data/lib/graphql/introspection/dynamic_fields.rb +1 -1
  30. data/lib/graphql/introspection/entry_points.rb +2 -2
  31. data/lib/graphql/language/block_string.rb +28 -16
  32. data/lib/graphql/language/definition_slice.rb +1 -1
  33. data/lib/graphql/language/document_from_schema_definition.rb +36 -35
  34. data/lib/graphql/language/nodes.rb +2 -2
  35. data/lib/graphql/language/printer.rb +294 -145
  36. data/lib/graphql/language/sanitized_printer.rb +20 -22
  37. data/lib/graphql/language/static_visitor.rb +167 -0
  38. data/lib/graphql/language/visitor.rb +20 -81
  39. data/lib/graphql/language.rb +1 -0
  40. data/lib/graphql/pagination/connection.rb +23 -1
  41. data/lib/graphql/query/context/scoped_context.rb +101 -0
  42. data/lib/graphql/query/context.rb +32 -98
  43. data/lib/graphql/query.rb +2 -19
  44. data/lib/graphql/rake_task.rb +3 -12
  45. data/lib/graphql/schema/directive/specified_by.rb +14 -0
  46. data/lib/graphql/schema/field/connection_extension.rb +1 -15
  47. data/lib/graphql/schema/field/scope_extension.rb +7 -1
  48. data/lib/graphql/schema/field.rb +6 -3
  49. data/lib/graphql/schema/has_single_input_argument.rb +156 -0
  50. data/lib/graphql/schema/introspection_system.rb +2 -0
  51. data/lib/graphql/schema/member/base_dsl_methods.rb +2 -1
  52. data/lib/graphql/schema/member/has_arguments.rb +14 -2
  53. data/lib/graphql/schema/member/has_fields.rb +4 -1
  54. data/lib/graphql/schema/member/has_interfaces.rb +21 -7
  55. data/lib/graphql/schema/member/scoped.rb +19 -0
  56. data/lib/graphql/schema/object.rb +8 -0
  57. data/lib/graphql/schema/printer.rb +8 -7
  58. data/lib/graphql/schema/relay_classic_mutation.rb +6 -128
  59. data/lib/graphql/schema/resolver.rb +4 -0
  60. data/lib/graphql/schema/scalar.rb +3 -3
  61. data/lib/graphql/schema/subscription.rb +11 -4
  62. data/lib/graphql/schema/warden.rb +23 -37
  63. data/lib/graphql/schema.rb +23 -22
  64. data/lib/graphql/static_validation/all_rules.rb +1 -1
  65. data/lib/graphql/static_validation/base_visitor.rb +1 -1
  66. data/lib/graphql/static_validation/literal_validator.rb +1 -1
  67. data/lib/graphql/static_validation/rules/fields_will_merge.rb +1 -1
  68. data/lib/graphql/static_validation/rules/required_arguments_are_present.rb +1 -1
  69. data/lib/graphql/static_validation/rules/required_input_object_attributes_are_present.rb +1 -1
  70. data/lib/graphql/static_validation/validation_context.rb +5 -5
  71. data/lib/graphql/static_validation.rb +0 -1
  72. data/lib/graphql/subscriptions/action_cable_subscriptions.rb +2 -1
  73. data/lib/graphql/subscriptions.rb +11 -6
  74. data/lib/graphql/tracing/appoptics_trace.rb +2 -2
  75. data/lib/graphql/tracing/appoptics_tracing.rb +2 -2
  76. data/lib/graphql/types/relay/connection_behaviors.rb +19 -2
  77. data/lib/graphql/types/relay/edge_behaviors.rb +7 -0
  78. data/lib/graphql/version.rb +1 -1
  79. data/lib/graphql.rb +1 -2
  80. metadata +7 -4
  81. data/lib/graphql/filter.rb +0 -59
  82. data/lib/graphql/static_validation/type_stack.rb +0 -216
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: e62749a86dd0b2913ebcb9a8be343d7f0eb7199f1e301bc6b584227ddfa6bdec
4
- data.tar.gz: d9de79c247f14d316741a66162c3ff96d5621dd689ab837f488277d53701d66d
3
+ metadata.gz: 9997a74f471a1d4abbca1fe24f276db80bbad6f87b278a8083491ced64354906
4
+ data.tar.gz: 4265a7d1e76f784ad3df4739328fedfae1698e26dc4a1fee982971650e553c47
5
5
  SHA512:
6
- metadata.gz: 11adf0c6bf4279b840b2b5ba1627ec11f4cca4d08957faac1956b261d01b914e7f8c9cc9a737c4ce02fb404bb3abac1904c9fe74255ecf367b7b88dc64133fcf
7
- data.tar.gz: b04cfe4e331da788f7c5706671e53fd2f0bfae680da896198a3d127922f6b92609cd6f720e98d7d2fb03cdea532b5a4f1536fd1bd6e154c5beb98b010dcedce5
6
+ metadata.gz: 2b1c5bed0660b9c194814e889384b3a37a27967c1f808aff55e693431056729434a2556cf85e9dd4c184262f8ad1344fe86316c3ffa12afc2c87315d69773f04
7
+ data.tar.gz: 0b2c961d00651a479ce183e0c77d23d2090df85f46490aac53d724d0f657f9cfe1c49bda191feec4b3a90f1cf671cd55f0a6a97d0c79c20d567fc47e214f921d
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  <% module_namespacing_when_supported do -%>
2
4
  module Mutations
3
5
  class BaseMutation < GraphQL::Schema::RelayClassicMutation
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  <% module_namespacing_when_supported do -%>
2
4
  module Types
3
5
  class MutationType < Types::BaseObject
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  <% module_namespacing_when_supported do -%>
2
4
  module Types
3
5
  class BaseArgument < GraphQL::Schema::Argument
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  <% module_namespacing_when_supported do -%>
2
4
  module Types
3
5
  class BaseConnection < Types::BaseObject
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  <% module_namespacing_when_supported do -%>
2
4
  module Types
3
5
  class BaseEdge < Types::BaseObject
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  <% module_namespacing_when_supported do -%>
2
4
  module Types
3
5
  class BaseEnum < GraphQL::Schema::Enum
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  <% module_namespacing_when_supported do -%>
2
4
  module Types
3
5
  class BaseField < GraphQL::Schema::Field
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  <% module_namespacing_when_supported do -%>
2
4
  module Types
3
5
  class BaseInputObject < GraphQL::Schema::InputObject
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  <% module_namespacing_when_supported do -%>
2
4
  module Types
3
5
  module BaseInterface
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  <% module_namespacing_when_supported do -%>
2
4
  module Types
3
5
  class BaseObject < GraphQL::Schema::Object
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  <% module_namespacing_when_supported do -%>
2
4
  module Types
3
5
  class BaseScalar < GraphQL::Schema::Scalar
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  <% module_namespacing_when_supported do -%>
2
4
  module Types
3
5
  class BaseUnion < GraphQL::Schema::Union
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  <% module_namespacing_when_supported do -%>
2
4
  class GraphqlController < ApplicationController
3
5
  # If accessing from outside this domain, nullify the session
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  <% module_namespacing_when_supported do -%>
2
4
  module Loaders
3
5
  class <%= class_name %> < GraphQL::Batch::Loader
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  <% module_namespacing_when_supported do -%>
2
4
  module Mutations
3
5
  class <%= class_name %> < BaseMutation
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  <% module_namespacing_when_supported do -%>
2
4
  module Types
3
5
  module NodeType
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  <% module_namespacing_when_supported do -%>
2
4
  module Types
3
5
  class QueryType < Types::BaseObject
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  <% module_namespacing_when_supported do -%>
2
4
  class <%= schema_name %> < GraphQL::Schema
3
5
  query(Types::QueryType)
@@ -29,6 +29,13 @@ module GraphQL
29
29
  true
30
30
  end
31
31
 
32
+ # Analyzer hook to decide at analysis time whether analysis
33
+ # requires a visitor pass; can be disabled for precomputed results.
34
+ # @return [Boolean] If analysis requires visitation or not
35
+ def visit?
36
+ true
37
+ end
38
+
32
39
  # The result for this analyzer. Returning {GraphQL::AnalysisError} results
33
40
  # in a query error.
34
41
  # @return [Any] The analyzer result
@@ -5,12 +5,12 @@ module GraphQL
5
5
  # Depth first traversal through a query AST, calling AST analyzers
6
6
  # along the way.
7
7
  #
8
- # The visitor is a special case of GraphQL::Language::Visitor, visiting
8
+ # The visitor is a special case of GraphQL::Language::StaticVisitor, visiting
9
9
  # only the selected operation, providing helpers for common use cases such
10
10
  # as skipped fields and visiting fragment spreads.
11
11
  #
12
12
  # @see {GraphQL::Analysis::AST::Analyzer} AST Analyzers for queries
13
- class Visitor < GraphQL::Language::Visitor
13
+ class Visitor < GraphQL::Language::StaticVisitor
14
14
  def initialize(query:, analyzers:)
15
15
  @analyzers = analyzers
16
16
  @path = []
@@ -51,22 +51,26 @@ module GraphQL
51
51
  query.current_trace.analyze_query(query: query) do
52
52
  query_analyzers = analyzers
53
53
  .map { |analyzer| analyzer.new(query) }
54
- .select { |analyzer| analyzer.analyze? }
54
+ .tap { _1.select!(&:analyze?) }
55
55
 
56
56
  analyzers_to_run = query_analyzers + multiplex_analyzers
57
57
  if analyzers_to_run.any?
58
- visitor = GraphQL::Analysis::AST::Visitor.new(
59
- query: query,
60
- analyzers: analyzers_to_run
61
- )
62
58
 
63
- visitor.visit
59
+ analyzers_to_run.select!(&:visit?)
60
+ if analyzers_to_run.any?
61
+ visitor = GraphQL::Analysis::AST::Visitor.new(
62
+ query: query,
63
+ analyzers: analyzers_to_run
64
+ )
64
65
 
65
- if visitor.rescued_errors.any?
66
- visitor.rescued_errors
67
- else
68
- query_analyzers.map(&:result)
66
+ visitor.visit
67
+
68
+ if visitor.rescued_errors.any?
69
+ return visitor.rescued_errors
70
+ end
69
71
  end
72
+
73
+ query_analyzers.map(&:result)
70
74
  else
71
75
  []
72
76
  end
@@ -74,7 +78,7 @@ module GraphQL
74
78
  end
75
79
 
76
80
  def analysis_errors(results)
77
- results.flatten.select { |r| r.is_a?(GraphQL::AnalysisError) }
81
+ results.flatten.tap { _1.select! { |r| r.is_a?(GraphQL::AnalysisError) } }
78
82
  end
79
83
  end
80
84
  end
@@ -161,6 +161,13 @@ module GraphQL
161
161
  [*batch_args, **batch_kwargs]
162
162
  end
163
163
 
164
+ # Clear any already-loaded objects for this source
165
+ # @return [void]
166
+ def clear_cache
167
+ @results.clear
168
+ nil
169
+ end
170
+
164
171
  attr_reader :pending
165
172
 
166
173
  private
@@ -104,6 +104,15 @@ module GraphQL
104
104
  nil
105
105
  end
106
106
 
107
+ # Clear any already-loaded objects from {Source} caches
108
+ # @return [void]
109
+ def clear_cache
110
+ @source_cache.each do |_source_class, batched_sources|
111
+ batched_sources.each_value(&:clear_cache)
112
+ end
113
+ nil
114
+ end
115
+
107
116
  # Use a self-contained queue for the work in the block.
108
117
  def run_isolated
109
118
  prev_queue = @pending_jobs
@@ -0,0 +1,170 @@
1
+ # frozen_string_literal: true
2
+
3
+ module GraphQL
4
+ module Execution
5
+ class Interpreter
6
+ class Runtime
7
+ module GraphQLResult
8
+ def initialize(result_name, parent_result, is_non_null_in_parent)
9
+ @graphql_parent = parent_result
10
+ if parent_result && parent_result.graphql_dead
11
+ @graphql_dead = true
12
+ end
13
+ @graphql_result_name = result_name
14
+ @graphql_is_non_null_in_parent = is_non_null_in_parent
15
+ # Jump through some hoops to avoid creating this duplicate storage if at all possible.
16
+ @graphql_metadata = nil
17
+ end
18
+
19
+ def path
20
+ @path ||= build_path([])
21
+ end
22
+
23
+ def build_path(path_array)
24
+ graphql_result_name && path_array.unshift(graphql_result_name)
25
+ @graphql_parent ? @graphql_parent.build_path(path_array) : path_array
26
+ end
27
+
28
+ attr_accessor :graphql_dead
29
+ attr_reader :graphql_parent, :graphql_result_name, :graphql_is_non_null_in_parent
30
+
31
+ # @return [Hash] Plain-Ruby result data (`@graphql_metadata` contains Result wrapper objects)
32
+ attr_accessor :graphql_result_data
33
+ end
34
+
35
+ class GraphQLResultHash
36
+ def initialize(_result_name, _parent_result, _is_non_null_in_parent)
37
+ super
38
+ @graphql_result_data = {}
39
+ end
40
+
41
+ include GraphQLResult
42
+
43
+ attr_accessor :graphql_merged_into
44
+
45
+ def set_leaf(key, value)
46
+ # This is a hack.
47
+ # Basically, this object is merged into the root-level result at some point.
48
+ # But the problem is, some lazies are created whose closures retain reference to _this_
49
+ # object. When those lazies are resolved, they cause an update to this object.
50
+ #
51
+ # In order to return a proper top-level result, we have to update that top-level result object.
52
+ # In order to return a proper partial result (eg, for a directive), we have to update this object, too.
53
+ # Yowza.
54
+ if (t = @graphql_merged_into)
55
+ t.set_leaf(key, value)
56
+ end
57
+
58
+ @graphql_result_data[key] = value
59
+ # keep this up-to-date if it's been initialized
60
+ @graphql_metadata && @graphql_metadata[key] = value
61
+
62
+ value
63
+ end
64
+
65
+ def set_child_result(key, value)
66
+ if (t = @graphql_merged_into)
67
+ t.set_child_result(key, value)
68
+ end
69
+ @graphql_result_data[key] = value.graphql_result_data
70
+ # If we encounter some part of this response that requires metadata tracking,
71
+ # then create the metadata hash if necessary. It will be kept up-to-date after this.
72
+ (@graphql_metadata ||= @graphql_result_data.dup)[key] = value
73
+ value
74
+ end
75
+
76
+ def delete(key)
77
+ @graphql_metadata && @graphql_metadata.delete(key)
78
+ @graphql_result_data.delete(key)
79
+ end
80
+
81
+ def each
82
+ (@graphql_metadata || @graphql_result_data).each { |k, v| yield(k, v) }
83
+ end
84
+
85
+ def values
86
+ (@graphql_metadata || @graphql_result_data).values
87
+ end
88
+
89
+ def key?(k)
90
+ @graphql_result_data.key?(k)
91
+ end
92
+
93
+ def [](k)
94
+ (@graphql_metadata || @graphql_result_data)[k]
95
+ end
96
+
97
+ def merge_into(into_result)
98
+ self.each do |key, value|
99
+ case value
100
+ when GraphQLResultHash
101
+ next_into = into_result[key]
102
+ if next_into
103
+ value.merge_into(next_into)
104
+ else
105
+ into_result.set_child_result(key, value)
106
+ end
107
+ when GraphQLResultArray
108
+ # There's no special handling of arrays because currently, there's no way to split the execution
109
+ # of a list over several concurrent flows.
110
+ next_result.set_child_result(key, value)
111
+ else
112
+ # We have to assume that, since this passed the `fields_will_merge` selection,
113
+ # that the old and new values are the same.
114
+ into_result.set_leaf(key, value)
115
+ end
116
+ end
117
+ @graphql_merged_into = into_result
118
+ end
119
+ end
120
+
121
+ class GraphQLResultArray
122
+ include GraphQLResult
123
+
124
+ def initialize(_result_name, _parent_result, _is_non_null_in_parent)
125
+ super
126
+ @graphql_result_data = []
127
+ end
128
+
129
+ def graphql_skip_at(index)
130
+ # Mark this index as dead. It's tricky because some indices may already be storing
131
+ # `Lazy`s. So the runtime is still holding indexes _before_ skipping,
132
+ # this object has to coordinate incoming writes to account for any already-skipped indices.
133
+ @skip_indices ||= []
134
+ @skip_indices << index
135
+ offset_by = @skip_indices.count { |skipped_idx| skipped_idx < index}
136
+ delete_at_index = index - offset_by
137
+ @graphql_metadata && @graphql_metadata.delete_at(delete_at_index)
138
+ @graphql_result_data.delete_at(delete_at_index)
139
+ end
140
+
141
+ def set_leaf(idx, value)
142
+ if @skip_indices
143
+ offset_by = @skip_indices.count { |skipped_idx| skipped_idx < idx }
144
+ idx -= offset_by
145
+ end
146
+ @graphql_result_data[idx] = value
147
+ @graphql_metadata && @graphql_metadata[idx] = value
148
+ value
149
+ end
150
+
151
+ def set_child_result(idx, value)
152
+ if @skip_indices
153
+ offset_by = @skip_indices.count { |skipped_idx| skipped_idx < idx }
154
+ idx -= offset_by
155
+ end
156
+ @graphql_result_data[idx] = value.graphql_result_data
157
+ # If we encounter some part of this response that requires metadata tracking,
158
+ # then create the metadata hash if necessary. It will be kept up-to-date after this.
159
+ (@graphql_metadata ||= @graphql_result_data.dup)[idx] = value
160
+ value
161
+ end
162
+
163
+ def values
164
+ (@graphql_metadata || @graphql_result_data)
165
+ end
166
+ end
167
+ end
168
+ end
169
+ end
170
+ end