lighthouse-graphql 0.8.2 → 0.9.1
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.
- checksums.yaml +4 -4
- data/CHANGELOG.md +168 -1
- data/docs/authorization.md +5 -1
- data/docs/configuration.md +71 -3
- data/docs/custom-directives.md +22 -0
- data/docs/defer.md +352 -0
- data/docs/directives.md +106 -0
- data/docs/events.md +30 -1
- data/docs/filtering-and-ordering.md +567 -0
- data/docs/getting-started.md +1 -1
- data/docs/performance.md +9 -0
- data/docs/resolvers.md +14 -2
- data/docs/testing.md +99 -0
- data/lib/lighthouse/complexity.rb +4 -2
- data/lib/lighthouse/config.rb +28 -11
- data/lib/lighthouse/contracts/field_resolver.rb +32 -0
- data/lib/lighthouse/contracts/search_builder.rb +16 -0
- data/lib/lighthouse/controller.rb +94 -18
- data/lib/lighthouse/defer.rb +114 -0
- data/lib/lighthouse/directive_resolvers/aggregate_resolver.rb +1 -0
- data/lib/lighthouse/directive_resolvers/all_resolver.rb +32 -3
- data/lib/lighthouse/directive_resolvers/argument_binding.rb +1 -3
- data/lib/lighthouse/directive_resolvers/association_resolvers.rb +1 -0
- data/lib/lighthouse/directive_resolvers/base.rb +116 -49
- data/lib/lighthouse/directive_resolvers/builder_applier.rb +24 -18
- data/lib/lighthouse/directive_resolvers/can_resolvers.rb +28 -2
- data/lib/lighthouse/directive_resolvers/count_resolver.rb +1 -0
- data/lib/lighthouse/directive_resolvers/field_resolver.rb +4 -0
- data/lib/lighthouse/directive_resolvers/find_resolver.rb +31 -16
- data/lib/lighthouse/directive_resolvers/first_resolver.rb +6 -3
- data/lib/lighthouse/directive_resolvers/global_id_types.rb +1 -2
- data/lib/lighthouse/directive_resolvers/model_mutation_resolver.rb +2 -0
- data/lib/lighthouse/directive_resolvers/paginate_resolver.rb +29 -4
- data/lib/lighthouse/directive_resolvers/throttle_resolver.rb +17 -6
- data/lib/lighthouse/directives/arguments/base.rb +14 -0
- data/lib/lighthouse/directives/arguments/filters.rb +6 -0
- data/lib/lighthouse/directives/arguments/more_filters.rb +3 -0
- data/lib/lighthouse/directives/arguments/search.rb +34 -0
- data/lib/lighthouse/directives/arguments/soft_deletes.rb +11 -0
- data/lib/lighthouse/directives/model_directive.rb +1 -1
- data/lib/lighthouse/directives/registry.rb +1 -1
- data/lib/lighthouse/events/execution_trace.rb +6 -1
- data/lib/lighthouse/events/lifecycle.rb +2 -1
- data/lib/lighthouse/events.rb +9 -0
- data/lib/lighthouse/field_resolver_stack.rb +22 -21
- data/lib/lighthouse/graphql/version.rb +1 -1
- data/lib/lighthouse/incremental/body.rb +76 -0
- data/lib/lighthouse/incremental/defer_directive.rb +34 -0
- data/lib/lighthouse/incremental/delivery.rb +94 -0
- data/lib/lighthouse/incremental/document.rb +242 -0
- data/lib/lighthouse/incremental/gone.rb +6 -0
- data/lib/lighthouse/incremental/legacy.rb +75 -0
- data/lib/lighthouse/incremental/multipart.rb +76 -0
- data/lib/lighthouse/incremental/plan.rb +249 -0
- data/lib/lighthouse/incremental/rules.rb +116 -0
- data/lib/lighthouse/incremental/run.rb +302 -0
- data/lib/lighthouse/incremental/serializer.rb +37 -0
- data/lib/lighthouse/incremental/state.rb +204 -0
- data/lib/lighthouse/incremental/stream_directive.rb +41 -0
- data/lib/lighthouse/incremental/stream_trace.rb +93 -0
- data/lib/lighthouse/incremental/tree.rb +68 -0
- data/lib/lighthouse/namespaces.rb +2 -0
- data/lib/lighthouse/nested_mutations/belongs_to_operations.rb +2 -2
- data/lib/lighthouse/operations.rb +6 -2
- data/lib/lighthouse/pagination.rb +61 -15
- data/lib/lighthouse/persisted_queries.rb +53 -8
- data/lib/lighthouse/rb_lighthouse.rb +35 -0
- data/lib/lighthouse/resolve_info.rb +7 -1
- data/lib/lighthouse/schema_generator.rb +2 -0
- data/lib/lighthouse/search/auto.rb +26 -0
- data/lib/lighthouse/search/enhancer.rb +151 -0
- data/lib/lighthouse/search/field_check.rb +173 -0
- data/lib/lighthouse/search/pg_search.rb +146 -0
- data/lib/lighthouse/search/query.rb +54 -0
- data/lib/lighthouse/search/result.rb +181 -0
- data/lib/lighthouse/search/searchkick.rb +335 -0
- data/lib/lighthouse/search/searchkick_index.rb +54 -0
- data/lib/lighthouse/search/searchkick_options.rb +74 -0
- data/lib/lighthouse/search/together_check.rb +137 -0
- data/lib/lighthouse/search.rb +101 -0
- data/lib/lighthouse/support/argument_walk.rb +81 -0
- data/lib/lighthouse/support/count.rb +16 -0
- data/lib/lighthouse/support/failures.rb +28 -0
- data/lib/lighthouse/support/memo.rb +72 -0
- data/lib/lighthouse/support/model_resolver.rb +24 -0
- data/lib/lighthouse/support/relations.rb +31 -0
- data/lib/lighthouse/support/soft_deletes.rb +42 -8
- data/lib/lighthouse/testing.rb +81 -0
- data/sig/lighthouse/controller.rbs +34 -1
- data/sig/lighthouse/directives.rbs +46 -5
- data/sig/lighthouse/events.rbs +7 -0
- data/sig/lighthouse/graphql.rbs +117 -0
- data/sig/lighthouse/search.rbs +153 -0
- data/sig/lighthouse.rbs +77 -1
- metadata +39 -1
|
@@ -0,0 +1,242 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'set'
|
|
4
|
+
|
|
5
|
+
# What incremental delivery reads of the client's document, which it never
|
|
6
|
+
# rewrites: graphql-ruby runs the document as the client sent it, and listeners,
|
|
7
|
+
# `query_string`, tracing and Rules all see that one.
|
|
8
|
+
#
|
|
9
|
+
# - `directives(name)`: the @defer and @stream directive nodes the operation reaches,
|
|
10
|
+
# and `owner(directive)`, the fragment or field each sits on. State tells them apart
|
|
11
|
+
# at run time by node identity (see State#directive_for).
|
|
12
|
+
# - `selections(fragment)`, `condition(fragment)`: what a deferred fragment selects.
|
|
13
|
+
# - `declarations(pattern)`: every declaration of the field at a response path, with what
|
|
14
|
+
# decides whether it is part of the field's selection set for a given object. A
|
|
15
|
+
# streamed field's later items get the selections of all of them (Document#merged),
|
|
16
|
+
# as graphql-ruby merges them for the first ones.
|
|
17
|
+
# - `included?`, `applies?`, `deferred?`: a node's @skip/@include, a fragment's type
|
|
18
|
+
# condition and a fragment's @defer, evaluated with a query's variables.
|
|
19
|
+
class Lighthouse::GraphQL::Incremental::Document
|
|
20
|
+
Nodes = ::GraphQL::Language::Nodes
|
|
21
|
+
DIRECTIVES = %w[defer stream].freeze
|
|
22
|
+
# The name defer or stream, as a word: a text without it uses neither directive (one with it may
|
|
23
|
+
# not: a field, a comment, a string), whatever the lexer ignores between `@` and the name
|
|
24
|
+
# (whitespace, commas, a BOM, comments).
|
|
25
|
+
INCREMENTAL = /\b(?:defer|stream)\b/
|
|
26
|
+
|
|
27
|
+
# A declaration of a field: the field node, the fragments around it in its parent's
|
|
28
|
+
# selection set (`around`, whose type conditions are the parent object's), the fragments and
|
|
29
|
+
# fields above those (`gates`, whose @skip and @include must hold), whether one of those has
|
|
30
|
+
# a type condition only some objects meet (`uncertain`: it cannot be told from the parent
|
|
31
|
+
# object), and the @defer fragments around it, at any level.
|
|
32
|
+
Declaration = Struct.new(:field, :around, :gates, :uncertain, :defers, keyword_init: true)
|
|
33
|
+
|
|
34
|
+
# Where the walk of `declarations` is: the response path (without list indices), the type of
|
|
35
|
+
# the selection set the level starts with and the one it is in, and what a Declaration holds.
|
|
36
|
+
At = Struct.new(:pattern, :level_type, :type, :around, :gates, :uncertain, :defers, keyword_init: true)
|
|
37
|
+
|
|
38
|
+
# The schema's directive definitions, each looked up once (Schema.directives builds its Hash on
|
|
39
|
+
# every call): a request's Document and an analysis's Rules each hold one.
|
|
40
|
+
class Definitions
|
|
41
|
+
def initialize(schema)
|
|
42
|
+
@schema = schema
|
|
43
|
+
@found = {}
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
def [](name) = @found.fetch(name) { @found[name] = @schema.directives[name] }
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
# Does the document use @defer or @stream? A walk of its selections, for the queries that do not.
|
|
50
|
+
def self.incremental?(document)
|
|
51
|
+
document.definitions.any? { |definition| definition.respond_to?(:selections) && uses?(definition.selections) }
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
def self.uses?(selections)
|
|
55
|
+
visit_directives(selections) { return true }
|
|
56
|
+
false
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
# Each @defer and @stream of `selections`, at any depth, with the selection it is on; through the
|
|
60
|
+
# named fragments they spread (each once), with the document's `definitions` (by name).
|
|
61
|
+
def self.visit_directives(selections, definitions = nil, seen = Set.new, &block)
|
|
62
|
+
selections.each do |selection|
|
|
63
|
+
selection.directives.each { |directive| yield directive, selection if DIRECTIVES.include?(directive.name) }
|
|
64
|
+
inner = selection.is_a?(Nodes::FragmentSpread) ? spread(selection, definitions, seen) : selection.selections
|
|
65
|
+
visit_directives(inner, definitions, seen, &block)
|
|
66
|
+
end
|
|
67
|
+
end
|
|
68
|
+
|
|
69
|
+
# The selections of the named fragment `spread` spreads, the first time it is: none after, nor
|
|
70
|
+
# without the document's definitions.
|
|
71
|
+
def self.spread(spread, definitions, seen)
|
|
72
|
+
definition = definitions&.[](spread.name)
|
|
73
|
+
definition && seen.add?(spread.name) ? definition.selections : []
|
|
74
|
+
end
|
|
75
|
+
|
|
76
|
+
attr_reader :schema
|
|
77
|
+
|
|
78
|
+
# `operation`: the operation that runs (GraphQL::Query#selected_operation).
|
|
79
|
+
def initialize(schema, document, operation)
|
|
80
|
+
@schema = schema
|
|
81
|
+
@operation = operation
|
|
82
|
+
@definitions = document.definitions.grep(Nodes::FragmentDefinition).index_by(&:name)
|
|
83
|
+
@owners = {}.compare_by_identity
|
|
84
|
+
@directive_definitions = Definitions.new(schema)
|
|
85
|
+
end
|
|
86
|
+
|
|
87
|
+
def directives(name)
|
|
88
|
+
(@directives ||= collect_directives)[name] || []
|
|
89
|
+
end
|
|
90
|
+
|
|
91
|
+
def owner(directive)
|
|
92
|
+
directives('defer')
|
|
93
|
+
@owners[directive]
|
|
94
|
+
end
|
|
95
|
+
|
|
96
|
+
def selections(fragment)
|
|
97
|
+
fragment.is_a?(Nodes::FragmentSpread) ? @definitions[fragment.name]&.selections.to_a : fragment.selections
|
|
98
|
+
end
|
|
99
|
+
|
|
100
|
+
def condition(fragment)
|
|
101
|
+
(fragment.is_a?(Nodes::FragmentSpread) ? @definitions[fragment.name]&.type : fragment.type)&.name
|
|
102
|
+
end
|
|
103
|
+
|
|
104
|
+
def declarations(pattern)
|
|
105
|
+
(@index ||= index)[pattern] || []
|
|
106
|
+
end
|
|
107
|
+
|
|
108
|
+
# Do the node's @skip and @include let it through? graphql-ruby's own check
|
|
109
|
+
# (GraphQL::Execution::DirectiveChecks), as its runtime makes it: arguments that do not
|
|
110
|
+
# evaluate (an ExecutionError) let it through; with the directives' definitions looked up once.
|
|
111
|
+
def included?(node, query)
|
|
112
|
+
node.directives.all? do |candidate|
|
|
113
|
+
name = candidate.name
|
|
114
|
+
next true unless name == 'skip' || name == 'include'
|
|
115
|
+
|
|
116
|
+
arguments = query.arguments_for(candidate, directive(name))
|
|
117
|
+
arguments.is_a?(::GraphQL::ExecutionError) || arguments[:if] != (name == 'skip')
|
|
118
|
+
end
|
|
119
|
+
end
|
|
120
|
+
|
|
121
|
+
# Does the fragment's type condition hold for an object of the object type `type`?
|
|
122
|
+
def applies?(fragment, type, query)
|
|
123
|
+
name = condition(fragment) or return true
|
|
124
|
+
named = query.types.type(name)
|
|
125
|
+
!named.nil? && query.types.possible_types(named).include?(type)
|
|
126
|
+
end
|
|
127
|
+
|
|
128
|
+
# Does the fragment carry a @defer that `if` does not turn off?
|
|
129
|
+
def deferred?(fragment, query)
|
|
130
|
+
directive = fragment.directives.find { |candidate| candidate.name == 'defer' } or return false
|
|
131
|
+
query.arguments_for(directive, directive('defer'))[:if] != false
|
|
132
|
+
end
|
|
133
|
+
|
|
134
|
+
# The schema's directive `name`, looked up once a request.
|
|
135
|
+
def directive(name) = @directive_definitions[name]
|
|
136
|
+
|
|
137
|
+
# The unwrapped type of `field` (a node) on `type`; nil on a union, or where it is not known.
|
|
138
|
+
def field_type(type, field)
|
|
139
|
+
return nil if type.nil? || type.kind.union?
|
|
140
|
+
|
|
141
|
+
@schema.get_field(type, field.name)&.type&.unwrap
|
|
142
|
+
end
|
|
143
|
+
|
|
144
|
+
# The selections the later items of a streamed list run: those of each declaration of the field
|
|
145
|
+
# at `path` that holds for the parent object (of type `parent_type`). nil when that cannot be
|
|
146
|
+
# told (a declaration under a type condition of an object above the parent, or under another
|
|
147
|
+
# @defer that `deferring` would defer; or the streamed field's node is declared there under
|
|
148
|
+
# different @defer, through a named fragment spread at several places, so which of them runs
|
|
149
|
+
# is not known): the list is then answered whole.
|
|
150
|
+
def merged(directive, path, parent_type, query, deferring)
|
|
151
|
+
field = owner(directive)
|
|
152
|
+
declarations = declarations(path.grep_v(Integer))
|
|
153
|
+
owns, others = declarations.partition { |declaration| declaration.field.equal?(field) }
|
|
154
|
+
own = own(owns) or return nil
|
|
155
|
+
others.each_with_object(field.selections.dup) do |declaration, selections|
|
|
156
|
+
return nil if unknowable?(declaration, own, query, deferring)
|
|
157
|
+
|
|
158
|
+
selections.concat(declaration.field.selections) if holds?(declaration, parent_type, query)
|
|
159
|
+
end
|
|
160
|
+
end
|
|
161
|
+
|
|
162
|
+
private
|
|
163
|
+
|
|
164
|
+
# The streamed field's declaration, unless it is declared under different @defer.
|
|
165
|
+
def own(owns)
|
|
166
|
+
defers = owns.map { |declaration| declaration.defers.map(&:object_id).sort }
|
|
167
|
+
owns.first if defers.uniq.one?
|
|
168
|
+
end
|
|
169
|
+
|
|
170
|
+
def holds?(declaration, parent_type, query)
|
|
171
|
+
(declaration.gates + declaration.around + [declaration.field]).all? { |node| included?(node, query) } &&
|
|
172
|
+
declaration.around.all? { |fragment| applies?(fragment, parent_type, query) }
|
|
173
|
+
end
|
|
174
|
+
|
|
175
|
+
# Whether a declaration is part of the field's selection set cannot be told from the parent
|
|
176
|
+
# object: a type condition above it, or a @defer around it but not around `own` that defers.
|
|
177
|
+
def unknowable?(declaration, own, query, deferring)
|
|
178
|
+
declaration.uncertain || (deferring && (declaration.defers - own.defers).any? { |fragment| deferred?(fragment, query) })
|
|
179
|
+
end
|
|
180
|
+
|
|
181
|
+
# The directive nodes the operation reaches, each fragment definition walked once.
|
|
182
|
+
def collect_directives
|
|
183
|
+
Hash.new { |hash, name| hash[name] = [] }.tap do |found|
|
|
184
|
+
self.class.visit_directives(@operation&.selections.to_a, @definitions) do |directive, owner|
|
|
185
|
+
found[directive.name] << directive
|
|
186
|
+
@owners[directive] = owner
|
|
187
|
+
end
|
|
188
|
+
end
|
|
189
|
+
end
|
|
190
|
+
|
|
191
|
+
def index
|
|
192
|
+
{}.tap do |index|
|
|
193
|
+
root = @schema.root_type_for_operation(@operation.operation_type)
|
|
194
|
+
top = At.new(pattern: [], level_type: root, type: root, around: [], gates: [], uncertain: false, defers: [])
|
|
195
|
+
collect(@operation.selections, top, index, [])
|
|
196
|
+
end
|
|
197
|
+
end
|
|
198
|
+
|
|
199
|
+
def collect(selections, at, index, stack)
|
|
200
|
+
selections.each do |selection|
|
|
201
|
+
case selection
|
|
202
|
+
when Nodes::Field then declare(selection, at, index, stack)
|
|
203
|
+
when Nodes::InlineFragment then within(selection, selection.selections, at, index, stack)
|
|
204
|
+
when Nodes::FragmentSpread
|
|
205
|
+
definition = @definitions[selection.name]
|
|
206
|
+
within(selection, definition.selections, at, index, stack + [selection.name]) if definition && stack.exclude?(selection.name)
|
|
207
|
+
end
|
|
208
|
+
end
|
|
209
|
+
end
|
|
210
|
+
|
|
211
|
+
def declare(field, at, index, stack)
|
|
212
|
+
pattern = at.pattern + [field.alias || field.name]
|
|
213
|
+
(index[pattern] ||= []) << Declaration.new(field: field, around: at.around, gates: at.gates, uncertain: at.uncertain, defers: at.defers)
|
|
214
|
+
collect(field.selections, below(field, at, pattern), index, stack) unless field.selections.empty?
|
|
215
|
+
end
|
|
216
|
+
|
|
217
|
+
# Where the walk is in the field's selection set.
|
|
218
|
+
def below(field, at, pattern)
|
|
219
|
+
type = field_type(at.type, field)
|
|
220
|
+
uncertain = at.uncertain || at.around.any? { |fragment| !always?(condition(fragment), at.level_type) }
|
|
221
|
+
At.new(pattern: pattern, level_type: type, type: type, around: [], gates: at.gates + at.around + [field], uncertain: uncertain, defers: at.defers)
|
|
222
|
+
end
|
|
223
|
+
|
|
224
|
+
def within(fragment, selections, at, index, stack)
|
|
225
|
+
name = condition(fragment)
|
|
226
|
+
defers = fragment.directives.any? { |directive| directive.name == 'defer' } ? at.defers + [fragment] : at.defers
|
|
227
|
+
collect(selections, at.dup.tap do |inner|
|
|
228
|
+
inner.type = name ? @schema.get_type(name) : at.type
|
|
229
|
+
inner.around = at.around + [fragment]
|
|
230
|
+
inner.defers = defers
|
|
231
|
+
end, index, stack)
|
|
232
|
+
end
|
|
233
|
+
|
|
234
|
+
# Does a type condition hold for every object of `type`?
|
|
235
|
+
def always?(name, type)
|
|
236
|
+
return true if name.nil? || name == type&.graphql_name
|
|
237
|
+
return false unless type&.kind&.object?
|
|
238
|
+
|
|
239
|
+
named = @schema.get_type(name)
|
|
240
|
+
!named.nil? && @schema.possible_types(named).include?(type)
|
|
241
|
+
end
|
|
242
|
+
end
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
# The client of a streamed response went away: writing to its stream failed (the
|
|
4
|
+
# controller raises it from the write). Run stops there, writes nothing more, and
|
|
5
|
+
# ends the operation with what the client was sent.
|
|
6
|
+
class Lighthouse::GraphQL::Incremental::Gone < StandardError; end
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
# The payloads of an incremental response in Apollo's older format, the one graphql-js
|
|
4
|
+
# 17.0.0-alpha.2 answered and a client names with `deferSpec=20220824` in its Accept header
|
|
5
|
+
# (Apollo Client 3, Apollo Client 4's Defer20220824Handler). Run answers the draft's payloads
|
|
6
|
+
# (Serializer); this converts each, as the client is sent it.
|
|
7
|
+
#
|
|
8
|
+
# The first payload is the first pass's result and `hasNext`, without `pending`. Each later one
|
|
9
|
+
# has `incremental`: for a fragment, its `data` at the `path` of its object (the draft's `path`
|
|
10
|
+
# and `subPath`), with its `label`; for a stream, its `items` at the path of the list and the
|
|
11
|
+
# index of its first item. A fragment or a stream that failed sends `data` (or `items`) null,
|
|
12
|
+
# with its `errors`. A fragment that completes with nothing of its own to send (the response
|
|
13
|
+
# already holds it) is sent as empty data at its path, so that the client hears of each one.
|
|
14
|
+
class Lighthouse::GraphQL::Incremental::Legacy
|
|
15
|
+
# `pendings`: the fragments and streams the client was told of, by id (Run's, as it announces them).
|
|
16
|
+
def initialize(pendings)
|
|
17
|
+
@pendings = pendings
|
|
18
|
+
@initial = true
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
# The payload of the draft's format, in this one.
|
|
22
|
+
def convert(payload)
|
|
23
|
+
return initial(payload) if @initial
|
|
24
|
+
|
|
25
|
+
entries = payload['incremental'].to_a
|
|
26
|
+
sent = entries.to_set { |entry| entry['id'] }
|
|
27
|
+
subsequent(entries.map { |entry| entry(entry) } + payload['completed'].to_a.filter_map { |done| completion(done, sent) }, payload)
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
private
|
|
31
|
+
|
|
32
|
+
# What tells the client of a fragment or a stream done: its failure, or, for a fragment that
|
|
33
|
+
# sent nothing (`sent`: the ids of the payload's entries), its empty data; else nothing.
|
|
34
|
+
def completion(done, sent)
|
|
35
|
+
pending = @pendings.fetch(done['id'])
|
|
36
|
+
return failure(pending, done['errors']) if done['errors']
|
|
37
|
+
|
|
38
|
+
{ 'data' => {}, 'path' => pending.path, 'label' => pending.label }.compact unless stream?(pending) || sent.include?(done['id'])
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
def initial(payload)
|
|
42
|
+
@initial = false
|
|
43
|
+
payload.except('pending')
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
def subsequent(incremental, payload)
|
|
47
|
+
converted = {}
|
|
48
|
+
converted['incremental'] = incremental unless incremental.empty?
|
|
49
|
+
converted['extensions'] = payload['extensions'] if payload['extensions']
|
|
50
|
+
converted['hasNext'] = payload['hasNext']
|
|
51
|
+
converted
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
def entry(entry)
|
|
55
|
+
pending = @pendings.fetch(entry['id'])
|
|
56
|
+
converted = if entry.key?('items')
|
|
57
|
+
{ 'items' => entry['items'], 'path' => items_path(pending) }
|
|
58
|
+
else
|
|
59
|
+
{ 'data' => entry['data'], 'path' => pending.path + entry['subPath'].to_a }
|
|
60
|
+
end
|
|
61
|
+
converted.merge({ 'label' => pending.label, 'errors' => entry['errors'], 'extensions' => entry['extensions'] }.compact)
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
def failure(pending, errors)
|
|
65
|
+
key = stream?(pending) ? 'items' : 'data'
|
|
66
|
+
{ key => nil, 'path' => stream?(pending) ? items_path(pending) : pending.path, 'label' => pending.label, 'errors' => errors }
|
|
67
|
+
.reject { |name, value| name == 'label' && value.nil? }
|
|
68
|
+
end
|
|
69
|
+
|
|
70
|
+
# A stream's are announced with the index its later items start at (`initialCount`).
|
|
71
|
+
def stream?(pending) = !pending.offset.nil?
|
|
72
|
+
|
|
73
|
+
# The path of a stream's first later item: the list's, and its index.
|
|
74
|
+
def items_path(pending) = pending.path + [pending.offset]
|
|
75
|
+
end
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'active_support/json'
|
|
4
|
+
require 'delegate'
|
|
5
|
+
require 'time'
|
|
6
|
+
|
|
7
|
+
# The parts of a `multipart/mixed` body (graphql-over-http's IncrementalDelivery RFC), one for
|
|
8
|
+
# each payload of an incremental response, as Body writes them.
|
|
9
|
+
#
|
|
10
|
+
# The body is, as the RFC has it: `\r\n---\r\n`, a part's headers and JSON, and so on for each
|
|
11
|
+
# part, then `\r\n-----\r\n`. Each part ends with the `---` that opens the next one, so a client
|
|
12
|
+
# that reads up to the next boundary has the part the moment it is written, not when the next
|
|
13
|
+
# one follows. A part's JSON is ActiveSupport's, as `render json:` writes a whole response: a
|
|
14
|
+
# Time, a BigDecimal or an object's `as_json` come out the same whichever the client accepts.
|
|
15
|
+
class Lighthouse::GraphQL::Incremental::Multipart
|
|
16
|
+
CONTENT_TYPE = 'multipart/mixed; boundary="-"'
|
|
17
|
+
PART_HEADERS = "Content-Type: application/json; charset=utf-8\r\n\r\n"
|
|
18
|
+
CLOSING = "--\r\n"
|
|
19
|
+
|
|
20
|
+
# The headers of a streamed response. `no-store` keeps it from any cache, and so from a
|
|
21
|
+
# conditional GET, and `Last-Modified` keeps Rack::ETag from buffering it; `X-Accel-Buffering`
|
|
22
|
+
# nginx.
|
|
23
|
+
def self.headers(content_type = CONTENT_TYPE)
|
|
24
|
+
{
|
|
25
|
+
'Content-Type' => content_type,
|
|
26
|
+
'Cache-Control' => 'no-store',
|
|
27
|
+
'X-Accel-Buffering' => 'no',
|
|
28
|
+
'Last-Modified' => Time.now.httpdate
|
|
29
|
+
}
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
# The parts of a multipart/mixed body: each payload's JSON, between boundaries.
|
|
33
|
+
def self.parse(body)
|
|
34
|
+
body.to_s.split(/\r\n-{3,5}(?:\r\n|\z)/).filter_map do |part|
|
|
35
|
+
json = part.split("\r\n\r\n", 2)[1]
|
|
36
|
+
JSON.parse(json) if json && !json.strip.empty?
|
|
37
|
+
end
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
# A streamed response as EndRequest carries it: its body the parts written (the client read
|
|
41
|
+
# the response's own as it went), joined when read.
|
|
42
|
+
class Response < SimpleDelegator
|
|
43
|
+
def initialize(response, parts)
|
|
44
|
+
super(response)
|
|
45
|
+
@parts = parts
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
def body = @body ||= @parts.join
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
# `keep`: keep the parts written, for EndRequest's body (only when something listens to it).
|
|
52
|
+
def initialize(keep: true)
|
|
53
|
+
@started = false
|
|
54
|
+
@written = keep ? [] : nil
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
# The part of `payload`, encoded: an error raised here is the app's values'.
|
|
58
|
+
def part(payload)
|
|
59
|
+
"#{@started ? "\r\n" : "\r\n---\r\n"}#{PART_HEADERS}#{ActiveSupport::JSON.encode(payload)}\r\n---"
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
# A part (or the closing boundary) was written.
|
|
63
|
+
def written(part)
|
|
64
|
+
@written&.push(part)
|
|
65
|
+
@started = true
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
# Has a part been written?
|
|
69
|
+
def started? = @started
|
|
70
|
+
|
|
71
|
+
# The closing boundary, once a part was written.
|
|
72
|
+
def closing = (CLOSING if @started)
|
|
73
|
+
|
|
74
|
+
# `response` as EndRequest carries it (Response): its body the parts kept.
|
|
75
|
+
def ended(response) = Response.new(response, @written.to_a)
|
|
76
|
+
end
|
|
@@ -0,0 +1,249 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
# What a round of incremental delivery runs (Run#next_payload): the units of
|
|
4
|
+
# work of its pending fragments and lists, each a GraphQL::Query::Partial.
|
|
5
|
+
#
|
|
6
|
+
# As the specification draft has it, a deferred fragment's fields that the
|
|
7
|
+
# response already holds at its path (the rest of the query asked for them, or
|
|
8
|
+
# another fragment delivered them) are neither resolved nor sent again: each
|
|
9
|
+
# field resolves once. An object field the response already holds, to which a
|
|
10
|
+
# fragment adds selections, is not resolved again either: its object (kept by
|
|
11
|
+
# StreamTrace when it resolved) gets units of its own, sent at their `subPath`.
|
|
12
|
+
#
|
|
13
|
+
# The fields the fragments of a round select at a path make one unit per set of
|
|
14
|
+
# fragments that select them (the draft's grouped field sets): a field two of
|
|
15
|
+
# them select is sent once, for both, and fails both if it fails; a field only
|
|
16
|
+
# one of them selects is that one's, and fails it alone. The draft groups the
|
|
17
|
+
# fields below an object field the same way: when the fragments that select a
|
|
18
|
+
# field select different fields of its object, the field resolves on its own
|
|
19
|
+
# first (a pass), and those are planned on its object as if the response held
|
|
20
|
+
# it (`follow`), in the round's next pass.
|
|
21
|
+
#
|
|
22
|
+
# The deferred fragments nested in a unit's fragments, at the same path, are found
|
|
23
|
+
# here and pending for the next round, without a pass to find them.
|
|
24
|
+
class Lighthouse::GraphQL::Incremental::Plan
|
|
25
|
+
Nodes = ::GraphQL::Language::Nodes
|
|
26
|
+
State = Lighthouse::GraphQL::Incremental::State
|
|
27
|
+
KNOWN = %w[skip include defer].freeze
|
|
28
|
+
# What a field that resolves on its own selects of its object, left out of the data.
|
|
29
|
+
PLACEHOLDER = '__lighthouse'
|
|
30
|
+
PLACEHOLDER_NODE = Nodes::Field.new(name: '__typename', field_alias: PLACEHOLDER)
|
|
31
|
+
|
|
32
|
+
# The object a fragment adds selections to was not kept (StreamTrace keeps every one a fragment
|
|
33
|
+
# may need before it resolves): a bug, which fails the round.
|
|
34
|
+
class Lost < StandardError; end
|
|
35
|
+
|
|
36
|
+
# A Partial's work: `selections` on `object` (of `type`) at `path`, delivered for the pending
|
|
37
|
+
# fragments that select them (`pendings`, `owner` the first), which complete with it, and the
|
|
38
|
+
# fields it resolves on their own (`follow`). A stream's: its items, on the list's object.
|
|
39
|
+
Unit = Struct.new(:owner, :pendings, :path, :object, :type, :selections, :follow, keyword_init: true) do
|
|
40
|
+
def stream? = owner.stream?
|
|
41
|
+
|
|
42
|
+
# The GraphQL::Query::Partial that runs the unit, part of `query`.
|
|
43
|
+
def partial(query, state)
|
|
44
|
+
return stream(query, state) if stream?
|
|
45
|
+
|
|
46
|
+
fragment = Nodes::InlineFragment.new(selections: selections)
|
|
47
|
+
::GraphQL::Query::Partial.new(object: object, path: path, query: query, fragment_node: fragment, type: type)
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
private
|
|
51
|
+
|
|
52
|
+
# A stream's items run as graphql-ruby runs a list: its field again, with the selections of its
|
|
53
|
+
# declarations and no directive, on the list's object (of its object type, whatever the type
|
|
54
|
+
# conditions above it), answered with the items left (StreamTrace, State#answer), its
|
|
55
|
+
# resolver not run. The Partial counts the items from 0: State puts them where they are.
|
|
56
|
+
def stream(query, state)
|
|
57
|
+
node = state.document.owner(owner.node).merge(selections: owner.selections, directives: [])
|
|
58
|
+
fragment = Nodes::InlineFragment.new(selections: [node])
|
|
59
|
+
partial = ::GraphQL::Query::Partial.new(object: owner.holder, path: path[0...-1], query: query, fragment_node: fragment, type: owner.type)
|
|
60
|
+
stand_in(state, partial, node)
|
|
61
|
+
partial
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
def stand_in(state, partial, node)
|
|
65
|
+
items = owner.object.to_a
|
|
66
|
+
state.stand_in(partial, node, items)
|
|
67
|
+
state.offset(partial, path, items.each_index.map { |index| index + owner.offset })
|
|
68
|
+
end
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
# Where a unit runs: on `object`, of the object type `type`, at `path`.
|
|
72
|
+
Spot = Struct.new(:path, :object, :type)
|
|
73
|
+
|
|
74
|
+
# A field (at `key`, of the type `type`) a unit resolves on its own, and what each fragment
|
|
75
|
+
# selects of it (`pairs`: each pending fragment with a node of the field).
|
|
76
|
+
Follow = Struct.new(:key, :type, :pairs)
|
|
77
|
+
|
|
78
|
+
# `tree`: the response's data so far, as the client has merged it.
|
|
79
|
+
def initialize(query, state, tree)
|
|
80
|
+
@query = query
|
|
81
|
+
@state = state
|
|
82
|
+
@document = state.document
|
|
83
|
+
@schema = query.schema
|
|
84
|
+
@tree = tree
|
|
85
|
+
end
|
|
86
|
+
|
|
87
|
+
# The units of the round's first pass.
|
|
88
|
+
def units(pendings)
|
|
89
|
+
streams, fragments = pendings.partition(&:stream?)
|
|
90
|
+
units = gathered do
|
|
91
|
+
fragments.group_by(&:path).each_value do |group|
|
|
92
|
+
first = group.first
|
|
93
|
+
plan(group.map { |pending| [pending, @document.selections(pending.node)] }, Spot.new(first.path, first.object, first.type))
|
|
94
|
+
end
|
|
95
|
+
end
|
|
96
|
+
streams.map { |pending| Unit.new(owner: pending, pendings: [pending], path: pending.path, object: pending.object, type: pending.type) } + units
|
|
97
|
+
end
|
|
98
|
+
|
|
99
|
+
# The units of the next pass: below the fields the units of the pass that ran (`units`, which
|
|
100
|
+
# answered `responses`) resolved on their own. Their placeholders leave the data.
|
|
101
|
+
def follow(units, responses)
|
|
102
|
+
gathered do
|
|
103
|
+
units.each_with_index do |unit, index|
|
|
104
|
+
data = responses[index]['data'] or next
|
|
105
|
+
unit.follow.to_a.each do |follow|
|
|
106
|
+
value = strip(data[follow.key])
|
|
107
|
+
path = unit.path + [follow.key]
|
|
108
|
+
next if value.nil?
|
|
109
|
+
|
|
110
|
+
targets(path, kept(path), value).each { |target, object| plan(by_pending(follow.pairs), spot(target, object, follow.type)) }
|
|
111
|
+
end
|
|
112
|
+
end
|
|
113
|
+
end
|
|
114
|
+
end
|
|
115
|
+
|
|
116
|
+
private
|
|
117
|
+
|
|
118
|
+
# The units of what the block plans: by path, the fields each fragment selects there.
|
|
119
|
+
def gathered
|
|
120
|
+
@fields = {}
|
|
121
|
+
yield
|
|
122
|
+
@fields.values.flat_map do |spot, fields|
|
|
123
|
+
fields.values.group_by { |pairs| pendings(pairs).map(&:object_id) }.map { |_, sets| unit(spot, sets) }
|
|
124
|
+
end
|
|
125
|
+
end
|
|
126
|
+
|
|
127
|
+
# `entries`: each pending fragment, with the selections it has at `spot`.
|
|
128
|
+
def plan(entries, spot)
|
|
129
|
+
held = at(spot.path)
|
|
130
|
+
held = nil unless held.is_a?(Hash)
|
|
131
|
+
fields = (@fields[spot.path] ||= [spot, {}]).last
|
|
132
|
+
shared = Hash.new { |hash, key| hash[key] = [] }
|
|
133
|
+
entries.each do |pending, selections|
|
|
134
|
+
flatten(selections, spot, pending).each { |selection| sort(selection, pending, held, fields, shared) }
|
|
135
|
+
end
|
|
136
|
+
shared.each { |key, pairs| descend(key, pairs, held[key], spot) unless held[key].nil? }
|
|
137
|
+
end
|
|
138
|
+
|
|
139
|
+
# A field the response holds goes to `shared` (when it adds selections to its object), with the
|
|
140
|
+
# fragment it is of; any other selection to `fields`, by response key (a fragment kept as it is,
|
|
141
|
+
# on its own).
|
|
142
|
+
def sort(selection, pending, held, fields, shared)
|
|
143
|
+
key = selection.is_a?(Nodes::Field) ? selection.alias || selection.name : selection
|
|
144
|
+
return (fields[key] ||= []) << [pending, selection] unless held&.key?(key)
|
|
145
|
+
|
|
146
|
+
shared[key] << [pending, selection] unless selection.selections.empty?
|
|
147
|
+
end
|
|
148
|
+
|
|
149
|
+
# The unit of the fields `sets` (one list of pairs a field) at `spot`, which the same fragments select.
|
|
150
|
+
def unit(spot, sets)
|
|
151
|
+
pendings = pendings(sets.first)
|
|
152
|
+
follow = []
|
|
153
|
+
selections = sets.flat_map do |pairs|
|
|
154
|
+
next pairs.map(&:last).uniq(&:object_id) unless apart?(pairs)
|
|
155
|
+
|
|
156
|
+
field = pairs.first.last
|
|
157
|
+
follow << Follow.new(field.alias || field.name, @document.field_type(spot.type, field), pairs)
|
|
158
|
+
pairs.map { |_, node| node.merge(selections: [PLACEHOLDER_NODE]) }
|
|
159
|
+
end
|
|
160
|
+
Unit.new(owner: pendings.first, pendings: pendings, path: spot.path, object: spot.object, type: spot.type, selections: selections,
|
|
161
|
+
follow: follow)
|
|
162
|
+
end
|
|
163
|
+
|
|
164
|
+
# The fragments of `pairs`, in the order they were announced.
|
|
165
|
+
def pendings(pairs) = pairs.map(&:first).uniq(&:object_id).sort_by { |pending| pending.id.to_i }
|
|
166
|
+
|
|
167
|
+
# Do the fragments that select an object field select different fields of its object?
|
|
168
|
+
def apart?(pairs)
|
|
169
|
+
return false unless pairs.first.last.is_a?(Nodes::Field) && pairs.any? { |_, node| !node.selections.empty? }
|
|
170
|
+
|
|
171
|
+
by_pending(pairs).map { |_, selections| selections.map(&:object_id) }.uniq.size > 1
|
|
172
|
+
end
|
|
173
|
+
|
|
174
|
+
# The fields `selections` select on the object, through the fragments that apply to it;
|
|
175
|
+
# a fragment with a directive other than @skip, @include or @defer is kept as it is. A
|
|
176
|
+
# deferred one is pending for the next round, under `pending`.
|
|
177
|
+
def flatten(selections, spot, pending)
|
|
178
|
+
selections.flat_map do |selection|
|
|
179
|
+
next [] unless @document.included?(selection, @query)
|
|
180
|
+
|
|
181
|
+
selection.is_a?(Nodes::Field) ? [selection] : fragment(selection, spot, pending)
|
|
182
|
+
end
|
|
183
|
+
end
|
|
184
|
+
|
|
185
|
+
def fragment(fragment, spot, pending)
|
|
186
|
+
return [] unless @document.applies?(fragment, spot.type, @query)
|
|
187
|
+
return defer(fragment, spot, pending) if @state.deferring && @document.deferred?(fragment, @query)
|
|
188
|
+
return [fragment] if fragment.directives.any? { |directive| KNOWN.exclude?(directive.name) }
|
|
189
|
+
|
|
190
|
+
flatten(@document.selections(fragment), spot, pending)
|
|
191
|
+
end
|
|
192
|
+
|
|
193
|
+
def defer(fragment, spot, parent)
|
|
194
|
+
directive = fragment.directives.find { |candidate| candidate.name == 'defer' }
|
|
195
|
+
label = @query.arguments_for(directive, @document.directive('defer'))[:label]
|
|
196
|
+
@state.found(self, State::Pending.new(node: fragment, object: spot.object, type: spot.type, path: spot.path, label: label,
|
|
197
|
+
parents: [parent]))
|
|
198
|
+
[]
|
|
199
|
+
end
|
|
200
|
+
|
|
201
|
+
# The selections `pairs` (each pending fragment with a node of the field) add to the object (or
|
|
202
|
+
# each object of the list) the response holds at `key`.
|
|
203
|
+
def descend(key, pairs, held, spot)
|
|
204
|
+
type = @document.field_type(spot.type, pairs.first.last) or return
|
|
205
|
+
path = spot.path + [key]
|
|
206
|
+
targets(path, kept(path), held).each { |target, object| plan(by_pending(pairs), spot(target, object, type)) }
|
|
207
|
+
end
|
|
208
|
+
|
|
209
|
+
# Each pending fragment of `pairs` with the selections of its nodes.
|
|
210
|
+
def by_pending(pairs)
|
|
211
|
+
pairs.group_by { |pending, _| pending.object_id }.map { |_, own| [own.first.first, own.flat_map { |_, node| node.selections }] }
|
|
212
|
+
end
|
|
213
|
+
|
|
214
|
+
# The object StreamTrace kept at `path`.
|
|
215
|
+
def kept(path)
|
|
216
|
+
value = @state.object_at(path)
|
|
217
|
+
raise Lost, "Lighthouse kept no object at #{path.join('.')}" if value.equal?(State::MISSING)
|
|
218
|
+
|
|
219
|
+
value
|
|
220
|
+
end
|
|
221
|
+
|
|
222
|
+
# The object at `path`, or those of the list there (the response holds `held`), each with its
|
|
223
|
+
# path; none where the response holds null.
|
|
224
|
+
def targets(path, value, held)
|
|
225
|
+
return [[path, value]] unless held.is_a?(Array)
|
|
226
|
+
|
|
227
|
+
objects = value.respond_to?(:to_a) ? value.to_a : []
|
|
228
|
+
held.each_index.filter_map { |index| [path + [index], objects[index]] unless held[index].nil? || objects[index].nil? }
|
|
229
|
+
end
|
|
230
|
+
|
|
231
|
+
# Where an object of `type` (its object type, if `type` is abstract) is at `path`.
|
|
232
|
+
def spot(path, object, type)
|
|
233
|
+
return Spot.new(path, object, type) unless type.kind.abstract?
|
|
234
|
+
|
|
235
|
+
resolved = @schema.sync_lazy(@query.resolve_type(type, object))
|
|
236
|
+
Spot.new(path, object, resolved.is_a?(Array) ? resolved.first : resolved)
|
|
237
|
+
end
|
|
238
|
+
|
|
239
|
+
# `value` without the placeholders a field that resolved on its own selected.
|
|
240
|
+
def strip(value)
|
|
241
|
+
case value
|
|
242
|
+
when Hash then value.tap { |object| object.delete(PLACEHOLDER) }
|
|
243
|
+
when Array then value.each { |item| strip(item) }
|
|
244
|
+
else value
|
|
245
|
+
end
|
|
246
|
+
end
|
|
247
|
+
|
|
248
|
+
def at(path) = @tree.at(path)
|
|
249
|
+
end
|