lighthouse-graphql 0.8.2 → 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 +152 -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 +102 -0
- 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/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/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_types.rb +1 -2
- 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/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 +34 -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 +38 -1
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
# What a block works out for a key, kept: the one cache of what Lighthouse
|
|
4
|
+
# reads of a model or an index (Search::Searchkick, Search::Searchkick::Index).
|
|
5
|
+
#
|
|
6
|
+
# - A value belongs to the class it was worked out for (`owner`): a class code
|
|
7
|
+
# reloading makes, under the same name, is worked out again.
|
|
8
|
+
# - With `ttl:`, a value is worked out again once it is that many seconds old;
|
|
9
|
+
# without, it is kept.
|
|
10
|
+
# - With `failed_ttl:`, a block that raises is kept as a failure that long
|
|
11
|
+
# (`get` answers nil, `failure` the error), and `on_failure` hears of it
|
|
12
|
+
# once; without, the error is raised.
|
|
13
|
+
# - One block at a time works out a key: other threads asking for it meanwhile
|
|
14
|
+
# wait and take its value, so the source is asked once however many ask.
|
|
15
|
+
class Lighthouse::Support::Memo
|
|
16
|
+
Entry = Struct.new(:owner, :value, :error, :expires)
|
|
17
|
+
|
|
18
|
+
# What the block works out for `object` (a field's definition), kept on it as
|
|
19
|
+
# `name`: worked out once, the first time it is asked (when the schema is
|
|
20
|
+
# built), and gone with the object, as a schema built again on reload is.
|
|
21
|
+
def self.on(object, name)
|
|
22
|
+
ivar = :"@lighthouse_#{name}"
|
|
23
|
+
return object.instance_variable_get(ivar) if object.instance_variable_defined?(ivar)
|
|
24
|
+
|
|
25
|
+
object.instance_variable_set(ivar, yield)
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
# `clock`: seconds, monotonic.
|
|
29
|
+
def initialize(ttl: nil, failed_ttl: nil, on_failure: nil, clock: -> { Process.clock_gettime(Process::CLOCK_MONOTONIC) })
|
|
30
|
+
@ttl = ttl
|
|
31
|
+
@failed_ttl = failed_ttl
|
|
32
|
+
@on_failure = on_failure
|
|
33
|
+
@clock = clock
|
|
34
|
+
@entries = {}
|
|
35
|
+
@locks = {}
|
|
36
|
+
@lock = Mutex.new
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
# The value for `key` and `owner`, worked out by the block when none is kept.
|
|
40
|
+
def get(key, owner = nil, &block)
|
|
41
|
+
entry = kept(key, owner)
|
|
42
|
+
return entry.value if entry
|
|
43
|
+
|
|
44
|
+
lock_for(key).synchronize do
|
|
45
|
+
entry = kept(key, owner) || (@entries[key] = work_out(owner, &block))
|
|
46
|
+
entry.value
|
|
47
|
+
end
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
# The error the last block for `key` raised, while its failure is kept; else nil.
|
|
51
|
+
def failure(key, owner = nil) = kept(key, owner)&.error
|
|
52
|
+
|
|
53
|
+
private
|
|
54
|
+
|
|
55
|
+
def kept(key, owner)
|
|
56
|
+
entry = @entries[key]
|
|
57
|
+
return nil unless entry && entry.owner.equal?(owner)
|
|
58
|
+
|
|
59
|
+
entry if entry.expires.nil? || @clock.call < entry.expires
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
def work_out(owner)
|
|
63
|
+
Entry.new(owner, yield, nil, @ttl && (@clock.call + @ttl))
|
|
64
|
+
rescue StandardError => e
|
|
65
|
+
raise unless @failed_ttl
|
|
66
|
+
|
|
67
|
+
@on_failure&.call(e)
|
|
68
|
+
Entry.new(owner, nil, e, @clock.call + @failed_ttl)
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
def lock_for(key) = @lock.synchronize { @locks[key] ||= Mutex.new }
|
|
72
|
+
end
|
|
@@ -13,6 +13,30 @@ module Lighthouse::Support::ModelResolver
|
|
|
13
13
|
|
|
14
14
|
DEFAULT_NAMESPACES = ['App::Graphql'].freeze
|
|
15
15
|
|
|
16
|
+
# The suffix of a type Lighthouse generates for @paginate: PostPaginator, PostSimplePaginator, PostConnection.
|
|
17
|
+
PAGINATION_SUFFIX = /(SimplePaginator|Paginator|Connection)\z/
|
|
18
|
+
|
|
19
|
+
# The name of the model a directive on a field acts on: the directive's own
|
|
20
|
+
# `model:`, else the `model:` of another directive on the field
|
|
21
|
+
# (`@count(model: "Post") @canQuery`), else the field's return type without a
|
|
22
|
+
# generated Paginator/Connection suffix. `directive_node` nil: the field's.
|
|
23
|
+
def field_model_name(field_def, directive_node = nil)
|
|
24
|
+
Lighthouse::Support::DirectiveArgs.literal(directive_node, 'model') ||
|
|
25
|
+
(field_def.ast_node&.directives || []).filter_map { |d| Lighthouse::Support::DirectiveArgs.literal(d, 'model') }.first ||
|
|
26
|
+
field_def.type.unwrap.graphql_name.sub(PAGINATION_SUFFIX, '')
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
# Is `value` an Active Record model class (not ActiveRecord::Base itself)?
|
|
30
|
+
def model?(value)
|
|
31
|
+
!!(defined?(::ActiveRecord::Base) && value.is_a?(Class) && value < ::ActiveRecord::Base)
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
# The Active Record model a class, or a class name, names; nil for anything else.
|
|
35
|
+
def model_class(name_or_class)
|
|
36
|
+
model = name_or_class.is_a?(Class) ? name_or_class : constantize(name_or_class)
|
|
37
|
+
model if model?(model)
|
|
38
|
+
end
|
|
39
|
+
|
|
16
40
|
def namespaces
|
|
17
41
|
if defined?(Lighthouse::Config) &&
|
|
18
42
|
Lighthouse::Config.respond_to?(:resolver_namespaces) &&
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
# What tells two relations of a model apart by the records they select, for
|
|
4
|
+
# the checks a search outside the database needs: is a relation the model's
|
|
5
|
+
# own (Search::Enhancer), has the model a default scope (Search::Searchkick)?
|
|
6
|
+
module Lighthouse::Support::Relations
|
|
7
|
+
module_function
|
|
8
|
+
|
|
9
|
+
# The parts of a relation that decide which records it selects. Order, select
|
|
10
|
+
# and preloading do not.
|
|
11
|
+
SELECTING = %i[where_clause having_clause from_clause joins_values left_outer_joins_values group_values limit_value offset_value].freeze
|
|
12
|
+
|
|
13
|
+
# Does `relation` select what `other` does? Each part equal, conditions and their
|
|
14
|
+
# values included; a null relation (`none`) selects nothing.
|
|
15
|
+
def same_records?(relation, other)
|
|
16
|
+
null?(relation) == null?(other) && SELECTING.all? { |part| relation.public_send(part) == other.public_send(part) }
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
# Has the model a default scope? Told from its declaration, never from what it
|
|
20
|
+
# selects now: a scope that reads the request (`where(tenant_id:
|
|
21
|
+
# Current.tenant_id) if Current.tenant_id`) narrows nothing outside one, and
|
|
22
|
+
# everything within. Any default scope counts, an order alone too.
|
|
23
|
+
def default_scope?(model) = model.respond_to?(:default_scopes) && model.default_scopes.any?
|
|
24
|
+
|
|
25
|
+
# `none`: a null relation (Rails 7.1+ answers null_relation?).
|
|
26
|
+
def null?(relation)
|
|
27
|
+
return relation.null_relation? if relation.respond_to?(:null_relation?)
|
|
28
|
+
|
|
29
|
+
!!(defined?(::ActiveRecord::NullRelation) && relation.is_a?(::ActiveRecord::NullRelation))
|
|
30
|
+
end
|
|
31
|
+
end
|
|
@@ -12,8 +12,11 @@
|
|
|
12
12
|
# `Config.soft_deletes` replaces the detection with an adapter of the app's
|
|
13
13
|
# own: any object answering `soft_deletes?(model)`, `only_trashed(relation)`,
|
|
14
14
|
# `with_trashed(relation)`, `without_trashed(relation)`, `restore(record)` and
|
|
15
|
-
# `force_delete(record)` (the last two answering whether they succeeded)
|
|
16
|
-
# `
|
|
15
|
+
# `force_delete(record)` (the last two answering whether they succeeded), and
|
|
16
|
+
# optionally `column(model)` and `kept_value(model)`: the column that marks the
|
|
17
|
+
# trashed records and its value for the others, which a search engine filters on
|
|
18
|
+
# (Search::Searchkick). `Column.new(:archived_at)` is a ready-made one for another
|
|
19
|
+
# column name.
|
|
17
20
|
module Lighthouse::Support::SoftDeletes
|
|
18
21
|
module_function
|
|
19
22
|
|
|
@@ -49,16 +52,36 @@ module Lighthouse::Support::SoftDeletes
|
|
|
49
52
|
adapter.without_trashed(relation)
|
|
50
53
|
end
|
|
51
54
|
|
|
55
|
+
TRASHED = { 'ONLY' => :only, 'WITH' => :with, 'WITHOUT' => :without }.freeze
|
|
56
|
+
|
|
57
|
+
# :only, :with or :without, for a `Trashed` enum value (ONLY, WITH or WITHOUT).
|
|
58
|
+
def trashed(value)
|
|
59
|
+
TRASHED.fetch(value.to_s.upcase) do
|
|
60
|
+
raise ::GraphQL::ExecutionError, "Unknown trashed value #{value.inspect}, expected ONLY, WITH or WITHOUT."
|
|
61
|
+
end
|
|
62
|
+
end
|
|
63
|
+
|
|
52
64
|
# The relation for a `Trashed` enum value: ONLY, WITH or WITHOUT.
|
|
53
|
-
def filter(relation,
|
|
54
|
-
case trashed
|
|
55
|
-
when
|
|
56
|
-
when
|
|
57
|
-
|
|
58
|
-
else raise ::GraphQL::ExecutionError, "Unknown trashed value #{trashed.inspect}, expected ONLY, WITH or WITHOUT."
|
|
65
|
+
def filter(relation, value)
|
|
66
|
+
case trashed(value)
|
|
67
|
+
when :only then only_trashed(relation)
|
|
68
|
+
when :with then with_trashed(relation)
|
|
69
|
+
else without_trashed(relation)
|
|
59
70
|
end
|
|
60
71
|
end
|
|
61
72
|
|
|
73
|
+
# The column that marks the model's trashed records, as the adapter tells it
|
|
74
|
+
# (`column(model)`), for a search engine to filter on; nil when it does not.
|
|
75
|
+
def column(model)
|
|
76
|
+
adapter.respond_to?(:column) ? adapter.column(model)&.to_s : nil
|
|
77
|
+
end
|
|
78
|
+
|
|
79
|
+
# What that column holds for the records that are not trashed: nil, or
|
|
80
|
+
# paranoia's sentinel value (`kept_value(model)`).
|
|
81
|
+
def kept_value(model)
|
|
82
|
+
adapter.respond_to?(:kept_value) ? adapter.kept_value(model) : nil
|
|
83
|
+
end
|
|
84
|
+
|
|
62
85
|
def restore(record)
|
|
63
86
|
require_support!(record.class)
|
|
64
87
|
adapter.restore(record)
|
|
@@ -88,6 +111,9 @@ module Lighthouse::Support::SoftDeletes
|
|
|
88
111
|
def restore(record) = record.restore
|
|
89
112
|
|
|
90
113
|
def force_delete(record) = record.really_destroy!
|
|
114
|
+
|
|
115
|
+
def column(model) = model.paranoia_column
|
|
116
|
+
def kept_value(model) = model.paranoia_sentinel_value
|
|
91
117
|
end
|
|
92
118
|
|
|
93
119
|
# discard: `kept` / `discarded` scopes, `#undiscard`; `#destroy` really deletes.
|
|
@@ -105,6 +131,9 @@ module Lighthouse::Support::SoftDeletes
|
|
|
105
131
|
def restore(record) = record.undiscard
|
|
106
132
|
|
|
107
133
|
def force_delete(record) = record.destroy
|
|
134
|
+
|
|
135
|
+
def column(model) = model.discard_column
|
|
136
|
+
def kept_value(_model) = nil
|
|
108
137
|
end
|
|
109
138
|
|
|
110
139
|
# A nullable timestamp column (`deleted_at` by default) and no gem. It marks
|
|
@@ -133,6 +162,9 @@ module Lighthouse::Support::SoftDeletes
|
|
|
133
162
|
end
|
|
134
163
|
|
|
135
164
|
def force_delete(record) = record.destroy
|
|
165
|
+
|
|
166
|
+
def column(_model) = @column
|
|
167
|
+
def kept_value(_model) = nil
|
|
136
168
|
end
|
|
137
169
|
|
|
138
170
|
# The default adapter: asks each strategy, in order, whether the model uses it.
|
|
@@ -150,6 +182,8 @@ module Lighthouse::Support::SoftDeletes
|
|
|
150
182
|
def without_trashed(relation) = strategy!(relation.klass).without_trashed(relation)
|
|
151
183
|
def restore(record) = strategy!(record.class).restore(record)
|
|
152
184
|
def force_delete(record) = strategy!(record.class).force_delete(record)
|
|
185
|
+
def column(model) = strategy(model)&.column(model)
|
|
186
|
+
def kept_value(model) = strategy(model)&.kept_value(model)
|
|
153
187
|
|
|
154
188
|
private
|
|
155
189
|
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'json'
|
|
4
|
+
|
|
5
|
+
# Requests to the app's GraphQL endpoint, for its tests: PHP Lighthouse's MakesGraphQLRequests.
|
|
6
|
+
# Include it where Rails' integration session runs (an RSpec request spec, an
|
|
7
|
+
# ActionDispatch::IntegrationTest):
|
|
8
|
+
#
|
|
9
|
+
# RSpec.configure { |config| config.include Lighthouse::GraphQL::Testing, type: :request }
|
|
10
|
+
#
|
|
11
|
+
# it 'lists the posts' do
|
|
12
|
+
# body = graphql('{ posts(first: 1) { data { title } } }')
|
|
13
|
+
# expect(body.dig('data', 'posts', 'data')).to eq([{ 'title' => 'Hello' }])
|
|
14
|
+
# end
|
|
15
|
+
#
|
|
16
|
+
# Each request answers the response's parsed JSON body (`response` holds the response itself, as
|
|
17
|
+
# after any request). The endpoint is `graphql_endpoint`, `/graphql` unless a test overrides it.
|
|
18
|
+
module Lighthouse::GraphQL::Testing
|
|
19
|
+
# A request that was to stream did not.
|
|
20
|
+
class NotStreamed < StandardError; end
|
|
21
|
+
|
|
22
|
+
# POSTs a query with its variables (and `extra_params`, `operationName` or `extensions`), as JSON.
|
|
23
|
+
def graphql(query, variables = {}, extra_params: {}, headers: {})
|
|
24
|
+
post_graphql(graphql_operation(query, variables, extra_params), headers: headers)
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
# POSTs `data` as JSON: an operation's Hash, or a batch's Array of them.
|
|
28
|
+
def post_graphql(data, headers: {})
|
|
29
|
+
post_graphql_json(data, headers)
|
|
30
|
+
response.parsed_body
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
# POSTs a multipart request (graphql-multipart-request-spec, MultipartRequest): `operations`,
|
|
34
|
+
# the `map` from each file's key to the variables it fills, and the files by key
|
|
35
|
+
# (Rack::Test::UploadedFile).
|
|
36
|
+
def multipart_graphql(operations, map, files, headers: {})
|
|
37
|
+
params = { 'operations' => operations.to_json, 'map' => map.to_json }.merge(files.transform_keys(&:to_s))
|
|
38
|
+
post graphql_endpoint, params: params, headers: headers
|
|
39
|
+
response.parsed_body
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
# The response to graphql-ruby's introspection query.
|
|
43
|
+
def introspect = graphql(::GraphQL::Introspection.query)
|
|
44
|
+
|
|
45
|
+
# The introspection of the type, or of the directive, named `name`; nil when the schema has none.
|
|
46
|
+
def introspect_type(name) = introspect_by_name('types', name)
|
|
47
|
+
def introspect_directive(name) = introspect_by_name('directives', name)
|
|
48
|
+
|
|
49
|
+
# POSTs a query whose response streams (@defer, @stream), and answers each of its payloads,
|
|
50
|
+
# in order: the first, then one per round. `Accept` is `multipart/mixed` unless the headers say
|
|
51
|
+
# otherwise (Apollo's older format: `multipart/mixed;deferSpec=20220824`). A response that did
|
|
52
|
+
# not stream (the query deferred nothing, or the schema does not use Lighthouse::GraphQL::Defer)
|
|
53
|
+
# raises NotStreamed.
|
|
54
|
+
def stream_graphql(query, variables = {}, extra_params: {}, headers: {})
|
|
55
|
+
post_graphql_json(graphql_operation(query, variables, extra_params), { 'Accept' => 'multipart/mixed' }.merge(headers))
|
|
56
|
+
unless response.content_type.to_s.split(';').first == Lighthouse::GraphQL::Defer::MULTIPART
|
|
57
|
+
raise NotStreamed, "Expected a streamed response, got a regular one (#{response.content_type})."
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
Lighthouse::GraphQL::Incremental::Multipart.parse(response.body)
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
# The endpoint's path.
|
|
64
|
+
def graphql_endpoint = '/graphql'
|
|
65
|
+
|
|
66
|
+
private
|
|
67
|
+
|
|
68
|
+
def graphql_operation(query, variables, extra_params)
|
|
69
|
+
operation = { query: query }
|
|
70
|
+
operation[:variables] = variables unless variables.empty?
|
|
71
|
+
operation.merge(extra_params)
|
|
72
|
+
end
|
|
73
|
+
|
|
74
|
+
def post_graphql_json(data, headers)
|
|
75
|
+
post graphql_endpoint, params: data.to_json, headers: { 'Content-Type' => 'application/json' }.merge(headers)
|
|
76
|
+
end
|
|
77
|
+
|
|
78
|
+
def introspect_by_name(path, name)
|
|
79
|
+
introspect.dig('data', '__schema', path)&.find { |entry| entry['name'] == name }
|
|
80
|
+
end
|
|
81
|
+
end
|
|
@@ -35,8 +35,41 @@ module Lighthouse
|
|
|
35
35
|
include Operations
|
|
36
36
|
|
|
37
37
|
# The action: dispatches StartRequest, runs the operation (or a batch),
|
|
38
|
-
# sets Cache-Control, renders JSON,
|
|
38
|
+
# sets Cache-Control, renders JSON (or streams an operation that defers, when the
|
|
39
|
+
# schema uses Defer), dispatches EndRequest.
|
|
39
40
|
def execute: () -> void
|
|
40
41
|
end
|
|
42
|
+
|
|
43
|
+
# Requests to the app's endpoint from its tests (PHP Lighthouse's MakesGraphQLRequests),
|
|
44
|
+
# in Rails' integration session. Each answers the response's parsed body.
|
|
45
|
+
module Testing : _IntegrationSession
|
|
46
|
+
class NotStreamed < StandardError
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
def graphql: (String query, ?Hash[String | Symbol, untyped] variables, ?extra_params: Hash[String | Symbol, untyped],
|
|
50
|
+
?headers: Hash[String, String]) -> untyped
|
|
51
|
+
def post_graphql: (Hash[String | Symbol, untyped] | Array[Hash[String | Symbol, untyped]] data, ?headers: Hash[String, String]) -> untyped
|
|
52
|
+
def multipart_graphql: (Hash[String | Symbol, untyped] operations, Hash[String, Array[String]] map, Hash[String | Symbol, untyped] files,
|
|
53
|
+
?headers: Hash[String, String]) -> untyped
|
|
54
|
+
def introspect: () -> untyped
|
|
55
|
+
def introspect_type: (String name) -> Hash[String, untyped]?
|
|
56
|
+
def introspect_directive: (String name) -> Hash[String, untyped]?
|
|
57
|
+
# The payloads of a streamed response; NotStreamed when it came whole.
|
|
58
|
+
def stream_graphql: (String query, ?Hash[String | Symbol, untyped] variables, ?extra_params: Hash[String | Symbol, untyped],
|
|
59
|
+
?headers: Hash[String, String]) -> Array[Hash[String, json]]
|
|
60
|
+
def graphql_endpoint: () -> String
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
# Rails' integration session (ActionDispatch::Integration::Runner), as Testing uses it.
|
|
64
|
+
interface _IntegrationSession
|
|
65
|
+
def post: (String path, ?params: untyped, ?headers: Hash[String, String]) -> untyped
|
|
66
|
+
def response: () -> _TestResponse
|
|
67
|
+
end
|
|
68
|
+
|
|
69
|
+
interface _TestResponse
|
|
70
|
+
def parsed_body: () -> untyped
|
|
71
|
+
def body: () -> String
|
|
72
|
+
def content_type: () -> String?
|
|
73
|
+
end
|
|
41
74
|
end
|
|
42
75
|
end
|
|
@@ -17,9 +17,20 @@ module Lighthouse
|
|
|
17
17
|
end
|
|
18
18
|
|
|
19
19
|
# Its instances, built with (field_def, type_def, directive_node, opts),
|
|
20
|
-
# install the field's resolver in `define_resolver(target_type)`.
|
|
20
|
+
# install the field's resolver in `define_resolver(target_type)`. Its class
|
|
21
|
+
# says whether it applies a @search on its field (by default, it does not).
|
|
21
22
|
module FieldResolver
|
|
22
23
|
include Directive
|
|
24
|
+
|
|
25
|
+
# The class methods a resolver gets with the contract, and overrides to opt in.
|
|
26
|
+
module SearchDefaults
|
|
27
|
+
def applies_search?: () -> bool
|
|
28
|
+
def takes_search_results?: (_NamedType type, _FieldDefinition field_def, _DirectiveNode? directive, Symbol? root_kind) -> bool
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
# What Search::FieldCheck asks of a resolver class, with or without the contract.
|
|
32
|
+
def self.applies_search?: (Class klass) -> bool
|
|
33
|
+
def self.takes_search_results?: (Class klass, _NamedType type, _FieldDefinition field_def, _DirectiveNode? directive, Symbol? root_kind) -> bool
|
|
23
34
|
end
|
|
24
35
|
|
|
25
36
|
# Its instances, built with (arg_node, directive_node), answer
|
|
@@ -47,6 +58,16 @@ module Lighthouse
|
|
|
47
58
|
module FieldBuilder
|
|
48
59
|
include Directive
|
|
49
60
|
end
|
|
61
|
+
|
|
62
|
+
# An argument directive that also narrows a full-text search: its instances,
|
|
63
|
+
# built with (arg_node, directive_node), answer `handle_search_builder(query, value)`
|
|
64
|
+
# with the Search::Query. @eq, @trashed and @builder on an argument do.
|
|
65
|
+
module SearchBuilder
|
|
66
|
+
include Directive
|
|
67
|
+
|
|
68
|
+
# Narrow the search by the client's value, writing to the query (`where`, `trashed`, `options`).
|
|
69
|
+
def handle_search_builder: (Search::Query query, untyped value) -> Search::Query
|
|
70
|
+
end
|
|
50
71
|
end
|
|
51
72
|
|
|
52
73
|
class DirectiveRegistry
|
|
@@ -146,12 +167,18 @@ module Lighthouse
|
|
|
146
167
|
|
|
147
168
|
def initialize: (_FieldDefinition field_def, _NamedType type_def, _DirectiveNode? directive_node, ?Hash[Symbol, untyped] opts) -> void
|
|
148
169
|
|
|
170
|
+
# Asked when the schema is built, of the field's resolving directive's class (Search::FieldCheck):
|
|
171
|
+
# does it run the field's argument filters, a relational search among them?
|
|
172
|
+
def self.applies_search?: () -> bool
|
|
173
|
+
# Does it take the records a search engine outside the database answers with, on this field?
|
|
174
|
+
def self.takes_search_results?: (_NamedType type, _FieldDefinition field_def, _DirectiveNode? directive, Symbol? root_kind) -> bool
|
|
175
|
+
|
|
149
176
|
# Install the field's resolver on `target_type_class` (a graphql-ruby type class).
|
|
150
177
|
def define_resolver: (Class target_type_class) -> void
|
|
151
178
|
|
|
152
179
|
# `ctx`: the query's context, which @builder on an argument receives.
|
|
153
|
-
|
|
154
|
-
def
|
|
180
|
+
# `search`: nil, a relation only; :model, a Search::Result for the model's own relation; :given, one for a relation the app's code gave.
|
|
181
|
+
def apply_where_filters: (relation relation, _ArgumentSet? args, ?context? ctx, ?search: (:model | :given)?) -> (relation | Search::Result)
|
|
155
182
|
def apply_field_builders: (relation relation, ?arguments args_h, ?context? ctx) -> relation
|
|
156
183
|
def constantize_model: (String? name) -> Class?
|
|
157
184
|
def apply_scopes: (relation relation) -> relation
|
|
@@ -170,9 +197,13 @@ module Lighthouse
|
|
|
170
197
|
class BuilderApplier
|
|
171
198
|
DEFINITION: String
|
|
172
199
|
|
|
173
|
-
|
|
174
|
-
def self.
|
|
200
|
+
# A builder that raises, or cannot be found, goes on to the caller, which fails the field.
|
|
201
|
+
def self.apply: (relation relation, _NamedNode? field_def_ast_node, arguments args, context? ctx) -> relation
|
|
202
|
+
def self.apply_directive: (relation relation, _DirectiveNode directive_node, arguments args, context? ctx) -> relation
|
|
175
203
|
def self.extract_directive_arg: (_DirectiveNode directive_node, String name) -> literal
|
|
204
|
+
# The field's own query directives `apply` applies (@builder, @whereAuth, field builders), in SDL order.
|
|
205
|
+
def self.query_directives: (_NamedNode? field_def_ast_node) -> Array[_DirectiveNode]
|
|
206
|
+
def self.any?: (_NamedNode? field_def_ast_node) -> bool
|
|
176
207
|
end
|
|
177
208
|
end
|
|
178
209
|
|
|
@@ -193,6 +224,16 @@ module Lighthouse
|
|
|
193
224
|
# (whatever its type allows, so untyped).
|
|
194
225
|
def handle_builder: (relation relation, untyped value) -> relation
|
|
195
226
|
|
|
227
|
+
# Does the directive, as the SDL writes it, change the field's query? A
|
|
228
|
+
# Searchkick search takes one that does not (@limit without `builder: true`).
|
|
229
|
+
def self.changes_query?: (_DirectiveNode? directive_node) -> bool
|
|
230
|
+
|
|
231
|
+
# Does the client's value, once sent, narrow the query? (@search: only a string.)
|
|
232
|
+
def self.filters?: (untyped value) -> bool
|
|
233
|
+
|
|
234
|
+
# Does the directive run the field's search (@search)?
|
|
235
|
+
def self.searches?: () -> bool
|
|
236
|
+
|
|
196
237
|
private
|
|
197
238
|
|
|
198
239
|
# The directive's `key:`, else the argument's name in snake_case.
|
data/sig/lighthouse/events.rbs
CHANGED
|
@@ -41,6 +41,13 @@ module Lighthouse
|
|
|
41
41
|
# ActiveSupport::Notifications subscriber.
|
|
42
42
|
def self.listening?: (Class event_class) -> bool
|
|
43
43
|
|
|
44
|
+
# The query of the operation `query` runs: a GraphQL::Query::Partial's is the
|
|
45
|
+
# query it runs part of.
|
|
46
|
+
def self.operation_query: (_Query query) -> _Query
|
|
47
|
+
|
|
48
|
+
# GraphQL::Query::Partial, where graphql-ruby has it (2.5.6 on).
|
|
49
|
+
PARTIAL: Class?
|
|
50
|
+
|
|
44
51
|
# The ActiveSupport::Notifications name an event class is instrumented as.
|
|
45
52
|
def self.notification_name: (Class event_class) -> String
|
|
46
53
|
|
data/sig/lighthouse/graphql.rbs
CHANGED
|
@@ -96,6 +96,16 @@ module Lighthouse
|
|
|
96
96
|
# protocol error as a ready-to-render { "errors" => [...] } Hash.
|
|
97
97
|
def self?.execute: (schema schema, query: String?, ?extensions: extensions?, ?variables: Hash[String, untyped]?,
|
|
98
98
|
?operation_name: String?, ?context: Hash[Symbol, untyped], **untyped options) -> _Result
|
|
99
|
+
# `execute` for a response that may be streamed (Defer): yields the started
|
|
100
|
+
# Incremental::Run and answers what the block does, or a protocol error as a
|
|
101
|
+
# ready-to-render Hash. The operation ends once the run is done, or the block.
|
|
102
|
+
# Without a block, it answers the Run, which the caller ends (Run#finish).
|
|
103
|
+
def self?.incremental: [T] (schema schema, query: String?, ?extensions: extensions?, ?format: Incremental::format,
|
|
104
|
+
?variables: Hash[String, untyped]?, ?operation_name: String?, ?context: Hash[Symbol, untyped],
|
|
105
|
+
**untyped options) { (Incremental::Run run) -> T } -> (T | Hash[String, json])
|
|
106
|
+
| (schema schema, query: String?, ?extensions: extensions?, ?format: Incremental::format,
|
|
107
|
+
?variables: Hash[String, untyped]?, ?operation_name: String?, ?context: Hash[Symbol, untyped],
|
|
108
|
+
**untyped options) -> (Incremental::Run | Hash[String, json])
|
|
99
109
|
# A batch: `schema.multiplex` with the query each operation (a Hash of
|
|
100
110
|
# query, extensions, variables, operation_name, context) names. One result
|
|
101
111
|
# per operation, in order; a protocol error is a ready-to-render Hash.
|
|
@@ -179,6 +189,113 @@ module Lighthouse
|
|
|
179
189
|
end
|
|
180
190
|
end
|
|
181
191
|
|
|
192
|
+
# @defer (experimental; the specification it follows is a draft): a graphql-ruby
|
|
193
|
+
# plugin, `use Lighthouse::GraphQL::Defer` in the schema class. Lighthouse::GraphQL::Controller
|
|
194
|
+
# then streams the response of an operation that defers, unless the client's Accept header
|
|
195
|
+
# leaves `multipart/mixed` out.
|
|
196
|
+
module Defer
|
|
197
|
+
MULTIPART: String
|
|
198
|
+
LEGACY: String
|
|
199
|
+
LEGACY_TYPE: String
|
|
200
|
+
FORMAT: String
|
|
201
|
+
GRAPHQL: String
|
|
202
|
+
|
|
203
|
+
# graphql-ruby's plugin hook.
|
|
204
|
+
def self?.use: (schema schema, **untyped options) -> void
|
|
205
|
+
# Does the schema use the plugin? `use` extends the schema class with Used.
|
|
206
|
+
def self?.enabled?: (schema schema) -> bool
|
|
207
|
+
module Used
|
|
208
|
+
end
|
|
209
|
+
# The Content-Type of a streamed response to a client that sends `accept`, or nil when it
|
|
210
|
+
# does not take one; it names Apollo's older format (`deferSpec=20220824`) when the client did.
|
|
211
|
+
def self?.stream_type: (String? accept) -> String?
|
|
212
|
+
# Is the Content-Type (stream_type's) Apollo's older format?
|
|
213
|
+
def self?.legacy?: (String? content_type) -> bool
|
|
214
|
+
# Each media range of the header.
|
|
215
|
+
def self?.ranges: (String? accept) -> Array[MediaRange]
|
|
216
|
+
|
|
217
|
+
# A media range of an Accept header.
|
|
218
|
+
class MediaRange
|
|
219
|
+
attr_reader type: String
|
|
220
|
+
attr_reader params: Array[String]
|
|
221
|
+
attr_reader q: Float
|
|
222
|
+
attr_reader index: Integer
|
|
223
|
+
def rank: () -> [Float, Integer]
|
|
224
|
+
def covers?: (String name) -> bool
|
|
225
|
+
def specificity: () -> Integer
|
|
226
|
+
def legacy?: () -> bool
|
|
227
|
+
end
|
|
228
|
+
end
|
|
229
|
+
|
|
230
|
+
module Incremental
|
|
231
|
+
# The payloads' format: the specification draft's, or Apollo's older one (Legacy).
|
|
232
|
+
type format = :draft | :legacy
|
|
233
|
+
|
|
234
|
+
# One operation, run in as many passes as @defer asks for.
|
|
235
|
+
class Run
|
|
236
|
+
# `options` are `schema.execute`'s: variables:, operation_name:, context:.
|
|
237
|
+
# A nil query string (a request without one) is answered as graphql-ruby answers it.
|
|
238
|
+
def initialize: (schema schema, String? query_string, ?format: format, **untyped options) -> void
|
|
239
|
+
# Runs the first pass; returns self. The block gets what ends the operation (see finish).
|
|
240
|
+
def start: () ?{ (_Result result) -> void } -> Run
|
|
241
|
+
# Calls start's block, once: with the first pass's result when nothing was deferred, else the response so far.
|
|
242
|
+
def finish: () -> void
|
|
243
|
+
# The first pass's result.
|
|
244
|
+
def result: () -> _Result
|
|
245
|
+
# Are fragments pending?
|
|
246
|
+
def deferred?: () -> bool
|
|
247
|
+
# The initial payload, then one per round of deferred fragments, until `hasNext` is false.
|
|
248
|
+
def each_payload: () { (Hash[String, json]) -> void } -> void
|
|
249
|
+
# The first payload, as the client is sent it (each_payload yields it first).
|
|
250
|
+
def first_payload: () -> Hash[String, json]
|
|
251
|
+
# The first payload could not be sent: the operation ends as one that failed.
|
|
252
|
+
def refuse!: () -> void
|
|
253
|
+
# The whole response: the data and the errors of every round the client was sent ({} if none).
|
|
254
|
+
def response: () -> Hash[String, json]
|
|
255
|
+
end
|
|
256
|
+
|
|
257
|
+
# Converts the draft's payloads to Apollo's older format (`deferSpec=20220824`).
|
|
258
|
+
class Legacy
|
|
259
|
+
def initialize: (Hash[String, untyped] pendings) -> void
|
|
260
|
+
def convert: (Hash[String, json] payload) -> Hash[String, json]
|
|
261
|
+
end
|
|
262
|
+
|
|
263
|
+
# The parts of a `multipart/mixed` response (graphql-over-http's IncrementalDelivery).
|
|
264
|
+
class Multipart
|
|
265
|
+
CONTENT_TYPE: String
|
|
266
|
+
PART_HEADERS: String
|
|
267
|
+
CLOSING: String
|
|
268
|
+
|
|
269
|
+
def self.headers: (?String content_type) -> Hash[String, String]
|
|
270
|
+
# The JSON of each part of a multipart/mixed body.
|
|
271
|
+
def self.parse: (String? body) -> Array[Hash[String, json]]
|
|
272
|
+
# The part of a payload, encoded as render json: does (the payload holds the app's values:
|
|
273
|
+
# a Time, an object's as_json; their errors are raised here).
|
|
274
|
+
def part: (Hash[String, untyped] payload) -> String
|
|
275
|
+
# A part (or the closing boundary) was written.
|
|
276
|
+
def written: (String part) -> void
|
|
277
|
+
def started?: () -> bool
|
|
278
|
+
# The closing boundary, once a part was written.
|
|
279
|
+
def closing: () -> String?
|
|
280
|
+
# The response as EndRequest carries it: its body the parts written.
|
|
281
|
+
def ended: (untyped response) -> Response
|
|
282
|
+
def initialize: (?keep: bool) -> void
|
|
283
|
+
|
|
284
|
+
class Response
|
|
285
|
+
def initialize: (untyped response, Array[String] parts) -> void
|
|
286
|
+
def body: () -> String
|
|
287
|
+
end
|
|
288
|
+
end
|
|
289
|
+
|
|
290
|
+
# The Rack body of a streamed response: writes each part as its round runs, then ends the
|
|
291
|
+
# operation and the request (`finish`, once).
|
|
292
|
+
class Body
|
|
293
|
+
def initialize: (Run run, Multipart multipart, String first, on_error: ^(StandardError) -> void, on_end: ^() -> void) -> void
|
|
294
|
+
def each: () { (String part) -> void } -> void
|
|
295
|
+
def finish: () -> void
|
|
296
|
+
end
|
|
297
|
+
end
|
|
298
|
+
|
|
182
299
|
# The opt-in cache of the manipulated schema document, in tmp/cache.
|
|
183
300
|
module SchemaCache
|
|
184
301
|
PREFIX: String
|