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,127 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
# Runs a field's search when the client sent one, as PHP Lighthouse's
|
|
4
|
+
# ScoutEnhancer does: an argument carrying @search with a string value (`""`
|
|
5
|
+
# included; null or omitted falls back to the database query). BaseDirectiveResolver
|
|
6
|
+
# calls it before the argument builders.
|
|
7
|
+
#
|
|
8
|
+
# - With a relational adapter (pg_search) it narrows the relation, and every
|
|
9
|
+
# other directive of the field applies on top as usual.
|
|
10
|
+
# - Otherwise (Searchkick) the directives of the other arguments the client sent
|
|
11
|
+
# write to a Search::Query (Contracts::SearchBuilder), and the answer is a
|
|
12
|
+
# Search::Result instead of a relation. Any other argument directive the client
|
|
13
|
+
# sends is an error, as is a second @search.
|
|
14
|
+
class Lighthouse::Search::Enhancer
|
|
15
|
+
# An argument directive of the field: its AST nodes, the builder class (or :builder), the client's value.
|
|
16
|
+
Argument = Struct.new(:node, :directive, :builder, :value, :present)
|
|
17
|
+
|
|
18
|
+
# Can a search engine take the argument directive (its builder class, or :builder,
|
|
19
|
+
# and its node)? It applies one that writes to the search (Contracts::SearchBuilder,
|
|
20
|
+
# @builder), and leaves alone one that does not change the query (`changes_query?`:
|
|
21
|
+
# @limit without `builder: true`). Search::FieldCheck asks too, of the defaults.
|
|
22
|
+
def self.compatible?(builder, directive)
|
|
23
|
+
builder == :builder || builder.include?(Lighthouse::Contracts::SearchBuilder) ||
|
|
24
|
+
(builder.respond_to?(:changes_query?) && !builder.changes_query?(directive))
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
# `search`: what the caller takes from an engine (BaseDirectiveResolver::SEARCHES):
|
|
28
|
+
# nil, a relation only; :model, a Search::Result for the model's own relation it
|
|
29
|
+
# built (with the `scopes:` Search::FieldCheck refuses for an engine), searched
|
|
30
|
+
# whole without a check; :given, one for a relation the app's code gave, checked.
|
|
31
|
+
# `field`: "Type.field", for the client's errors.
|
|
32
|
+
def initialize(arguments, relation, search:, context:, field: 'the field')
|
|
33
|
+
@arguments = arguments
|
|
34
|
+
@relation = relation
|
|
35
|
+
@search = search
|
|
36
|
+
@context = context
|
|
37
|
+
@field = field
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
# The relation, searched; or a Search::Result. The relation itself if the client sent no search.
|
|
41
|
+
def enhance
|
|
42
|
+
search = the_search
|
|
43
|
+
return @relation unless search
|
|
44
|
+
|
|
45
|
+
model = @relation.klass
|
|
46
|
+
index = Lighthouse::Support::DirectiveArgs.literal(search.directive, 'within')
|
|
47
|
+
adapter = Lighthouse::Search.adapter_for(model, index) ||
|
|
48
|
+
raise(Lighthouse::DefinitionError, "@search: #{model.name} is not searchable (#{Lighthouse::Search::FieldCheck::HINT}).")
|
|
49
|
+
query = Lighthouse::Search::Query.new(model, search.value, index)
|
|
50
|
+
return adapter.scope(@relation, query) if adapter.relational?
|
|
51
|
+
|
|
52
|
+
result(adapter, query)
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
private
|
|
56
|
+
|
|
57
|
+
def the_search
|
|
58
|
+
searches = @arguments.select { |argument| search?(argument) && argument.builder.filters?(argument.value) }
|
|
59
|
+
raise ::GraphQL::ExecutionError, 'Found more than 1 argument with @search.' if searches.size > 1
|
|
60
|
+
|
|
61
|
+
searches.first
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
def search?(argument)
|
|
65
|
+
argument.builder.respond_to?(:searches?) && argument.builder.searches?
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
def result(adapter, query)
|
|
69
|
+
unless @search
|
|
70
|
+
raise ::GraphQL::ExecutionError,
|
|
71
|
+
"@search with #{adapter.class.name.demodulize} answers outside the database, and #{@field} pages a relation here: it cannot search it."
|
|
72
|
+
end
|
|
73
|
+
|
|
74
|
+
searchable!(adapter, query.model)
|
|
75
|
+
whole_index!(adapter) if @search == :given
|
|
76
|
+
reject_incompatible
|
|
77
|
+
query = @arguments.reject { |argument| search?(argument) }.reduce(query) { |narrowed, argument| narrow(narrowed, argument) }
|
|
78
|
+
Lighthouse::Search::Result.checked(adapter, query, @field) || Lighthouse::Search::Result.new(adapter, query, context: @context, field: @field)
|
|
79
|
+
end
|
|
80
|
+
|
|
81
|
+
# A model the adapter cannot search as it is (Searchkick's model_error: a default
|
|
82
|
+
# scope without search_where) is the schema's error, found here, as the field's
|
|
83
|
+
# filters run, for a field whose model the build could not tell. The one check
|
|
84
|
+
# as a search runs; Search::FieldCheck's is the build's.
|
|
85
|
+
def searchable!(adapter, model)
|
|
86
|
+
error = adapter.model_error(model) if adapter.respond_to?(:model_error)
|
|
87
|
+
raise Lighthouse::DefinitionError, "@search: #{error}." if error
|
|
88
|
+
end
|
|
89
|
+
|
|
90
|
+
# The engine searches the model's whole index: the conditions of a relation
|
|
91
|
+
# narrowed beforehand (`resolve_info.enhance_builder(Post.where(...))`) would be
|
|
92
|
+
# lost, and the search could find records the relation leaves out. A relation
|
|
93
|
+
# the app's code gives (enhance_builder) must select what the model's own does.
|
|
94
|
+
def whole_index!(adapter)
|
|
95
|
+
model = @relation.klass
|
|
96
|
+
# Every part that selects records equals the model's own, conditions and their values
|
|
97
|
+
# included: a default scope whose values read the clock is refused, as it cannot be told apart.
|
|
98
|
+
return if Lighthouse::Support::Relations.same_records?(@relation, model.all)
|
|
99
|
+
|
|
100
|
+
raise Lighthouse::DefinitionError,
|
|
101
|
+
"@search with #{adapter.class.name.demodulize} searches the whole index of #{model.name}: it cannot apply the conditions " \
|
|
102
|
+
"of the relation it was given. Search #{model.name}.all, and narrow the search with the field's arguments."
|
|
103
|
+
end
|
|
104
|
+
|
|
105
|
+
# The directives of an argument the client sent that a search engine cannot apply.
|
|
106
|
+
def reject_incompatible
|
|
107
|
+
incompatible = @arguments.find do |argument|
|
|
108
|
+
!search?(argument) && !argument.value.nil? && !self.class.compatible?(argument.builder, argument.directive)
|
|
109
|
+
end
|
|
110
|
+
return unless incompatible
|
|
111
|
+
|
|
112
|
+
raise ::GraphQL::ExecutionError,
|
|
113
|
+
"Found arg builder arguments that do not work with @search: @#{incompatible.directive.name} on #{incompatible.node.name}."
|
|
114
|
+
end
|
|
115
|
+
|
|
116
|
+
def narrow(query, argument)
|
|
117
|
+
if argument.builder == :builder
|
|
118
|
+
return query unless argument.present
|
|
119
|
+
|
|
120
|
+
Lighthouse::GraphQL::DirectiveResolvers::BuilderApplier.apply_argument(query, argument.directive, argument.value, @context)
|
|
121
|
+
elsif !argument.value.nil? && argument.builder.include?(Lighthouse::Contracts::SearchBuilder)
|
|
122
|
+
argument.builder.new(argument.node, argument.directive).handle_search_builder(query, argument.value)
|
|
123
|
+
else
|
|
124
|
+
query # nothing sent, or a directive that leaves the query alone
|
|
125
|
+
end
|
|
126
|
+
end
|
|
127
|
+
end
|
|
@@ -0,0 +1,171 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
# What a field with @search must satisfy, checked when the schema is built
|
|
4
|
+
# (FieldResolverStack), where PHP Lighthouse fails when the query runs, or not at all:
|
|
5
|
+
#
|
|
6
|
+
# - `within:` is a string.
|
|
7
|
+
# - Something applies the search: a resolver that runs the field's argument
|
|
8
|
+
# filters (`applies_search?`, declared by each resolver class), or the app's
|
|
9
|
+
# own code (a convention class, a type class's method, @field), which reads it
|
|
10
|
+
# through resolve_info.enhance_builder. Never the default reader, which would
|
|
11
|
+
# ignore it.
|
|
12
|
+
# - The field's model, when it can be told, is searchable by an adapter.
|
|
13
|
+
# - With an adapter outside the database (Searchkick), the resolver takes the
|
|
14
|
+
# records the engine answers with (`takes_search_results?`: @all, @paginate of
|
|
15
|
+
# the model's whole table, @first, @find, the app's own code); and the field has no `scopes:`,
|
|
16
|
+
# @builder, @whereAuth or @orderBy(column:), which the engine cannot apply.
|
|
17
|
+
# PHP Lighthouse drops them silently, so a search could return records the
|
|
18
|
+
# user must not see. A relational adapter (pg_search) applies them.
|
|
19
|
+
# - An @canQuery, which runs the search to check the records it finds, searches
|
|
20
|
+
# a searchable model, and without `scopes:` when the engine cannot apply them.
|
|
21
|
+
class Lighthouse::Search::FieldCheck
|
|
22
|
+
HINT = 'it needs searchkick or pg_search, or an adapter in Config.search'
|
|
23
|
+
def self.check!(...) = new(...).check!
|
|
24
|
+
|
|
25
|
+
# `type_class`: the type's class (Types::PostType), whose methods resolve its fields with no resolving directive.
|
|
26
|
+
def initialize(type, field_def, directives, root_kind, type_class: nil)
|
|
27
|
+
@type = type
|
|
28
|
+
@field_def = field_def
|
|
29
|
+
@directives = directives
|
|
30
|
+
@root_kind = root_kind
|
|
31
|
+
@type_class = type_class
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
def check!
|
|
35
|
+
searches = Lighthouse::Search.directives(@field_def)
|
|
36
|
+
return if searches.empty?
|
|
37
|
+
|
|
38
|
+
searches.each { |path, directive| within_string!(path, directive) }
|
|
39
|
+
applied!(searches.first.first)
|
|
40
|
+
model = model_class
|
|
41
|
+
searches.each do |path, directive|
|
|
42
|
+
index = Lighthouse::Support::DirectiveArgs.literal(directive, 'within')
|
|
43
|
+
field_searched!(path, model, index) if model
|
|
44
|
+
can_queries.each { |can_query| can_query_searched!(path, can_query, index) }
|
|
45
|
+
end
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
private
|
|
49
|
+
|
|
50
|
+
def within_string!(path, directive)
|
|
51
|
+
within = Lighthouse::Support::DirectiveArgs.literal(directive, 'within')
|
|
52
|
+
return if within.nil? || within.is_a?(String)
|
|
53
|
+
|
|
54
|
+
fail!(path, "expected the value of the `within` argument of @search to be a string, got: #{within.to_json}")
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
def applied!(path)
|
|
58
|
+
return if own_code? || (resolver_class && Lighthouse::Contracts::FieldResolver.applies_search?(resolver_class))
|
|
59
|
+
|
|
60
|
+
fail!(path, "nothing applies the search: #{resolver_name} runs no argument filters. Resolve the field with a directive " \
|
|
61
|
+
'that does (@all, @paginate, @find, a relationship, ...), or a class of your own that calls resolve_info.enhance_builder; ' \
|
|
62
|
+
'a directive of your own that applies it says so with `def self.applies_search? = true` (Contracts::FieldResolver)')
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
def resolver_name
|
|
66
|
+
resolving_directive ? "@#{resolving_directive.name}" : "graphql-ruby's default reader, which reads #{@field_def.name} off the parent,"
|
|
67
|
+
end
|
|
68
|
+
|
|
69
|
+
def field_searched!(path, model, index)
|
|
70
|
+
adapter = adapter!(path, model, index)
|
|
71
|
+
outside_database!(path, adapter) unless adapter.relational?
|
|
72
|
+
end
|
|
73
|
+
|
|
74
|
+
# The adapter that searches the model, which must be able to as it is (`model_error`, optional).
|
|
75
|
+
def adapter!(path, model, index)
|
|
76
|
+
adapter = Lighthouse::Search.adapter_for(model, index) || fail!(path, "#{model.name} is not searchable (#{HINT})")
|
|
77
|
+
error = adapter.model_error(model) if adapter.respond_to?(:model_error)
|
|
78
|
+
error ? fail!(path, error) : adapter
|
|
79
|
+
end
|
|
80
|
+
|
|
81
|
+
def outside_database!(path, adapter)
|
|
82
|
+
name = adapter.class.name.demodulize
|
|
83
|
+
unless own_code? || Lighthouse::Contracts::FieldResolver.takes_search_results?(resolver_class, @type, @field_def, resolving_directive, @root_kind)
|
|
84
|
+
fail!(path, "#{name} answers outside the database, so it needs @all, @paginate of the model's whole table (on a root field, " \
|
|
85
|
+
'with its own `model:`, or where the parent has no association of its name), @first, @find (whose `id` ' \
|
|
86
|
+
'argument has a filter directive, @eq), or a resolver of your own (which reads the search through ' \
|
|
87
|
+
'resolve_info.enhance_builder)')
|
|
88
|
+
end
|
|
89
|
+
dropped = dropped_constraints
|
|
90
|
+
unless dropped.empty?
|
|
91
|
+
fail!(path, "#{name} cannot apply #{dropped.join(', ')} (PHP Lighthouse drops them silently); " \
|
|
92
|
+
'remove them, or search with a relational adapter (pg_search)')
|
|
93
|
+
end
|
|
94
|
+
defaulted_constraints.each do |constraint|
|
|
95
|
+
fail!(path, "#{name} cannot apply #{constraint}, whose default value would apply to every search (PHP Lighthouse " \
|
|
96
|
+
'fails every search); remove the default, or search with a relational adapter (pg_search)')
|
|
97
|
+
end
|
|
98
|
+
end
|
|
99
|
+
|
|
100
|
+
# "@where on filter.minId" for every argument or input field that holds a value
|
|
101
|
+
# the client did not send (Support::ArgumentWalk.implicit: graphql-ruby fills
|
|
102
|
+
# defaults in, as PHP Lighthouse's arguments hold them), with a directive an
|
|
103
|
+
# engine cannot apply: it would apply to every search the client sends without it.
|
|
104
|
+
def defaulted_constraints
|
|
105
|
+
registry = Lighthouse::DirectiveRegistry.default
|
|
106
|
+
walk = Lighthouse::Support::ArgumentWalk
|
|
107
|
+
walk.implicit(@field_def.arguments).flat_map do |path, argument|
|
|
108
|
+
walk.directives(argument).filter_map do |directive|
|
|
109
|
+
builder = registry.arg_builder_class(directive.name)
|
|
110
|
+
next if builder.nil? || directive.name == 'search' || Lighthouse::Search::Enhancer.compatible?(builder, directive)
|
|
111
|
+
|
|
112
|
+
"@#{directive.name} on #{path}"
|
|
113
|
+
end
|
|
114
|
+
end
|
|
115
|
+
end
|
|
116
|
+
|
|
117
|
+
# @canQuery checks each record the search finds (CanQueryDirectiveResolver): its
|
|
118
|
+
# model must be searchable, and its `scopes:` applied by the engine.
|
|
119
|
+
def can_query_searched!(path, can_query, index)
|
|
120
|
+
model = model_of(Lighthouse::Support::ModelResolver.field_model_name(@field_def, can_query)) || return
|
|
121
|
+
adapter = adapter!(path, model, index)
|
|
122
|
+
return if adapter.relational? || Lighthouse::Support::DirectiveArgs.literal(can_query, 'scopes').blank?
|
|
123
|
+
|
|
124
|
+
fail!(path, "#{adapter.class.name.demodulize} cannot apply the `scopes:` of @canQuery, which checks the records the search finds " \
|
|
125
|
+
'(PHP Lighthouse drops them silently); remove them, or search with a relational adapter (pg_search)')
|
|
126
|
+
end
|
|
127
|
+
|
|
128
|
+
# The field's own query constraints a search engine drops.
|
|
129
|
+
def dropped_constraints = Lighthouse::Search.dropped_constraints(@field_def, resolving_directive)
|
|
130
|
+
|
|
131
|
+
def resolving_directive
|
|
132
|
+
return @resolving_directive if defined?(@resolving_directive)
|
|
133
|
+
|
|
134
|
+
@resolving_directive = Lighthouse::GraphQL::FieldResolverStack.resolving_directive(@directives)
|
|
135
|
+
end
|
|
136
|
+
|
|
137
|
+
# The resolver class of the field's resolving directive; nil without one.
|
|
138
|
+
def resolver_class
|
|
139
|
+
resolving_directive && Lighthouse::DirectiveRegistry.default.resolution_class(resolving_directive.name)
|
|
140
|
+
end
|
|
141
|
+
|
|
142
|
+
# Is the field resolved by the app's own code, which reads the search through enhance_builder?
|
|
143
|
+
def own_code?
|
|
144
|
+
resolving_directive.nil? && Lighthouse::GraphQL::FieldResolverStack.own_resolver?(@field_def, @root_kind, @type_class)
|
|
145
|
+
end
|
|
146
|
+
|
|
147
|
+
def can_queries = @directives.select { |directive| directive.name == 'canQuery' }
|
|
148
|
+
|
|
149
|
+
# The field's model, as its resolving directive tells it (BaseDirectiveResolver#model_class),
|
|
150
|
+
# else the model of the parent's association it reads (@count(relation:)); nil when it cannot
|
|
151
|
+
# be told.
|
|
152
|
+
def model_class
|
|
153
|
+
model_of(Lighthouse::Support::ModelResolver.field_model_name(@field_def, resolving_directive)) || association_model
|
|
154
|
+
end
|
|
155
|
+
|
|
156
|
+
def association_model
|
|
157
|
+
return if @root_kind
|
|
158
|
+
|
|
159
|
+
parent = model_of(Lighthouse::GraphQL::Directives::ModelDirective.model_for(@type)) || return
|
|
160
|
+
name = Lighthouse::Support::DirectiveArgs.literal(resolving_directive, 'relation') || Lighthouse::Support::Naming.attribute(@field_def.name)
|
|
161
|
+
model_of(parent.reflect_on_association(name.to_sym)&.klass)
|
|
162
|
+
rescue ArgumentError, NameError # a polymorphic association, or one whose class does not exist
|
|
163
|
+
nil
|
|
164
|
+
end
|
|
165
|
+
|
|
166
|
+
def model_of(name_or_class) = Lighthouse::Support::ModelResolver.model_class(name_or_class)
|
|
167
|
+
|
|
168
|
+
def fail!(path, message)
|
|
169
|
+
raise Lighthouse::DefinitionError, "@search on #{@type.graphql_name}.#{@field_def.name}(#{path}): #{message}."
|
|
170
|
+
end
|
|
171
|
+
end
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
# pg_search (PostgreSQL full-text search, in the database). A pg_search scope
|
|
4
|
+
# returns an ActiveRecord::Relation, so the search composes with everything
|
|
5
|
+
# else Lighthouse applies to the query: scopes, @builder, @whereAuth, argument
|
|
6
|
+
# filters, @orderBy and nested fields. pg_search orders the rows by rank first;
|
|
7
|
+
# the relation's own orders, the client's `orderBy` and the primary key follow.
|
|
8
|
+
#
|
|
9
|
+
# `within:` names the `pg_search_scope` to search with, `search` by default:
|
|
10
|
+
#
|
|
11
|
+
# pg_search_scope :search, against: %i[title body]
|
|
12
|
+
#
|
|
13
|
+
# The model's `search_results` scope, if it has one, narrows the search too
|
|
14
|
+
# (Lighthouse::Search).
|
|
15
|
+
#
|
|
16
|
+
# The gem is not a dependency of Lighthouse: the adapter is used when the
|
|
17
|
+
# model includes PgSearch::Model.
|
|
18
|
+
class Lighthouse::Search::PgSearch
|
|
19
|
+
DEFAULT_SCOPE = :search
|
|
20
|
+
|
|
21
|
+
def searchable?(model, index = nil)
|
|
22
|
+
!!(defined?(::PgSearch::Model) && model.include?(::PgSearch::Model) && pg_search_scope?(model, scope_name(index)))
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
def relational? = true
|
|
26
|
+
|
|
27
|
+
# Does `index` name one of the model's pg_search scopes? (Search::Auto)
|
|
28
|
+
def named?(model, index) = !index.nil? && searchable?(model, index)
|
|
29
|
+
|
|
30
|
+
# The relation narrowed to the records that match, ordered by rank first: a
|
|
31
|
+
# search is in order of relevance, so the orders the relation had (the model's
|
|
32
|
+
# default scope, the field's `scopes:`, the search_results scope) break the ties
|
|
33
|
+
# of rank, before the client's `orderBy` and the field's @orderBy, applied after.
|
|
34
|
+
# pg_search's own tie-break, the primary key (unless the scope names
|
|
35
|
+
# `order_within_rank:`), is dropped so that they decide; the key still ends the
|
|
36
|
+
# order, after them (BaseDirectiveResolver#apply_search).
|
|
37
|
+
def scope(relation, query)
|
|
38
|
+
relation = Lighthouse::Search.results(relation)
|
|
39
|
+
searched = relation.public_send(scope_name(query.index), query.term)
|
|
40
|
+
added = searched.order_values.drop(relation.order_values.size)
|
|
41
|
+
orders = [*without_key_tie_break(added, relation.klass), *relation.order_values]
|
|
42
|
+
orders.empty? ? searched : searched.reorder(*orders)
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
private
|
|
46
|
+
|
|
47
|
+
def scope_name(index) = (index || DEFAULT_SCOPE).to_sym
|
|
48
|
+
|
|
49
|
+
# Is the model's class method `name` a pg_search scope? pg_search keeps no list
|
|
50
|
+
# of them: `pg_search_scope` defines each from pg_search/model.rb (2.3 and 2.4
|
|
51
|
+
# alike), which the method's source tells, so another gem's method of the name
|
|
52
|
+
# (Searchkick's `search`, on a model that uses both) is not taken for one.
|
|
53
|
+
def pg_search_scope?(model, name)
|
|
54
|
+
return false unless model.respond_to?(name)
|
|
55
|
+
|
|
56
|
+
!!model.method(name).source_location&.first&.end_with?(File.join('pg_search', 'model.rb'))
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
# pg_search's orders without its primary-key tie-break, which is a node of its
|
|
60
|
+
# own after the rank's in pg_search 2.4, and the end of one SQL literal in 2.3
|
|
61
|
+
# ("pg_search_1.rank DESC, \"posts\".\"id\" ASC"); the orders themselves when it
|
|
62
|
+
# has none (`order_within_rank:`).
|
|
63
|
+
def without_key_tie_break(orders, model)
|
|
64
|
+
last = orders.last
|
|
65
|
+
return orders[0...-1] if key_node?(last, model)
|
|
66
|
+
|
|
67
|
+
tie_break = ", #{model.quoted_table_name}.#{model.connection.quote_column_name(model.primary_key)} ASC"
|
|
68
|
+
return orders unless last.is_a?(String) && last.end_with?(tie_break)
|
|
69
|
+
|
|
70
|
+
[*orders[0...-1], ::Arel.sql(last.delete_suffix(tie_break))]
|
|
71
|
+
end
|
|
72
|
+
|
|
73
|
+
def key_node?(order, model)
|
|
74
|
+
order.is_a?(::Arel::Nodes::Ascending) && order.expr == model.arel_table[model.primary_key]
|
|
75
|
+
end
|
|
76
|
+
end
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
# What a search holds, as Laravel Scout's builder does: the model, the term and
|
|
4
|
+
# what narrows it. The directives of the other arguments write to it
|
|
5
|
+
# (Contracts::SearchBuilder), and the adapter runs it (Search::Searchkick#page).
|
|
6
|
+
# Directives never know which engine is behind it.
|
|
7
|
+
class Lighthouse::Search::Query
|
|
8
|
+
# The model searched, the `@search` argument's value, the `within:` index (nil: the model's own).
|
|
9
|
+
attr_reader :model, :term, :index
|
|
10
|
+
|
|
11
|
+
# [[column, value], ...], from @eq (and @builder), in order: all of them hold,
|
|
12
|
+
# the same column twice too (a list of inputs), as the database applies them.
|
|
13
|
+
attr_reader :wheres
|
|
14
|
+
|
|
15
|
+
# Adapter-specific options, from @builder on an argument (Searchkick's `fields:`, `boost_by:`, ...).
|
|
16
|
+
attr_reader :options
|
|
17
|
+
|
|
18
|
+
# :with, :only or :without, from @trashed; nil: the client did not say.
|
|
19
|
+
attr_accessor :trashed
|
|
20
|
+
|
|
21
|
+
def initialize(model, term, index = nil)
|
|
22
|
+
@model = model
|
|
23
|
+
@term = term
|
|
24
|
+
@index = index
|
|
25
|
+
@wheres = []
|
|
26
|
+
@options = {}
|
|
27
|
+
@trashed = nil
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
# The column equals the value, as Scout's `where`.
|
|
31
|
+
def where(column, value)
|
|
32
|
+
@wheres << [column.to_s, value]
|
|
33
|
+
self
|
|
34
|
+
end
|
|
35
|
+
end
|
|
@@ -0,0 +1,181 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
# A search an engine outside the database answers: the page of records it found
|
|
4
|
+
# and its total, in relevance order. It is the list @paginate pages
|
|
5
|
+
# (Pagination::List), and @all reads whole. The engine is asked once per page,
|
|
6
|
+
# whose response holds the total too, so a page costs one request even when
|
|
7
|
+
# the client selects `paginatorInfo`. Each field's search is its own Result, and
|
|
8
|
+
# asks the engine itself: one is never shared between fields or requests, nor
|
|
9
|
+
# kept in the query's context. Within one resolution of a field, the search
|
|
10
|
+
# @canQuery checked serves the page (`Result.serving`), from the hits it read and
|
|
11
|
+
# checked: a record the index gains in between is never answered unchecked.
|
|
12
|
+
#
|
|
13
|
+
# The total is exact, every match counted (Search::Searchkick asks for it): pages,
|
|
14
|
+
# truncation and @canQuery's check of every hit rely on it.
|
|
15
|
+
#
|
|
16
|
+
# An engine pages no further than its result window (Elasticsearch's and
|
|
17
|
+
# OpenSearch's index.max_result_window): a window that runs past it is cut at
|
|
18
|
+
# its end, and one that starts past it is a client error. When the adapter does
|
|
19
|
+
# not know the window (`result_window` nil), the engine refuses on its own.
|
|
20
|
+
#
|
|
21
|
+
# The engine's requests fail as the field's filters do (Support::Failures),
|
|
22
|
+
# whenever they run: an engine's error is reported to Config.on_error.
|
|
23
|
+
class Lighthouse::Search::Result
|
|
24
|
+
include Lighthouse::GraphQL::Pagination::List
|
|
25
|
+
|
|
26
|
+
# The result window of an adapter that does not tell it (`result_window(query)`):
|
|
27
|
+
# Elasticsearch's and OpenSearch's default index.max_result_window.
|
|
28
|
+
MAX_WINDOW = 10_000
|
|
29
|
+
|
|
30
|
+
# The most records one read asks for, whatever the window (a billion with
|
|
31
|
+
# Searchkick's `deep_paging`): @all, and the records @canQuery checks.
|
|
32
|
+
READ_LIMIT = 10_000
|
|
33
|
+
|
|
34
|
+
# The fiber-local key of the search @canQuery checked, while the field it checks resolves.
|
|
35
|
+
CHECKED = :lighthouse_checked_search
|
|
36
|
+
|
|
37
|
+
# The block (one resolution of a field: CanQueryDirectiveResolver), in which a
|
|
38
|
+
# search of the same field (`checked`) is the `result` @canQuery read: the
|
|
39
|
+
# Enhancer answers it, whose hits serve the page. Fiber-local, so neither
|
|
40
|
+
# another field, resolving meanwhile in another fiber, nor another query sees it.
|
|
41
|
+
def self.serving(result)
|
|
42
|
+
previous = Thread.current[CHECKED]
|
|
43
|
+
Thread.current[CHECKED] = result
|
|
44
|
+
yield
|
|
45
|
+
ensure
|
|
46
|
+
Thread.current[CHECKED] = previous
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
# The search @canQuery checked in this resolution, when it is this one: the same
|
|
50
|
+
# field, adapter, model, term, index and conditions; else nil.
|
|
51
|
+
def self.checked(adapter, query, field)
|
|
52
|
+
checked = Thread.current[CHECKED]
|
|
53
|
+
checked if checked.is_a?(self) && checked.same_search?(adapter, query, field)
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
# `context`: the query's, to which the engine's errors are reported. `field`:
|
|
57
|
+
# "Type.field", whose search this is.
|
|
58
|
+
def initialize(adapter, query, context: nil, field: nil)
|
|
59
|
+
@adapter = adapter
|
|
60
|
+
@query = query
|
|
61
|
+
@context = context
|
|
62
|
+
@field = field
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
attr_reader :query
|
|
66
|
+
|
|
67
|
+
# The engine's name, for errors: Searchkick.
|
|
68
|
+
def adapter_name = @adapter.class.name.demodulize
|
|
69
|
+
|
|
70
|
+
# The records at `offset`, `limit` positions of them, within the result window:
|
|
71
|
+
# a Pagination::Window, whose records keep their engine positions. The adapter
|
|
72
|
+
# answers one entry per hit, nil for a hit that did not load (the model's
|
|
73
|
+
# scopes, a record gone from the database), so pages and cursors follow the
|
|
74
|
+
# engine's positions, and none shows a record twice.
|
|
75
|
+
def window(offset, limit)
|
|
76
|
+
return [] if limit.zero?
|
|
77
|
+
|
|
78
|
+
limit = within_window(offset, limit)
|
|
79
|
+
entries = @checked ? (@checked[offset, limit] || []) : request(offset, limit)
|
|
80
|
+
loaded = entries.each_index.reject { |index| entries[index].nil? }
|
|
81
|
+
Lighthouse::GraphQL::Pagination::Window.new(entries.compact, loaded.map { |index| offset + index }, entries.size)
|
|
82
|
+
end
|
|
83
|
+
|
|
84
|
+
# How many records match: the total the last page came with, else a request for no records.
|
|
85
|
+
def count
|
|
86
|
+
request(0, 0) unless @count
|
|
87
|
+
@count
|
|
88
|
+
end
|
|
89
|
+
|
|
90
|
+
# What @all returns: `first` records from `offset` (every one when nil), of
|
|
91
|
+
# which one read takes READ_LIMIT at most, within the result window.
|
|
92
|
+
# `truncated?` tells when that answered fewer than asked while more match.
|
|
93
|
+
def records(first: nil, offset: nil)
|
|
94
|
+
offset ||= 0
|
|
95
|
+
found = window(offset, [first || READ_LIMIT, READ_LIMIT].min)
|
|
96
|
+
read = Lighthouse::GraphQL::Pagination.covered(found)
|
|
97
|
+
@truncated = (first.nil? || read < first) && count > offset + read
|
|
98
|
+
found
|
|
99
|
+
end
|
|
100
|
+
|
|
101
|
+
# Did the last `records` answer fewer matches than asked for, while more match
|
|
102
|
+
# (READ_LIMIT, the result window)?
|
|
103
|
+
def truncated? = @truncated == true
|
|
104
|
+
|
|
105
|
+
# What @find returns: the one record the search finds, or nil. More than one
|
|
106
|
+
# match, as the engine counts them, is an error, as in PHP Lighthouse.
|
|
107
|
+
def only_record
|
|
108
|
+
found = window(0, 1)
|
|
109
|
+
raise ::GraphQL::ExecutionError, 'The query returned more than one result.' if count > 1
|
|
110
|
+
|
|
111
|
+
found.first
|
|
112
|
+
end
|
|
113
|
+
|
|
114
|
+
# What @first returns: the best match that loads, or nil. A best hit that does
|
|
115
|
+
# not load (the model's scopes, a record gone from the database) yields to the
|
|
116
|
+
# next one, read within the result window and READ_LIMIT.
|
|
117
|
+
def first_record
|
|
118
|
+
found = window(0, 1)
|
|
119
|
+
return found.first if found.any? || count <= 1
|
|
120
|
+
|
|
121
|
+
window(0, [reachable, READ_LIMIT].min).first
|
|
122
|
+
end
|
|
123
|
+
|
|
124
|
+
# How many matches a page can reach: the total, within the result window.
|
|
125
|
+
def reachable = result_window ? [count, result_window].min : count
|
|
126
|
+
|
|
127
|
+
# Every record the search finds that a page can reach, for @canQuery to check:
|
|
128
|
+
# one request, of READ_LIMIT records at most, whose hits then serve every page
|
|
129
|
+
# of this Result, and no other request. A search that finds more within the
|
|
130
|
+
# result window is a client error, as a page could reach a record not checked.
|
|
131
|
+
def every_record
|
|
132
|
+
found = window(0, READ_LIMIT)
|
|
133
|
+
unless reachable <= READ_LIMIT
|
|
134
|
+
raise ::GraphQL::ExecutionError,
|
|
135
|
+
"@canQuery checks every record the search finds, at most #{READ_LIMIT}, and this one finds #{reachable}. Narrow the search."
|
|
136
|
+
end
|
|
137
|
+
|
|
138
|
+
@checked ||= @last_entries
|
|
139
|
+
found
|
|
140
|
+
end
|
|
141
|
+
|
|
142
|
+
# Is it the search of `field` that `adapter` runs for `query`? Its options aside,
|
|
143
|
+
# which may hold an @builder's procs, made again for each search.
|
|
144
|
+
def same_search?(adapter, query, field)
|
|
145
|
+
@adapter.equal?(adapter) && @field == field && query.model.equal?(@query.model) &&
|
|
146
|
+
[query.term, query.index, query.wheres, query.trashed] == [@query.term, @query.index, @query.wheres, @query.trashed]
|
|
147
|
+
end
|
|
148
|
+
|
|
149
|
+
# How many of the matches the engine pages through; nil when the adapter cannot tell.
|
|
150
|
+
def result_window
|
|
151
|
+
return @result_window if defined?(@result_window)
|
|
152
|
+
|
|
153
|
+
@result_window = Lighthouse::Support::Failures.strictly(@context) do
|
|
154
|
+
@adapter.respond_to?(:result_window) ? @adapter.result_window(@query) : MAX_WINDOW
|
|
155
|
+
end
|
|
156
|
+
end
|
|
157
|
+
|
|
158
|
+
private
|
|
159
|
+
|
|
160
|
+
# The engine's entries at `offset`, `limit` of them; its total, which must be a count.
|
|
161
|
+
def request(offset, limit)
|
|
162
|
+
Lighthouse::Support::Failures.strictly(@context) do
|
|
163
|
+
entries, total = @adapter.page(@query, offset: offset, limit: limit)
|
|
164
|
+
unless total.is_a?(Integer)
|
|
165
|
+
raise Lighthouse::DefinitionError, "@search: #{adapter_name} answered #{total.inspect} for the total, not the count of every match"
|
|
166
|
+
end
|
|
167
|
+
|
|
168
|
+
@count = total
|
|
169
|
+
@last_entries = entries
|
|
170
|
+
end
|
|
171
|
+
end
|
|
172
|
+
|
|
173
|
+
def within_window(offset, limit)
|
|
174
|
+
return limit if result_window.nil?
|
|
175
|
+
return [limit, result_window - offset].min if offset < result_window
|
|
176
|
+
|
|
177
|
+
raise ::GraphQL::ExecutionError,
|
|
178
|
+
"The search reads no further than its first #{result_window} matches (the index.max_result_window of " \
|
|
179
|
+
"#{@query.model.name}), so offset #{offset} is past them. Narrow the search instead."
|
|
180
|
+
end
|
|
181
|
+
end
|