lighthouse-graphql 0.8.2 → 0.9.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +168 -1
- data/docs/authorization.md +5 -1
- data/docs/configuration.md +71 -3
- data/docs/custom-directives.md +22 -0
- data/docs/defer.md +352 -0
- data/docs/directives.md +106 -0
- data/docs/events.md +30 -1
- data/docs/filtering-and-ordering.md +567 -0
- data/docs/getting-started.md +1 -1
- data/docs/performance.md +9 -0
- data/docs/resolvers.md +14 -2
- data/docs/testing.md +99 -0
- data/lib/lighthouse/complexity.rb +4 -2
- data/lib/lighthouse/config.rb +28 -11
- data/lib/lighthouse/contracts/field_resolver.rb +32 -0
- data/lib/lighthouse/contracts/search_builder.rb +16 -0
- data/lib/lighthouse/controller.rb +94 -18
- data/lib/lighthouse/defer.rb +114 -0
- data/lib/lighthouse/directive_resolvers/aggregate_resolver.rb +1 -0
- data/lib/lighthouse/directive_resolvers/all_resolver.rb +32 -3
- data/lib/lighthouse/directive_resolvers/argument_binding.rb +1 -3
- data/lib/lighthouse/directive_resolvers/association_resolvers.rb +1 -0
- data/lib/lighthouse/directive_resolvers/base.rb +116 -49
- data/lib/lighthouse/directive_resolvers/builder_applier.rb +24 -18
- data/lib/lighthouse/directive_resolvers/can_resolvers.rb +28 -2
- data/lib/lighthouse/directive_resolvers/count_resolver.rb +1 -0
- data/lib/lighthouse/directive_resolvers/field_resolver.rb +4 -0
- data/lib/lighthouse/directive_resolvers/find_resolver.rb +31 -16
- data/lib/lighthouse/directive_resolvers/first_resolver.rb +6 -3
- data/lib/lighthouse/directive_resolvers/global_id_types.rb +1 -2
- data/lib/lighthouse/directive_resolvers/model_mutation_resolver.rb +2 -0
- data/lib/lighthouse/directive_resolvers/paginate_resolver.rb +29 -4
- data/lib/lighthouse/directive_resolvers/throttle_resolver.rb +17 -6
- data/lib/lighthouse/directives/arguments/base.rb +14 -0
- data/lib/lighthouse/directives/arguments/filters.rb +6 -0
- data/lib/lighthouse/directives/arguments/more_filters.rb +3 -0
- data/lib/lighthouse/directives/arguments/search.rb +34 -0
- data/lib/lighthouse/directives/arguments/soft_deletes.rb +11 -0
- data/lib/lighthouse/directives/model_directive.rb +1 -1
- data/lib/lighthouse/directives/registry.rb +1 -1
- data/lib/lighthouse/events/execution_trace.rb +6 -1
- data/lib/lighthouse/events/lifecycle.rb +2 -1
- data/lib/lighthouse/events.rb +9 -0
- data/lib/lighthouse/field_resolver_stack.rb +22 -21
- data/lib/lighthouse/graphql/version.rb +1 -1
- data/lib/lighthouse/incremental/body.rb +76 -0
- data/lib/lighthouse/incremental/defer_directive.rb +34 -0
- data/lib/lighthouse/incremental/delivery.rb +94 -0
- data/lib/lighthouse/incremental/document.rb +242 -0
- data/lib/lighthouse/incremental/gone.rb +6 -0
- data/lib/lighthouse/incremental/legacy.rb +75 -0
- data/lib/lighthouse/incremental/multipart.rb +76 -0
- data/lib/lighthouse/incremental/plan.rb +249 -0
- data/lib/lighthouse/incremental/rules.rb +116 -0
- data/lib/lighthouse/incremental/run.rb +302 -0
- data/lib/lighthouse/incremental/serializer.rb +37 -0
- data/lib/lighthouse/incremental/state.rb +204 -0
- data/lib/lighthouse/incremental/stream_directive.rb +41 -0
- data/lib/lighthouse/incremental/stream_trace.rb +93 -0
- data/lib/lighthouse/incremental/tree.rb +68 -0
- data/lib/lighthouse/namespaces.rb +2 -0
- data/lib/lighthouse/nested_mutations/belongs_to_operations.rb +2 -2
- data/lib/lighthouse/operations.rb +6 -2
- data/lib/lighthouse/pagination.rb +61 -15
- data/lib/lighthouse/persisted_queries.rb +53 -8
- data/lib/lighthouse/rb_lighthouse.rb +35 -0
- data/lib/lighthouse/resolve_info.rb +7 -1
- data/lib/lighthouse/schema_generator.rb +2 -0
- data/lib/lighthouse/search/auto.rb +26 -0
- data/lib/lighthouse/search/enhancer.rb +151 -0
- data/lib/lighthouse/search/field_check.rb +173 -0
- data/lib/lighthouse/search/pg_search.rb +146 -0
- data/lib/lighthouse/search/query.rb +54 -0
- data/lib/lighthouse/search/result.rb +181 -0
- data/lib/lighthouse/search/searchkick.rb +335 -0
- data/lib/lighthouse/search/searchkick_index.rb +54 -0
- data/lib/lighthouse/search/searchkick_options.rb +74 -0
- data/lib/lighthouse/search/together_check.rb +137 -0
- data/lib/lighthouse/search.rb +101 -0
- data/lib/lighthouse/support/argument_walk.rb +81 -0
- data/lib/lighthouse/support/count.rb +16 -0
- data/lib/lighthouse/support/failures.rb +28 -0
- data/lib/lighthouse/support/memo.rb +72 -0
- data/lib/lighthouse/support/model_resolver.rb +24 -0
- data/lib/lighthouse/support/relations.rb +31 -0
- data/lib/lighthouse/support/soft_deletes.rb +42 -8
- data/lib/lighthouse/testing.rb +81 -0
- data/sig/lighthouse/controller.rbs +34 -1
- data/sig/lighthouse/directives.rbs +46 -5
- data/sig/lighthouse/events.rbs +7 -0
- data/sig/lighthouse/graphql.rbs +117 -0
- data/sig/lighthouse/search.rbs +153 -0
- data/sig/lighthouse.rbs +77 -1
- metadata +39 -1
|
@@ -0,0 +1,151 @@
|
|
|
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
|
+
# - For models searched together (`models`: a union's or an interface's), there is no
|
|
15
|
+
# relation: the search is one over all of them, a Search::Result, whatever the adapter.
|
|
16
|
+
class Lighthouse::Search::Enhancer
|
|
17
|
+
# An argument directive of the field: its AST nodes, the builder class (or :builder), the client's value.
|
|
18
|
+
Argument = Struct.new(:node, :directive, :builder, :value, :present)
|
|
19
|
+
|
|
20
|
+
# Can a search engine take the argument directive (its builder class, or :builder,
|
|
21
|
+
# and its node)? It applies one that writes to the search (Contracts::SearchBuilder,
|
|
22
|
+
# @builder), and leaves alone one that does not change the query (`changes_query?`:
|
|
23
|
+
# @limit without `builder: true`). Search::FieldCheck asks too, of the defaults.
|
|
24
|
+
def self.compatible?(builder, directive)
|
|
25
|
+
builder == :builder || builder.include?(Lighthouse::Contracts::SearchBuilder) ||
|
|
26
|
+
(builder.respond_to?(:changes_query?) && !builder.changes_query?(directive))
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
# `search`: what the caller takes from an engine (BaseDirectiveResolver::SEARCHES):
|
|
30
|
+
# nil, a relation only; :model, a Search::Result for the model's own relation it
|
|
31
|
+
# built (with the `scopes:` Search::FieldCheck refuses for an engine), searched
|
|
32
|
+
# whole without a check; :given, one for a relation the app's code gave, checked.
|
|
33
|
+
# `field`: "Type.field", for the client's errors. `search` is the models searched together, in
|
|
34
|
+
# place of a relation (nil), for a field of a union's or an interface's (Search.together).
|
|
35
|
+
def initialize(arguments, relation, search:, context:, field: 'the field')
|
|
36
|
+
@arguments = arguments
|
|
37
|
+
@relation = relation
|
|
38
|
+
@search = search
|
|
39
|
+
@context = context
|
|
40
|
+
@field = field
|
|
41
|
+
@models = search if search.is_a?(Array)
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
# The relation, searched; or a Search::Result. The relation itself if the client sent no search.
|
|
45
|
+
def enhance
|
|
46
|
+
search = the_search
|
|
47
|
+
return together(search) if @models
|
|
48
|
+
return @relation unless search
|
|
49
|
+
|
|
50
|
+
model = @relation.klass
|
|
51
|
+
index = Lighthouse::Support::DirectiveArgs.literal(search.directive, 'within')
|
|
52
|
+
adapter = Lighthouse::Search.adapter_for(model, index) ||
|
|
53
|
+
raise(Lighthouse::DefinitionError, "@search: #{model.name} is not searchable (#{Lighthouse::Search::FieldCheck::HINT}).")
|
|
54
|
+
query = Lighthouse::Search::Query.new(model, search.value, index)
|
|
55
|
+
return adapter.scope(@relation, query) if adapter.relational?
|
|
56
|
+
|
|
57
|
+
result(adapter, query)
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
private
|
|
61
|
+
|
|
62
|
+
def the_search
|
|
63
|
+
searches = @arguments.select { |argument| search?(argument) && argument.builder.filters?(argument.value) }
|
|
64
|
+
raise ::GraphQL::ExecutionError, 'Found more than 1 argument with @search.' if searches.size > 1
|
|
65
|
+
|
|
66
|
+
searches.first
|
|
67
|
+
end
|
|
68
|
+
|
|
69
|
+
def search?(argument)
|
|
70
|
+
argument.builder.respond_to?(:searches?) && argument.builder.searches?
|
|
71
|
+
end
|
|
72
|
+
|
|
73
|
+
def result(adapter, query)
|
|
74
|
+
unless @search
|
|
75
|
+
raise ::GraphQL::ExecutionError,
|
|
76
|
+
"@search with #{adapter.class.name.demodulize} answers outside the database, and #{@field} pages a relation here: it cannot search it."
|
|
77
|
+
end
|
|
78
|
+
|
|
79
|
+
searchable!(adapter, query.model)
|
|
80
|
+
whole_index!(adapter) if @search == :given
|
|
81
|
+
narrowed(adapter, query)
|
|
82
|
+
end
|
|
83
|
+
|
|
84
|
+
# The search of the models, together: the field's argument is non-null (Search::FieldCheck), so
|
|
85
|
+
# the client always sends one.
|
|
86
|
+
def together(search)
|
|
87
|
+
raise ::GraphQL::ExecutionError, "#{@field} searches #{@models.map(&:name).join(', ')} together: it needs a search." unless search
|
|
88
|
+
|
|
89
|
+
adapter = Lighthouse::Search.adapter_together(@models) ||
|
|
90
|
+
raise(Lighthouse::DefinitionError, "@search: no engine searches #{@models.map(&:name).join(', ')} together.")
|
|
91
|
+
@models.each do |model|
|
|
92
|
+
error = adapter.together_error(model)
|
|
93
|
+
raise Lighthouse::DefinitionError, "@search: #{error}." if error
|
|
94
|
+
end
|
|
95
|
+
narrowed(adapter, Lighthouse::Search::Query.new(nil, search.value, models: @models))
|
|
96
|
+
end
|
|
97
|
+
|
|
98
|
+
# The search, narrowed by the other arguments the client sent, which the engine must take.
|
|
99
|
+
def narrowed(adapter, query)
|
|
100
|
+
reject_incompatible
|
|
101
|
+
query = @arguments.reject { |argument| search?(argument) }.reduce(query) { |narrowed, argument| narrow(narrowed, argument) }
|
|
102
|
+
Lighthouse::Search::Result.checked(adapter, query, @field) || Lighthouse::Search::Result.new(adapter, query, context: @context, field: @field)
|
|
103
|
+
end
|
|
104
|
+
|
|
105
|
+
# A model the adapter cannot search as it is (Searchkick's model_error: a default
|
|
106
|
+
# scope without search_where) is the schema's error, found here, as the field's
|
|
107
|
+
# filters run, for a field whose model the build could not tell. The one check
|
|
108
|
+
# as a search runs; Search::FieldCheck's is the build's.
|
|
109
|
+
def searchable!(adapter, model)
|
|
110
|
+
error = adapter.model_error(model) if adapter.respond_to?(:model_error)
|
|
111
|
+
raise Lighthouse::DefinitionError, "@search: #{error}." if error
|
|
112
|
+
end
|
|
113
|
+
|
|
114
|
+
# The engine searches the model's whole index: the conditions of a relation
|
|
115
|
+
# narrowed beforehand (`resolve_info.enhance_builder(Post.where(...))`) would be
|
|
116
|
+
# lost, and the search could find records the relation leaves out. A relation
|
|
117
|
+
# the app's code gives (enhance_builder) must select what the model's own does.
|
|
118
|
+
def whole_index!(adapter)
|
|
119
|
+
model = @relation.klass
|
|
120
|
+
# Every part that selects records equals the model's own, conditions and their values
|
|
121
|
+
# included: a default scope whose values read the clock is refused, as it cannot be told apart.
|
|
122
|
+
return if Lighthouse::Support::Relations.same_records?(@relation, model.all)
|
|
123
|
+
|
|
124
|
+
raise Lighthouse::DefinitionError,
|
|
125
|
+
"@search with #{adapter.class.name.demodulize} searches the whole index of #{model.name}: it cannot apply the conditions " \
|
|
126
|
+
"of the relation it was given. Search #{model.name}.all, and narrow the search with the field's arguments."
|
|
127
|
+
end
|
|
128
|
+
|
|
129
|
+
# The directives of an argument the client sent that a search engine cannot apply.
|
|
130
|
+
def reject_incompatible
|
|
131
|
+
incompatible = @arguments.find do |argument|
|
|
132
|
+
!search?(argument) && !argument.value.nil? && !self.class.compatible?(argument.builder, argument.directive)
|
|
133
|
+
end
|
|
134
|
+
return unless incompatible
|
|
135
|
+
|
|
136
|
+
raise ::GraphQL::ExecutionError,
|
|
137
|
+
"Found arg builder arguments that do not work with @search: @#{incompatible.directive.name} on #{incompatible.node.name}."
|
|
138
|
+
end
|
|
139
|
+
|
|
140
|
+
def narrow(query, argument)
|
|
141
|
+
if argument.builder == :builder
|
|
142
|
+
return query unless argument.present
|
|
143
|
+
|
|
144
|
+
Lighthouse::GraphQL::DirectiveResolvers::BuilderApplier.apply_argument(query, argument.directive, argument.value, @context)
|
|
145
|
+
elsif !argument.value.nil? && argument.builder.include?(Lighthouse::Contracts::SearchBuilder)
|
|
146
|
+
argument.builder.new(argument.node, argument.directive).handle_search_builder(query, argument.value)
|
|
147
|
+
else
|
|
148
|
+
query # nothing sent, or a directive that leaves the query alone
|
|
149
|
+
end
|
|
150
|
+
end
|
|
151
|
+
end
|
|
@@ -0,0 +1,173 @@
|
|
|
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
|
+
#
|
|
22
|
+
# A field of a union or an interface whose models are searched together is TogetherCheck's.
|
|
23
|
+
class Lighthouse::Search::FieldCheck
|
|
24
|
+
HINT = 'it needs searchkick or pg_search, or an adapter in Config.search'
|
|
25
|
+
def self.check!(...) = new(...).check!
|
|
26
|
+
|
|
27
|
+
# `type_class`: the type's class (Types::PostType), whose methods resolve its fields with no resolving directive.
|
|
28
|
+
def initialize(type, field_def, directives, root_kind, type_class: nil)
|
|
29
|
+
@type = type
|
|
30
|
+
@field_def = field_def
|
|
31
|
+
@directives = directives
|
|
32
|
+
@root_kind = root_kind
|
|
33
|
+
@type_class = type_class
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
def check!
|
|
37
|
+
searches = Lighthouse::Search.directives(@field_def)
|
|
38
|
+
return if searches.empty? || Lighthouse::Search.together(@field_def)
|
|
39
|
+
|
|
40
|
+
searches.each { |path, directive| within_string!(path, directive) }
|
|
41
|
+
applied!(searches.first.first)
|
|
42
|
+
model = model_class
|
|
43
|
+
searches.each do |path, directive|
|
|
44
|
+
index = Lighthouse::Support::DirectiveArgs.literal(directive, 'within')
|
|
45
|
+
field_searched!(path, model, index) if model
|
|
46
|
+
can_queries.each { |can_query| can_query_searched!(path, can_query, index) }
|
|
47
|
+
end
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
private
|
|
51
|
+
|
|
52
|
+
def within_string!(path, directive)
|
|
53
|
+
within = Lighthouse::Support::DirectiveArgs.literal(directive, 'within')
|
|
54
|
+
return if within.nil? || within.is_a?(String)
|
|
55
|
+
|
|
56
|
+
fail!(path, "expected the value of the `within` argument of @search to be a string, got: #{within.to_json}")
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
def applied!(path)
|
|
60
|
+
return if own_code? || (resolver_class && Lighthouse::Contracts::FieldResolver.applies_search?(resolver_class))
|
|
61
|
+
|
|
62
|
+
fail!(path, "nothing applies the search: #{resolver_name} runs no argument filters. Resolve the field with a directive " \
|
|
63
|
+
'that does (@all, @paginate, @find, a relationship, ...), or a class of your own that calls resolve_info.enhance_builder; ' \
|
|
64
|
+
'a directive of your own that applies it says so with `def self.applies_search? = true` (Contracts::FieldResolver)')
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
def resolver_name
|
|
68
|
+
resolving_directive ? "@#{resolving_directive.name}" : "graphql-ruby's default reader, which reads #{@field_def.name} off the parent,"
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
def field_searched!(path, model, index)
|
|
72
|
+
adapter = adapter!(path, model, index)
|
|
73
|
+
outside_database!(path, adapter) unless adapter.relational?
|
|
74
|
+
end
|
|
75
|
+
|
|
76
|
+
# The adapter that searches the model, which must be able to as it is (`model_error`, optional).
|
|
77
|
+
def adapter!(path, model, index)
|
|
78
|
+
adapter = Lighthouse::Search.adapter_for(model, index) || fail!(path, "#{model.name} is not searchable (#{HINT})")
|
|
79
|
+
error = adapter.model_error(model) if adapter.respond_to?(:model_error)
|
|
80
|
+
error ? fail!(path, error) : adapter
|
|
81
|
+
end
|
|
82
|
+
|
|
83
|
+
def outside_database!(path, adapter)
|
|
84
|
+
name = adapter.class.name.demodulize
|
|
85
|
+
unless own_code? || Lighthouse::Contracts::FieldResolver.takes_search_results?(resolver_class, @type, @field_def, resolving_directive, @root_kind)
|
|
86
|
+
fail!(path, "#{name} answers outside the database, so it needs @all, @paginate of the model's whole table (on a root field, " \
|
|
87
|
+
'with its own `model:`, or where the parent has no association of its name), @first, @find (whose `id` ' \
|
|
88
|
+
'argument has a filter directive, @eq), or a resolver of your own (which reads the search through ' \
|
|
89
|
+
'resolve_info.enhance_builder)')
|
|
90
|
+
end
|
|
91
|
+
dropped = dropped_constraints
|
|
92
|
+
unless dropped.empty?
|
|
93
|
+
fail!(path, "#{name} cannot apply #{dropped.join(', ')} (PHP Lighthouse drops them silently); " \
|
|
94
|
+
'remove them, or search with a relational adapter (pg_search)')
|
|
95
|
+
end
|
|
96
|
+
defaulted_constraints.each do |constraint|
|
|
97
|
+
fail!(path, "#{name} cannot apply #{constraint}, whose default value would apply to every search (PHP Lighthouse " \
|
|
98
|
+
'fails every search); remove the default, or search with a relational adapter (pg_search)')
|
|
99
|
+
end
|
|
100
|
+
end
|
|
101
|
+
|
|
102
|
+
# "@where on filter.minId" for every argument or input field that holds a value
|
|
103
|
+
# the client did not send (Support::ArgumentWalk.implicit: graphql-ruby fills
|
|
104
|
+
# defaults in, as PHP Lighthouse's arguments hold them), with a directive an
|
|
105
|
+
# engine cannot apply: it would apply to every search the client sends without it.
|
|
106
|
+
def defaulted_constraints
|
|
107
|
+
registry = Lighthouse::DirectiveRegistry.default
|
|
108
|
+
walk = Lighthouse::Support::ArgumentWalk
|
|
109
|
+
walk.implicit(@field_def.arguments).flat_map do |path, argument|
|
|
110
|
+
walk.directives(argument).filter_map do |directive|
|
|
111
|
+
builder = registry.arg_builder_class(directive.name)
|
|
112
|
+
next if builder.nil? || directive.name == 'search' || Lighthouse::Search::Enhancer.compatible?(builder, directive)
|
|
113
|
+
|
|
114
|
+
"@#{directive.name} on #{path}"
|
|
115
|
+
end
|
|
116
|
+
end
|
|
117
|
+
end
|
|
118
|
+
|
|
119
|
+
# @canQuery checks each record the search finds (CanQueryDirectiveResolver): its
|
|
120
|
+
# model must be searchable, and its `scopes:` applied by the engine.
|
|
121
|
+
def can_query_searched!(path, can_query, index)
|
|
122
|
+
model = model_of(Lighthouse::Support::ModelResolver.field_model_name(@field_def, can_query)) || return
|
|
123
|
+
adapter = adapter!(path, model, index)
|
|
124
|
+
return if adapter.relational? || Lighthouse::Support::DirectiveArgs.literal(can_query, 'scopes').blank?
|
|
125
|
+
|
|
126
|
+
fail!(path, "#{adapter.class.name.demodulize} cannot apply the `scopes:` of @canQuery, which checks the records the search finds " \
|
|
127
|
+
'(PHP Lighthouse drops them silently); remove them, or search with a relational adapter (pg_search)')
|
|
128
|
+
end
|
|
129
|
+
|
|
130
|
+
# The field's own query constraints a search engine drops.
|
|
131
|
+
def dropped_constraints = Lighthouse::Search.dropped_constraints(@field_def, resolving_directive)
|
|
132
|
+
|
|
133
|
+
def resolving_directive
|
|
134
|
+
return @resolving_directive if defined?(@resolving_directive)
|
|
135
|
+
|
|
136
|
+
@resolving_directive = Lighthouse::GraphQL::FieldResolverStack.resolving_directive(@directives)
|
|
137
|
+
end
|
|
138
|
+
|
|
139
|
+
# The resolver class of the field's resolving directive; nil without one.
|
|
140
|
+
def resolver_class
|
|
141
|
+
resolving_directive && Lighthouse::DirectiveRegistry.default.resolution_class(resolving_directive.name)
|
|
142
|
+
end
|
|
143
|
+
|
|
144
|
+
# Is the field resolved by the app's own code, which reads the search through enhance_builder?
|
|
145
|
+
def own_code?
|
|
146
|
+
resolving_directive.nil? && Lighthouse::GraphQL::FieldResolverStack.own_resolver?(@field_def, @root_kind, @type_class)
|
|
147
|
+
end
|
|
148
|
+
|
|
149
|
+
def can_queries = @directives.select { |directive| directive.name == 'canQuery' }
|
|
150
|
+
|
|
151
|
+
# The field's model, as its resolving directive tells it (BaseDirectiveResolver#model_class),
|
|
152
|
+
# else the model of the parent's association it reads (@count(relation:)); nil when it cannot
|
|
153
|
+
# be told.
|
|
154
|
+
def model_class
|
|
155
|
+
model_of(Lighthouse::Support::ModelResolver.field_model_name(@field_def, resolving_directive)) || association_model
|
|
156
|
+
end
|
|
157
|
+
|
|
158
|
+
def association_model
|
|
159
|
+
return if @root_kind
|
|
160
|
+
|
|
161
|
+
parent = model_of(Lighthouse::GraphQL::Directives::ModelDirective.model_for(@type)) || return
|
|
162
|
+
name = Lighthouse::Support::DirectiveArgs.literal(resolving_directive, 'relation') || Lighthouse::Support::Naming.attribute(@field_def.name)
|
|
163
|
+
model_of(parent.reflect_on_association(name.to_sym)&.klass)
|
|
164
|
+
rescue ArgumentError, NameError # a polymorphic association, or one whose class does not exist
|
|
165
|
+
nil
|
|
166
|
+
end
|
|
167
|
+
|
|
168
|
+
def model_of(name_or_class) = Lighthouse::Support::ModelResolver.model_class(name_or_class)
|
|
169
|
+
|
|
170
|
+
def fail!(path, message)
|
|
171
|
+
raise Lighthouse::DefinitionError, "@search on #{@type.graphql_name}.#{@field_def.name}(#{path}): #{message}."
|
|
172
|
+
end
|
|
173
|
+
end
|
|
@@ -0,0 +1,146 @@
|
|
|
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
|
+
# Models searched together (Search::Together) are searched through pg_search's
|
|
17
|
+
# multisearch, the documents each `multisearchable` model keeps in one table
|
|
18
|
+
# (PgSearch::Document, `pg_search_documents`), ranked together. A document counts only
|
|
19
|
+
# when its record is one its model's relation selects (the model's default scope, its
|
|
20
|
+
# search_results scope, the field's @eq on a column of its): the documents are narrowed
|
|
21
|
+
# in the database, so pages and totals hold only what loads. Each page loads its records
|
|
22
|
+
# through the same relations, a query per model.
|
|
23
|
+
#
|
|
24
|
+
# The gem is not a dependency of Lighthouse: the adapter is used when the
|
|
25
|
+
# model includes PgSearch::Model.
|
|
26
|
+
class Lighthouse::Search::PgSearch
|
|
27
|
+
DEFAULT_SCOPE = :search
|
|
28
|
+
|
|
29
|
+
def searchable?(model, index = nil)
|
|
30
|
+
!!(defined?(::PgSearch::Model) && model.include?(::PgSearch::Model) && pg_search_scope?(model, scope_name(index)))
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
def relational? = true
|
|
34
|
+
|
|
35
|
+
# Does `index` name one of the model's pg_search scopes? (Search::Auto)
|
|
36
|
+
def named?(model, index) = !index.nil? && searchable?(model, index)
|
|
37
|
+
|
|
38
|
+
# The relation narrowed to the records that match, ordered by rank first: a
|
|
39
|
+
# search is in order of relevance, so the orders the relation had (the model's
|
|
40
|
+
# default scope, the field's `scopes:`, the search_results scope) break the ties
|
|
41
|
+
# of rank, before the client's `orderBy` and the field's @orderBy, applied after.
|
|
42
|
+
# pg_search's own tie-break, the primary key (unless the scope names
|
|
43
|
+
# `order_within_rank:`), is dropped so that they decide; the key still ends the
|
|
44
|
+
# order, after them (BaseDirectiveResolver#apply_search).
|
|
45
|
+
def scope(relation, query)
|
|
46
|
+
relation = Lighthouse::Search.results(relation)
|
|
47
|
+
searched = relation.public_send(scope_name(query.index), query.term)
|
|
48
|
+
added = searched.order_values.drop(relation.order_values.size)
|
|
49
|
+
orders = [*without_key_tie_break(added, relation.klass), *relation.order_values]
|
|
50
|
+
orders.empty? ? searched : searched.reorder(*orders)
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
# Models searched together: those that include PgSearch::Model, which must keep multisearch
|
|
54
|
+
# documents (`multisearchable`), whether or not they have a pg_search scope of their own.
|
|
55
|
+
def searchable_together?(model) = !!(defined?(::PgSearch::Model) && model.include?(::PgSearch::Model))
|
|
56
|
+
|
|
57
|
+
def together_error(model)
|
|
58
|
+
return nil if model.respond_to?(:pg_search_multisearchable_options)
|
|
59
|
+
|
|
60
|
+
"#{model.name} keeps no pg_search multisearch documents, which models searched together are searched through: " \
|
|
61
|
+
'call `multisearchable against: [...]` in it'
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
# Models searched together: [one entry per document at `offset`, `limit` of them, in rank
|
|
65
|
+
# order: its record, or nil for one that did not load; how many documents match].
|
|
66
|
+
def page(query, offset:, limit:)
|
|
67
|
+
options!(query)
|
|
68
|
+
documents = documents(query)
|
|
69
|
+
found = limit.zero? ? [] : documents.offset(offset).limit(limit).to_a
|
|
70
|
+
[records(query, found), documents.unscope(:order, :select).count]
|
|
71
|
+
end
|
|
72
|
+
|
|
73
|
+
# pg_search has no result window.
|
|
74
|
+
def result_window(_query) = nil
|
|
75
|
+
|
|
76
|
+
private
|
|
77
|
+
|
|
78
|
+
# The documents that match, in rank order, each one's record selected by its model's relation.
|
|
79
|
+
def documents(query)
|
|
80
|
+
kept = query.models.map do |model|
|
|
81
|
+
::PgSearch::Document.where(searchable_type: model.polymorphic_name, searchable_id: relation(query.for(model)).select(model.primary_key))
|
|
82
|
+
end
|
|
83
|
+
::PgSearch.multisearch(query.term).where(id: kept.reduce(:or).select(:id))
|
|
84
|
+
end
|
|
85
|
+
|
|
86
|
+
# The model's records a search of it may answer: its relation and search_results scope, with the
|
|
87
|
+
# field's @eq conditions; none where a condition names a column the model does not have.
|
|
88
|
+
def relation(query)
|
|
89
|
+
model = query.model
|
|
90
|
+
query.wheres.reduce(Lighthouse::Search.results(model.all)) do |relation, (column, value)|
|
|
91
|
+
model.column_names.include?(column) ? relation.where(column => value) : relation.none
|
|
92
|
+
end
|
|
93
|
+
end
|
|
94
|
+
|
|
95
|
+
# The records of `documents`, in their order, nil for one its model's relation no longer selects.
|
|
96
|
+
# The models that share a document type (an STI class and its subclasses) each load theirs.
|
|
97
|
+
def records(query, documents)
|
|
98
|
+
loaded = documents.group_by(&:searchable_type).to_h do |type, typed|
|
|
99
|
+
ids = typed.map(&:searchable_id)
|
|
100
|
+
found = query.models.select { |model| model.polymorphic_name == type }.flat_map do |model|
|
|
101
|
+
relation(query.for(model)).where(model.primary_key => ids).to_a
|
|
102
|
+
end
|
|
103
|
+
[type, found.index_by { |record| record.id.to_s }]
|
|
104
|
+
end
|
|
105
|
+
documents.map { |document| loaded.dig(document.searchable_type, document.searchable_id.to_s) }
|
|
106
|
+
end
|
|
107
|
+
|
|
108
|
+
# An @builder's Searchkick options, which multisearch cannot take: one that narrows what loads
|
|
109
|
+
# (`scope_results:`) would be dropped.
|
|
110
|
+
def options!(query)
|
|
111
|
+
return if query.options.empty?
|
|
112
|
+
|
|
113
|
+
raise Lighthouse::DefinitionError,
|
|
114
|
+
"@search: pg_search's multisearch takes no option of Searchkick's, which an @builder set: #{query.options.keys.join(', ')}"
|
|
115
|
+
end
|
|
116
|
+
|
|
117
|
+
def scope_name(index) = (index || DEFAULT_SCOPE).to_sym
|
|
118
|
+
|
|
119
|
+
# Is the model's class method `name` a pg_search scope? pg_search keeps no list
|
|
120
|
+
# of them: `pg_search_scope` defines each from pg_search/model.rb (2.3 and 2.4
|
|
121
|
+
# alike), which the method's source tells, so another gem's method of the name
|
|
122
|
+
# (Searchkick's `search`, on a model that uses both) is not taken for one.
|
|
123
|
+
def pg_search_scope?(model, name)
|
|
124
|
+
return false unless model.respond_to?(name)
|
|
125
|
+
|
|
126
|
+
!!model.method(name).source_location&.first&.end_with?(File.join('pg_search', 'model.rb'))
|
|
127
|
+
end
|
|
128
|
+
|
|
129
|
+
# pg_search's orders without its primary-key tie-break, which is a node of its
|
|
130
|
+
# own after the rank's in pg_search 2.4, and the end of one SQL literal in 2.3
|
|
131
|
+
# ("pg_search_1.rank DESC, \"posts\".\"id\" ASC"); the orders themselves when it
|
|
132
|
+
# has none (`order_within_rank:`).
|
|
133
|
+
def without_key_tie_break(orders, model)
|
|
134
|
+
last = orders.last
|
|
135
|
+
return orders[0...-1] if key_node?(last, model)
|
|
136
|
+
|
|
137
|
+
tie_break = ", #{model.quoted_table_name}.#{model.connection.quote_column_name(model.primary_key)} ASC"
|
|
138
|
+
return orders unless last.is_a?(String) && last.end_with?(tie_break)
|
|
139
|
+
|
|
140
|
+
[*orders[0...-1], ::Arel.sql(last.delete_suffix(tie_break))]
|
|
141
|
+
end
|
|
142
|
+
|
|
143
|
+
def key_node?(order, model)
|
|
144
|
+
order.is_a?(::Arel::Nodes::Ascending) && order.expr == model.arel_table[model.primary_key]
|
|
145
|
+
end
|
|
146
|
+
end
|
|
@@ -0,0 +1,54 @@
|
|
|
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
|
+
#
|
|
8
|
+
# A search of several models together (a union's or an interface's: Search::Together) has
|
|
9
|
+
# `models` and no `model`; `for(model)` is the same search of one of them.
|
|
10
|
+
class Lighthouse::Search::Query
|
|
11
|
+
# The model searched, the `@search` argument's value, the `within:` index (nil: the model's own).
|
|
12
|
+
attr_reader :model, :term, :index
|
|
13
|
+
|
|
14
|
+
# The models searched together, or nil for a search of one.
|
|
15
|
+
attr_reader :models
|
|
16
|
+
|
|
17
|
+
# [[column, value], ...], from @eq (and @builder), in order: all of them hold,
|
|
18
|
+
# the same column twice too (a list of inputs), as the database applies them.
|
|
19
|
+
attr_reader :wheres
|
|
20
|
+
|
|
21
|
+
# Adapter-specific options, from @builder on an argument (Searchkick's `fields:`, `boost_by:`, ...).
|
|
22
|
+
attr_reader :options
|
|
23
|
+
|
|
24
|
+
# :with, :only or :without, from @trashed; nil: the client did not say.
|
|
25
|
+
attr_accessor :trashed
|
|
26
|
+
|
|
27
|
+
def initialize(model, term, index = nil, models: nil)
|
|
28
|
+
@model = model
|
|
29
|
+
@term = term
|
|
30
|
+
@index = index
|
|
31
|
+
@models = models
|
|
32
|
+
@wheres = []
|
|
33
|
+
@options = {}
|
|
34
|
+
@trashed = nil
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
# The model's name, or the names of the models searched together, for errors.
|
|
38
|
+
def name = models ? models.map(&:name).join(', ') : model.name
|
|
39
|
+
|
|
40
|
+
# The same search of `model` alone: its term, conditions and options.
|
|
41
|
+
def for(model)
|
|
42
|
+
self.class.new(model, term, index).tap do |one|
|
|
43
|
+
wheres.each { |column, value| one.where(column, value) }
|
|
44
|
+
one.options.merge!(options)
|
|
45
|
+
one.trashed = trashed
|
|
46
|
+
end
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
# The column equals the value, as Scout's `where`.
|
|
50
|
+
def where(column, value)
|
|
51
|
+
@wheres << [column.to_s, value]
|
|
52
|
+
self
|
|
53
|
+
end
|
|
54
|
+
end
|