lighthouse-graphql 0.8.1 → 0.9.0
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 +166 -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 -1
- data/docs/events.md +30 -1
- data/docs/filtering-and-ordering.md +488 -0
- data/docs/getting-started.md +1 -1
- data/docs/performance.md +9 -0
- data/docs/relay.md +27 -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 +30 -1
- data/lib/lighthouse/directive_resolvers/argument_binding.rb +1 -3
- data/lib/lighthouse/directive_resolvers/argument_transforms_resolver.rb +14 -4
- data/lib/lighthouse/directive_resolvers/association_resolvers.rb +1 -0
- data/lib/lighthouse/directive_resolvers/base.rb +99 -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_resolver.rb +6 -0
- data/lib/lighthouse/directive_resolvers/global_id_types.rb +83 -0
- data/lib/lighthouse/directive_resolvers/model_mutation_resolver.rb +2 -0
- data/lib/lighthouse/directive_resolvers/paginate_resolver.rb +27 -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/global_id.rb +28 -0
- 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/search/auto.rb +21 -0
- data/lib/lighthouse/search/enhancer.rb +127 -0
- data/lib/lighthouse/search/field_check.rb +171 -0
- data/lib/lighthouse/search/pg_search.rb +76 -0
- data/lib/lighthouse/search/query.rb +35 -0
- data/lib/lighthouse/search/result.rb +181 -0
- data/lib/lighthouse/search/searchkick.rb +292 -0
- data/lib/lighthouse/search/searchkick_index.rb +54 -0
- data/lib/lighthouse/search/searchkick_options.rb +74 -0
- data/lib/lighthouse/search.rb +78 -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 +132 -0
- data/sig/lighthouse.rbs +69 -1
- metadata +39 -1
|
@@ -17,6 +17,9 @@ require_relative 'base'
|
|
|
17
17
|
# ActionController::TooManyRequests, which Rails answers with a 429
|
|
18
18
|
# (`head :too_many_requests` on a Rails without it, 7.2). Without Action
|
|
19
19
|
# Pack at all, the field fails: "Rate limit for Query.x exceeded. Try again later."
|
|
20
|
+
# So does it where the response is already on its way (a later part of a
|
|
21
|
+
# streamed @defer response, see Defer): it can no longer be a 429, nor
|
|
22
|
+
# anything `with:` would render, so `with:` is not called.
|
|
20
23
|
# name: as rate_limit's: the field by default (Query.x). Fields, and the
|
|
21
24
|
# controller's own `rate_limit`, of the same name count together.
|
|
22
25
|
# prefix: as rate_limit's scope: the controller's path by default.
|
|
@@ -112,16 +115,24 @@ class Lighthouse::GraphQL::DirectiveResolvers::ThrottleDirectiveResolver < Light
|
|
|
112
115
|
# Past the limit: instrumented as rate_limit's, around `with`, after which a
|
|
113
116
|
# response `with` performed ends the request, as a before_action's would.
|
|
114
117
|
def refuse(controller, limit, location, counted)
|
|
118
|
+
committed = committed?(controller)
|
|
115
119
|
payload = { request: controller&.request, to: limit[:to], within: limit[:within], **counted }
|
|
116
|
-
ActiveSupport::Notifications.instrument('rate_limit.action_controller', payload) { limited(controller, limit[:with], location) }
|
|
120
|
+
ActiveSupport::Notifications.instrument('rate_limit.action_controller', payload) { limited(controller, limit[:with], location, committed) }
|
|
117
121
|
raise Lighthouse::GraphQL::Controller::Performed if controller&.performed?
|
|
118
122
|
end
|
|
119
123
|
|
|
120
|
-
#
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
124
|
+
# Is the controller's response on its way (a later part of a streamed @defer response)? It
|
|
125
|
+
# can then be neither a 429 nor what `with:` renders.
|
|
126
|
+
def committed?(controller) = controller.respond_to?(:response) && controller.response.respond_to?(:committed?) && controller.response.committed?
|
|
127
|
+
|
|
128
|
+
# The controller's `with` method, else rate_limit's default `with`; a field error once the
|
|
129
|
+
# response is `committed`.
|
|
130
|
+
def limited(controller, with, location, committed)
|
|
131
|
+
unless committed
|
|
132
|
+
return call_with(controller, with, location) if with && controller
|
|
133
|
+
raise ::ActionController::TooManyRequests if defined?(::ActionController::TooManyRequests)
|
|
134
|
+
return controller.head(:too_many_requests) if controller
|
|
135
|
+
end
|
|
125
136
|
|
|
126
137
|
raise ::GraphQL::ExecutionError, "Rate limit for #{location} exceeded. Try again later."
|
|
127
138
|
end
|
|
@@ -25,6 +25,20 @@ class Lighthouse::GraphQL::Directives::Arguments::Base
|
|
|
25
25
|
GRAPHQL
|
|
26
26
|
end
|
|
27
27
|
|
|
28
|
+
# Does the directive, as the SDL writes it (`directive_node`), change the field's
|
|
29
|
+
# query? Most do; one that does not (@limit without `builder: true`, which cuts
|
|
30
|
+
# what the field resolved) leaves a search outside the database alone.
|
|
31
|
+
def self.changes_query?(_directive_node) = true
|
|
32
|
+
|
|
33
|
+
# Does the client's value, once sent, narrow the query? Any value does, null
|
|
34
|
+
# too, as PHP Lighthouse's wouldEnhanceBuilder counts what the client sent; a
|
|
35
|
+
# mass mutation (@delete, ...) refuses to run unless one argument does. @search
|
|
36
|
+
# narrows only for a string, which runs the search.
|
|
37
|
+
def self.filters?(_value) = true
|
|
38
|
+
|
|
39
|
+
# Does the directive run the field's search (@search)?
|
|
40
|
+
def self.searches? = false
|
|
41
|
+
|
|
28
42
|
def initialize(arg_node, directive_node)
|
|
29
43
|
@arg_node = arg_node
|
|
30
44
|
@directive_node = directive_node
|
|
@@ -5,6 +5,8 @@ require_relative 'base'
|
|
|
5
5
|
module Lighthouse::GraphQL::Directives::Arguments
|
|
6
6
|
# @eq — equality filter (the default). `field(status: String @eq)`
|
|
7
7
|
class Eq < Base
|
|
8
|
+
include Lighthouse::Contracts::SearchBuilder
|
|
9
|
+
|
|
8
10
|
def self.directive_name = 'eq'
|
|
9
11
|
def self.definition = sdl('eq', 'Add an equality filter to the query: the column equals the value.')
|
|
10
12
|
|
|
@@ -13,6 +15,10 @@ module Lighthouse::GraphQL::Directives::Arguments
|
|
|
13
15
|
|
|
14
16
|
relation.where(column => value)
|
|
15
17
|
end
|
|
18
|
+
|
|
19
|
+
def handle_search_builder(query, value)
|
|
20
|
+
value.nil? ? query : query.where(column, value)
|
|
21
|
+
end
|
|
16
22
|
end
|
|
17
23
|
|
|
18
24
|
# @whereKey — filter by the model's primary key, whatever its column is
|
|
@@ -158,6 +158,9 @@ module Lighthouse::GraphQL::Directives::Arguments
|
|
|
158
158
|
class Limit < Base
|
|
159
159
|
def self.directive_name = 'limit'
|
|
160
160
|
|
|
161
|
+
# Only with `builder: true`: otherwise LimitResolver cuts the results after.
|
|
162
|
+
def self.changes_query?(directive_node) = Lighthouse::Support::DirectiveArgs.literal(directive_node, 'builder') == true
|
|
163
|
+
|
|
161
164
|
def self.definition
|
|
162
165
|
<<~GRAPHQL.chomp
|
|
163
166
|
"""
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative 'base'
|
|
4
|
+
|
|
5
|
+
# @search -- on a String argument: when the client sends a string (`""` too;
|
|
6
|
+
# null or nothing falls back to the database query), the field's query runs
|
|
7
|
+
# through a full-text engine (see Lighthouse::Search), as PHP Lighthouse's
|
|
8
|
+
# @search does through Laravel Scout.
|
|
9
|
+
#
|
|
10
|
+
# posts(search: String @search, authorId: ID @eq): [Post!]! @paginate
|
|
11
|
+
#
|
|
12
|
+
# `within:` names the index (Searchkick) or the pg_search scope. The directive
|
|
13
|
+
# constrains nothing itself: the field's resolver acts on it
|
|
14
|
+
# (BaseDirectiveResolver#apply_search) before the argument builders.
|
|
15
|
+
class Lighthouse::GraphQL::Directives::Arguments::Search < Lighthouse::GraphQL::Directives::Arguments::Base
|
|
16
|
+
def self.directive_name = 'search'
|
|
17
|
+
|
|
18
|
+
def self.definition
|
|
19
|
+
<<~GRAPHQL.chomp
|
|
20
|
+
"""
|
|
21
|
+
Perform a full-text search by the given input value.
|
|
22
|
+
"""
|
|
23
|
+
directive @search(
|
|
24
|
+
#{argument_sdl('within: String', 'Specify a custom index to use for search.')}
|
|
25
|
+
) on ARGUMENT_DEFINITION | INPUT_FIELD_DEFINITION
|
|
26
|
+
GRAPHQL
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
# The search runs for a string: null, or nothing, leaves the query whole.
|
|
30
|
+
def self.filters?(value) = value.is_a?(String)
|
|
31
|
+
def self.searches? = true
|
|
32
|
+
|
|
33
|
+
def handle_builder(relation, _value) = relation
|
|
34
|
+
end
|
|
@@ -11,6 +11,8 @@ module Lighthouse::GraphQL::Directives::Arguments
|
|
|
11
11
|
#
|
|
12
12
|
# Usually added by @softDeletes: `tasks: [Task!]! @all @softDeletes`.
|
|
13
13
|
class Trashed < Base
|
|
14
|
+
include Lighthouse::Contracts::SearchBuilder
|
|
15
|
+
|
|
14
16
|
def self.directive_name = 'trashed'
|
|
15
17
|
|
|
16
18
|
def self.definition
|
|
@@ -27,6 +29,15 @@ module Lighthouse::GraphQL::Directives::Arguments
|
|
|
27
29
|
|
|
28
30
|
Lighthouse::Support::SoftDeletes.filter(relation, value)
|
|
29
31
|
end
|
|
32
|
+
|
|
33
|
+
# In a search, the engine decides what trashed records it holds (see Search::Searchkick).
|
|
34
|
+
def handle_search_builder(query, value)
|
|
35
|
+
return query if value.nil?
|
|
36
|
+
|
|
37
|
+
Lighthouse::Support::SoftDeletes.require_support!(query.model)
|
|
38
|
+
query.trashed = Lighthouse::Support::SoftDeletes.trashed(value)
|
|
39
|
+
query
|
|
40
|
+
end
|
|
30
41
|
end
|
|
31
42
|
|
|
32
43
|
# @withoutGlobalScopes(names: [...]) — on a Boolean argument: when the client
|
|
@@ -69,7 +69,7 @@ class Lighthouse::GraphQL::Directives::ModelDirective
|
|
|
69
69
|
next unless name
|
|
70
70
|
|
|
71
71
|
model = declared_model(type)
|
|
72
|
-
next if model
|
|
72
|
+
next if Lighthouse::Support::ModelResolver.model?(model)
|
|
73
73
|
|
|
74
74
|
raise Lighthouse::DefinitionError, "@model(class: #{name.inspect}) on #{type.graphql_name} names no ActiveRecord model."
|
|
75
75
|
end
|
|
@@ -209,7 +209,7 @@ class Lighthouse::DirectiveRegistry
|
|
|
209
209
|
# Argument-builder filter/sort directives.
|
|
210
210
|
ARG_BUILDERS = %i[
|
|
211
211
|
Eq Neq In NotIn Like Where WhereKey OrderBy WhereConditions WhereHasConditions Trashed WithoutGlobalScopes
|
|
212
|
-
WhereNull WhereNotNull WhereBetween WhereNotBetween WhereJsonContains Scope Limit
|
|
212
|
+
WhereNull WhereNotNull WhereBetween WhereNotBetween WhereJsonContains Scope Limit Search
|
|
213
213
|
].freeze
|
|
214
214
|
|
|
215
215
|
def register_defaults
|
|
@@ -5,6 +5,10 @@
|
|
|
5
5
|
# ManipulateResult, EndExecution) around each query of a multiplex. A query
|
|
6
6
|
# runs through a multiplex even on its own. With nothing listening it adds one
|
|
7
7
|
# lookup per request.
|
|
8
|
+
#
|
|
9
|
+
# A deferred fragment runs as a GraphQL::Query::Partial of the operation's query
|
|
10
|
+
# (Incremental::Run): one more multiplex, with its own StartExecution and
|
|
11
|
+
# EndExecution, which carry the operation's document, variables and name.
|
|
8
12
|
module Lighthouse::Events::ExecutionTrace
|
|
9
13
|
EVENTS = [
|
|
10
14
|
Lighthouse::Events::StartExecution, Lighthouse::Events::BuildExtensionsResponse,
|
|
@@ -22,8 +26,9 @@ module Lighthouse::Events::ExecutionTrace
|
|
|
22
26
|
|
|
23
27
|
class << self
|
|
24
28
|
def start(schema, query)
|
|
29
|
+
source = Lighthouse::Events.operation_query(query)
|
|
25
30
|
Lighthouse::Events.dispatch(
|
|
26
|
-
Lighthouse::Events::StartExecution.new(schema,
|
|
31
|
+
Lighthouse::Events::StartExecution.new(schema, source.document, source.provided_variables, source.operation_name, query.context, query)
|
|
27
32
|
)
|
|
28
33
|
end
|
|
29
34
|
|
|
@@ -79,7 +79,8 @@ module Lighthouse::Events
|
|
|
79
79
|
# Array of them), which is merged into the result's "extensions".
|
|
80
80
|
# `query_complexity` is the query's complexity score, as graphql-ruby checks it
|
|
81
81
|
# against max_complexity (see GraphQL::Complexity); nil for a query that
|
|
82
|
-
# failed validation, which is never scored
|
|
82
|
+
# failed validation, which is never scored, and for the later parts of a
|
|
83
|
+
# streamed response (@defer), whose operation's score comes with the first.
|
|
83
84
|
class BuildExtensionsResponse < Event
|
|
84
85
|
attr_reader :result, :query_complexity
|
|
85
86
|
|
data/lib/lighthouse/events.rb
CHANGED
|
@@ -25,6 +25,8 @@ module Lighthouse::Events
|
|
|
25
25
|
end
|
|
26
26
|
end
|
|
27
27
|
|
|
28
|
+
PARTIAL = (::GraphQL::Query::Partial if defined?(::GraphQL::Query::Partial))
|
|
29
|
+
|
|
28
30
|
@listeners = [].freeze
|
|
29
31
|
@mutex = Mutex.new
|
|
30
32
|
|
|
@@ -68,6 +70,13 @@ module Lighthouse::Events
|
|
|
68
70
|
::ActiveSupport::Notifications.notifier.listening?(notification_name(event_class))
|
|
69
71
|
end
|
|
70
72
|
|
|
73
|
+
# The query of the operation `query` runs: a GraphQL::Query::Partial (a deferred fragment's
|
|
74
|
+
# run, see Defer) runs part of one; a GraphQL::Query is its own. graphql-ruby has Partial from
|
|
75
|
+
# 2.5.6 (PARTIAL, nil before), and only Defer, which needs a later one, runs them.
|
|
76
|
+
def operation_query(query)
|
|
77
|
+
PARTIAL && query.is_a?(PARTIAL) ? query.query : query
|
|
78
|
+
end
|
|
79
|
+
|
|
71
80
|
# The ActiveSupport::Notifications name an event class is instrumented as.
|
|
72
81
|
def notification_name(event_class)
|
|
73
82
|
"#{(event_class.name || 'event').demodulize.underscore}.lighthouse"
|
|
@@ -16,6 +16,7 @@ module Lighthouse::GraphQL::FieldResolverStack
|
|
|
16
16
|
# resolve its fields that have no resolving directive.
|
|
17
17
|
def apply(type, field_def, root_kind, bases: {}, type_class: nil)
|
|
18
18
|
directives = field_def.ast_node.directives || []
|
|
19
|
+
Lighthouse::Search::FieldCheck.check!(type, field_def, directives, root_kind, type_class: type_class)
|
|
19
20
|
opts = { root_kind: root_kind, bases: bases }
|
|
20
21
|
steps = directive_steps(type, field_def, directives, opts)
|
|
21
22
|
steps << limit_step(type, field_def, opts) if Lighthouse::GraphQL::DirectiveResolvers::LimitResolver.keyword(field_def)
|
|
@@ -38,11 +39,22 @@ module Lighthouse::GraphQL::FieldResolverStack
|
|
|
38
39
|
end
|
|
39
40
|
end
|
|
40
41
|
|
|
41
|
-
#
|
|
42
|
-
# another resolver),
|
|
43
|
-
def resolving_directive
|
|
42
|
+
# The directive that resolves the field itself (a wrapper only decorates
|
|
43
|
+
# another resolver), or nil: then the field needs a conventional one.
|
|
44
|
+
def resolving_directive(directives)
|
|
44
45
|
registry = Lighthouse::DirectiveRegistry.default
|
|
45
|
-
directives.
|
|
46
|
+
directives.find { |d| registry.resolution_class(d.name) && !registry.wrapper?(d.name) }
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
def resolving_directive?(directives) = !resolving_directive(directives).nil?
|
|
50
|
+
|
|
51
|
+
# Does the app's own code resolve a field with no resolving directive (own_step):
|
|
52
|
+
# the conventional class of a root Query or Mutation field, or a method of the
|
|
53
|
+
# type's class? Otherwise graphql-ruby's default reader reads it off the parent.
|
|
54
|
+
def own_resolver?(field_def, root_kind, type_class)
|
|
55
|
+
return true if %i[queries mutations].include?(root_kind)
|
|
56
|
+
|
|
57
|
+
!!(type_class && Lighthouse::GraphQL::DirectiveResolvers::TypeMethodResolver.method_for(type_class, field_def))
|
|
46
58
|
end
|
|
47
59
|
|
|
48
60
|
def convention_step(type, field_def, root_kind, bases = {})
|
|
@@ -117,31 +129,20 @@ module Lighthouse::GraphQL::FieldResolverStack
|
|
|
117
129
|
def argument_authorization_steps(type, field_def, opts)
|
|
118
130
|
resolver = Lighthouse::GraphQL::DirectiveResolvers::CanModelDirectiveResolver
|
|
119
131
|
order = Lighthouse::DirectiveRegistry.default.resolution_order(resolver.directive_name)
|
|
120
|
-
authorized_arguments(field_def
|
|
132
|
+
authorized_arguments(field_def).map do |path, dnode|
|
|
121
133
|
[order, -> { resolver.new(field_def, type, dnode, opts.merge(argument: path)).define_resolver(type) }]
|
|
122
134
|
end
|
|
123
135
|
end
|
|
124
136
|
|
|
125
137
|
# [dot path ("filter.trashed", "inputs.*.status"), @canModel node] for every
|
|
126
|
-
# argument and input field carrying @canModel
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
own = (argument.ast_node&.directives || []).select { |d| d.name == 'canModel' }.map { |dnode| [path, dnode] }
|
|
132
|
-
own + authorized_input_fields(argument, path, seen)
|
|
138
|
+
# argument and input field carrying @canModel (Support::ArgumentWalk).
|
|
139
|
+
def authorized_arguments(field_def)
|
|
140
|
+
walk = Lighthouse::Support::ArgumentWalk
|
|
141
|
+
walk.field_definitions(field_def).flat_map do |path, argument|
|
|
142
|
+
walk.directives(argument).select { |d| d.name == 'canModel' }.map { |dnode| [path, dnode] }
|
|
133
143
|
end
|
|
134
144
|
end
|
|
135
145
|
|
|
136
|
-
# Those of the input type an argument takes; `*` steps into each of a list.
|
|
137
|
-
def authorized_input_fields(argument, path, seen)
|
|
138
|
-
type = argument.type.unwrap
|
|
139
|
-
return [] unless type.kind.input_object? && seen.exclude?(type.graphql_name)
|
|
140
|
-
|
|
141
|
-
list = (argument.type.non_null? ? argument.type.of_type : argument.type).list?
|
|
142
|
-
authorized_arguments(type.arguments, list ? "#{path}.*" : path, seen | [type.graphql_name])
|
|
143
|
-
end
|
|
144
|
-
|
|
145
146
|
# Around whatever resolves the field, as @lazyLoad: the result is cut to the client's @limit.
|
|
146
147
|
def limit_step(type, field_def, opts)
|
|
147
148
|
[1, -> { Lighthouse::GraphQL::DirectiveResolvers::LimitResolver.new(field_def, type, nil, opts).define_resolver(type) }]
|
data/lib/lighthouse/global_id.rb
CHANGED
|
@@ -60,6 +60,34 @@ module Lighthouse::GraphQL::GlobalId
|
|
|
60
60
|
gid ? [gid.model_name.demodulize, gid.model_id] : decode_object(global_id, context)
|
|
61
61
|
end
|
|
62
62
|
|
|
63
|
+
# Is `global_id`, decoded as `type`, an id of `model` or of a subclass (STI)?
|
|
64
|
+
# A Rails GlobalID names its model class; another names a GraphQL type, whose
|
|
65
|
+
# model is its @model(class:), else the class of its name.
|
|
66
|
+
def of_model?(global_id, type, model, context)
|
|
67
|
+
klass = rails_model(global_id) || type_model(type, context)
|
|
68
|
+
klass.is_a?(Class) && klass <= model
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
def rails_model(global_id)
|
|
72
|
+
return unless defined?(::GlobalID) && global_id.is_a?(String)
|
|
73
|
+
|
|
74
|
+
::GlobalID.parse(global_id)&.model_class
|
|
75
|
+
rescue NameError
|
|
76
|
+
nil
|
|
77
|
+
end
|
|
78
|
+
|
|
79
|
+
# The model of the GraphQL type (or model) named `name`.
|
|
80
|
+
def type_model(name, context)
|
|
81
|
+
Lighthouse::Support::ModelResolver.constantize(declared_model(name, context) || name)
|
|
82
|
+
end
|
|
83
|
+
|
|
84
|
+
# The @model(class:) of the schema's type of that name.
|
|
85
|
+
def declared_model(name, context)
|
|
86
|
+
schema_type = context.schema.get_type(name.to_s) if context.respond_to?(:schema) && context.schema
|
|
87
|
+
directives = schema_type.respond_to?(:ast_node) ? schema_type.ast_node&.directives : nil
|
|
88
|
+
Lighthouse::Support::DirectiveArgs.literal(directives&.find { |d| d.name == 'model' }, 'class')
|
|
89
|
+
end
|
|
90
|
+
|
|
63
91
|
# [type, id] of the object the schema's object_from_id finds.
|
|
64
92
|
def decode_object(global_id, context)
|
|
65
93
|
object = context.schema.object_from_id(global_id, context)
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
# The body of a streamed response: a Rack body the server iterates, writing each part of the
|
|
4
|
+
# `multipart/mixed` response as Run answers its payload, as Rails streams a template
|
|
5
|
+
# (`self.response_body =`): in the request's thread, inside its executor, with no thread of its
|
|
6
|
+
# own. The action has returned by then: what happens next is the body's.
|
|
7
|
+
#
|
|
8
|
+
# The first part was encoded in the action (`first`), where an error is answered as the JSON
|
|
9
|
+
# path answers it. Then each round runs as the server asks for the next part. Whatever fails, the
|
|
10
|
+
# stream ends here: Run has yielded the last payload an error ends it with, the error is reported
|
|
11
|
+
# (`on_error`) and goes no further, as the response is on its way, and the closing boundary is
|
|
12
|
+
# written. A client that went away (the server's write raised) ends it quietly. Last, as the
|
|
13
|
+
# body is done (`finish`, once), the operation ends (Run#finish), then the request (`on_end`).
|
|
14
|
+
#
|
|
15
|
+
# Rails maps a Rack body's `close` to nothing (Response#abort), so `finish` runs when `each`
|
|
16
|
+
# is done, and from the server's `rack.response_finished` callbacks, where there are, for a body
|
|
17
|
+
# never iterated.
|
|
18
|
+
class Lighthouse::GraphQL::Incremental::Body
|
|
19
|
+
Gone = Lighthouse::GraphQL::Incremental::Gone
|
|
20
|
+
|
|
21
|
+
def initialize(run, multipart, first, on_error:, on_end:)
|
|
22
|
+
@run = run
|
|
23
|
+
@multipart = multipart
|
|
24
|
+
@first = first
|
|
25
|
+
@on_error = on_error
|
|
26
|
+
@on_end = on_end
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
def each(&write)
|
|
30
|
+
@run.each_payload { |payload| emit(write, payload.equal?(@run.first_payload) ? @first : @multipart.part(payload)) }
|
|
31
|
+
emit(write, @multipart.closing)
|
|
32
|
+
rescue Gone
|
|
33
|
+
nil
|
|
34
|
+
rescue StandardError => e
|
|
35
|
+
@on_error.call(e)
|
|
36
|
+
close_after(write)
|
|
37
|
+
ensure
|
|
38
|
+
finish
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
# Ends the operation, then the request, once.
|
|
42
|
+
def finish
|
|
43
|
+
return if @finished
|
|
44
|
+
|
|
45
|
+
@finished = true
|
|
46
|
+
begin
|
|
47
|
+
@run.finish
|
|
48
|
+
ensure
|
|
49
|
+
@on_end.call
|
|
50
|
+
end
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
private
|
|
54
|
+
|
|
55
|
+
# Writes a part. An error the server raises writing it is a client gone (Gone).
|
|
56
|
+
def emit(write, part)
|
|
57
|
+
return unless part
|
|
58
|
+
|
|
59
|
+
begin
|
|
60
|
+
write.call(part)
|
|
61
|
+
rescue StandardError
|
|
62
|
+
raise Gone
|
|
63
|
+
end
|
|
64
|
+
@multipart.written(part)
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
# The closing boundary of a stream an error ended, once that error was reported: a client gone
|
|
68
|
+
# by then ends it quietly, and an error writing it is reported in turn.
|
|
69
|
+
def close_after(write)
|
|
70
|
+
emit(write, @multipart.closing)
|
|
71
|
+
rescue Gone
|
|
72
|
+
nil
|
|
73
|
+
rescue StandardError => e
|
|
74
|
+
@on_error.call(e)
|
|
75
|
+
end
|
|
76
|
+
end
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
# @defer, as the specification draft has it (graphql-spec PR #1110): on a
|
|
4
|
+
# fragment, it asks for the fragment's fields after the rest of the response.
|
|
5
|
+
#
|
|
6
|
+
# It is a graphql-ruby execution directive: `include?` decides whether the
|
|
7
|
+
# runtime runs the fragment where it stands. With an Incremental::State in the
|
|
8
|
+
# context (Run puts one there when the response may stream), the
|
|
9
|
+
# fragment is left out and kept in the State, to run once the first response is
|
|
10
|
+
# out. Without one, `include?` is true and the directive does nothing: a server
|
|
11
|
+
# may ignore @defer, and answers the whole response at once.
|
|
12
|
+
#
|
|
13
|
+
# `include?` is given the directive's arguments, not its node: State knows the
|
|
14
|
+
# fragment by them (State#directive_for). The document is never rewritten.
|
|
15
|
+
class Lighthouse::GraphQL::Incremental::DeferDirective < GraphQL::Schema::Directive
|
|
16
|
+
graphql_name 'defer'
|
|
17
|
+
description 'Deliver the fields of a fragment in a later part when the response is streamed (multipart/mixed, ' \
|
|
18
|
+
'to a client that prefers it to JSON); with the rest otherwise (experimental).'
|
|
19
|
+
locations ::GraphQL::Schema::Directive::FRAGMENT_SPREAD, ::GraphQL::Schema::Directive::INLINE_FRAGMENT
|
|
20
|
+
|
|
21
|
+
argument :if, ::GraphQL::Types::Boolean, required: true, default_value: true,
|
|
22
|
+
description: 'Defer the fragment only when true.'
|
|
23
|
+
argument :label, ::GraphQL::Types::String, required: false,
|
|
24
|
+
description: 'Names the fragment in the streamed response. Unique in the document, and not a variable.'
|
|
25
|
+
|
|
26
|
+
class << self
|
|
27
|
+
def include?(object, arguments, context)
|
|
28
|
+
state = context[Lighthouse::GraphQL::Incremental::State::CONTEXT_KEY]
|
|
29
|
+
return true if state.nil? || arguments[:if] == false
|
|
30
|
+
|
|
31
|
+
!state.defer(object, arguments, context)
|
|
32
|
+
end
|
|
33
|
+
end
|
|
34
|
+
end
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
# What a round delivers (Run#next_payload), from the responses of its units
|
|
4
|
+
# (Plan): the `incremental` entries, the fragments and lists that failed, with
|
|
5
|
+
# their errors, and the payload's own `extensions`. The data it delivers goes in
|
|
6
|
+
# the response's Tree.
|
|
7
|
+
#
|
|
8
|
+
# A unit is what the draft calls a grouped field set: its data belongs to every
|
|
9
|
+
# pending fragment it serves (`pendings`: those that select its fields). A
|
|
10
|
+
# fragment fails when a unit it depends on leaves no data (a non-null field
|
|
11
|
+
# failed beyond the unit's root), and a fragment that fails delivers no data, as
|
|
12
|
+
# the draft has it: a unit that serves only fragments that failed is withheld. A
|
|
13
|
+
# unit that also serves one that did not is delivered, under the id of that one:
|
|
14
|
+
# the field it shares reaches the client through the fragment that succeeded.
|
|
15
|
+
# Another fragment's failure does not fail a fragment.
|
|
16
|
+
#
|
|
17
|
+
# A stream fails when an item it delivers is null (or an error nulls it) where the
|
|
18
|
+
# list's items are non-null, as the list would have in one response.
|
|
19
|
+
#
|
|
20
|
+
# An entry that carries nothing (no data, or no items, and no errors) is not
|
|
21
|
+
# sent: its extensions, and those of a withheld unit, go on the payload's.
|
|
22
|
+
class Lighthouse::GraphQL::Incremental::Delivery
|
|
23
|
+
Outcome = Struct.new(:incremental, :failed, :extensions, :errors, keyword_init: true)
|
|
24
|
+
|
|
25
|
+
def initialize(state, tree)
|
|
26
|
+
@state = state
|
|
27
|
+
@tree = tree
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
# `units`, `partials`, `responses`: the round's, in the same order.
|
|
31
|
+
def call(units, partials, responses)
|
|
32
|
+
responses = responses.each_with_index.map { |response, index| restore(units[index], partials[index], response) }
|
|
33
|
+
outcome = Outcome.new(incremental: [], failed: failures(units, responses), extensions: {})
|
|
34
|
+
units.each_with_index { |unit, index| deliver(unit, responses[index], outcome) }
|
|
35
|
+
outcome.errors = outcome.incremental.flat_map { |entry| entry['errors'].to_a } + outcome.failed.values.flatten.uniq
|
|
36
|
+
outcome
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
private
|
|
40
|
+
|
|
41
|
+
def deliver(unit, response, outcome)
|
|
42
|
+
owner = unit.pendings.find { |pending| !outcome.failed.key?(pending) } unless response['data'].nil?
|
|
43
|
+
return withhold(unit, response, outcome) unless owner
|
|
44
|
+
|
|
45
|
+
entry = entry(unit, owner, response)
|
|
46
|
+
return outcome.extensions.merge!(response['extensions'].to_h) unless carries?(entry)
|
|
47
|
+
|
|
48
|
+
unit.stream? ? @tree.append(unit.path, entry['items']) : @tree.merge(unit.path, entry['data'])
|
|
49
|
+
outcome.incremental << entry
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
# A unit no fragment that did not fail is left to deliver: its errors are its fragments'.
|
|
53
|
+
def withhold(unit, response, outcome)
|
|
54
|
+
unit.pendings.each { |pending| outcome.failed[pending] = (outcome.failed[pending] + response['errors'].to_a).uniq } unless response['data'].nil?
|
|
55
|
+
outcome.extensions.merge!(response['extensions'].to_h)
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
# A unit's entry, under `owner`'s id: a stream's `items`, or data below the fragment's path at
|
|
59
|
+
# its `subPath`, with the errors and the extensions of its run (BuildExtensionsResponse,
|
|
60
|
+
# ManipulateResult).
|
|
61
|
+
def entry(unit, owner, response)
|
|
62
|
+
data, errors, extensions = response.values_at('data', 'errors', 'extensions')
|
|
63
|
+
sub_path = unit.path.drop(owner.path.size) unless unit.stream?
|
|
64
|
+
{ 'id' => owner.id, 'subPath' => sub_path.presence, (unit.stream? ? 'items' : 'data') => data, 'errors' => errors,
|
|
65
|
+
'extensions' => extensions }.compact
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
def carries?(entry) = !(entry['data'] || entry['items']).empty? || entry.key?('errors')
|
|
69
|
+
|
|
70
|
+
# The pending fragments and lists that failed, with their errors: those a unit that left no data serves.
|
|
71
|
+
def failures(units, responses)
|
|
72
|
+
failed = {}.compare_by_identity
|
|
73
|
+
units.each_with_index do |unit, index|
|
|
74
|
+
next unless responses[index]['data'].nil?
|
|
75
|
+
|
|
76
|
+
unit.pendings.each { |pending| failed[pending] = (failed[pending].to_a + responses[index]['errors'].to_a).uniq }
|
|
77
|
+
end
|
|
78
|
+
failed
|
|
79
|
+
end
|
|
80
|
+
|
|
81
|
+
# A stream's response as the list's: its errors' paths, which count the items its Partial ran
|
|
82
|
+
# from 0, as the response's, and its items, the field's list. A list an error nulled (a null
|
|
83
|
+
# item where its items are non-null) fails the stream.
|
|
84
|
+
def restore(unit, partial, response)
|
|
85
|
+
return response unless unit.stream?
|
|
86
|
+
|
|
87
|
+
response['errors'].to_a.each { |error| absolute(partial, error) }
|
|
88
|
+
response.merge('data' => response['data']&.fetch(unit.path.last, nil))
|
|
89
|
+
end
|
|
90
|
+
|
|
91
|
+
def absolute(partial, error)
|
|
92
|
+
error['path'] = @state.absolute(partial, error['path']) if error['path']
|
|
93
|
+
end
|
|
94
|
+
end
|