lighthouse-graphql 0.9.0 → 0.10.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.
Files changed (49) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +80 -1
  3. data/README.md +14 -8
  4. data/docs/configuration.md +4 -10
  5. data/docs/directives.md +4 -0
  6. data/docs/federation.md +250 -75
  7. data/docs/filtering-and-ordering.md +79 -0
  8. data/docs/getting-started.md +2 -4
  9. data/docs/subscriptions.md +142 -2
  10. data/lib/generators/lighthouse/install/templates/initializer.rb.tt +2 -4
  11. data/lib/lighthouse/controller.rb +35 -8
  12. data/lib/lighthouse/directive_resolvers/all_resolver.rb +2 -2
  13. data/lib/lighthouse/directive_resolvers/base.rb +17 -0
  14. data/lib/lighthouse/directive_resolvers/can_resolvers.rb +2 -2
  15. data/lib/lighthouse/directive_resolvers/paginate_resolver.rb +2 -0
  16. data/lib/lighthouse/directives/pagination_manipulator.rb +7 -1
  17. data/lib/lighthouse/event_stream.rb +188 -0
  18. data/lib/lighthouse/federation/any.rb +19 -0
  19. data/lib/lighthouse/federation/apollo_federation_check.rb +89 -0
  20. data/lib/lighthouse/federation/callback_subscriptions.rb +261 -0
  21. data/lib/lighthouse/federation/definitions.rb +83 -0
  22. data/lib/lighthouse/federation/entities.rb +265 -0
  23. data/lib/lighthouse/federation/link.rb +89 -0
  24. data/lib/lighthouse/federation/printer.rb +44 -0
  25. data/lib/lighthouse/federation.rb +145 -0
  26. data/lib/lighthouse/graphql/version.rb +1 -1
  27. data/lib/lighthouse/graphql_ruby_patches.rb +7 -1
  28. data/lib/lighthouse/namespaces.rb +1 -0
  29. data/lib/lighthouse/rb_lighthouse.rb +10 -2
  30. data/lib/lighthouse/scalar_coercion.rb +3 -2
  31. data/lib/lighthouse/schema_generator.rb +14 -17
  32. data/lib/lighthouse/schema_printer.rb +3 -1
  33. data/lib/lighthouse/search/auto.rb +5 -0
  34. data/lib/lighthouse/search/enhancer.rb +25 -1
  35. data/lib/lighthouse/search/field_check.rb +3 -1
  36. data/lib/lighthouse/search/pg_search.rb +70 -0
  37. data/lib/lighthouse/search/query.rb +20 -1
  38. data/lib/lighthouse/search/result.rb +2 -2
  39. data/lib/lighthouse/search/searchkick.rb +47 -4
  40. data/lib/lighthouse/search/together_check.rb +137 -0
  41. data/lib/lighthouse/search.rb +23 -0
  42. data/lib/lighthouse/type_resolution.rb +5 -4
  43. data/sig/lighthouse/federation.rbs +51 -0
  44. data/sig/lighthouse/graphql.rbs +29 -0
  45. data/sig/lighthouse/search.rbs +23 -2
  46. data/sig/lighthouse.rbs +14 -0
  47. metadata +12 -3
  48. data/lib/lighthouse/federation_directives.rb +0 -53
  49. data/lib/lighthouse/reference_resolver.rb +0 -22
@@ -4,10 +4,16 @@
4
4
  # what narrows it. The directives of the other arguments write to it
5
5
  # (Contracts::SearchBuilder), and the adapter runs it (Search::Searchkick#page).
6
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.
7
10
  class Lighthouse::Search::Query
8
11
  # The model searched, the `@search` argument's value, the `within:` index (nil: the model's own).
9
12
  attr_reader :model, :term, :index
10
13
 
14
+ # The models searched together, or nil for a search of one.
15
+ attr_reader :models
16
+
11
17
  # [[column, value], ...], from @eq (and @builder), in order: all of them hold,
12
18
  # the same column twice too (a list of inputs), as the database applies them.
13
19
  attr_reader :wheres
@@ -18,15 +24,28 @@ class Lighthouse::Search::Query
18
24
  # :with, :only or :without, from @trashed; nil: the client did not say.
19
25
  attr_accessor :trashed
20
26
 
21
- def initialize(model, term, index = nil)
27
+ def initialize(model, term, index = nil, models: nil)
22
28
  @model = model
23
29
  @term = term
24
30
  @index = index
31
+ @models = models
25
32
  @wheres = []
26
33
  @options = {}
27
34
  @trashed = nil
28
35
  end
29
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
+
30
49
  # The column equals the value, as Scout's `where`.
31
50
  def where(column, value)
32
51
  @wheres << [column.to_s, value]
@@ -142,7 +142,7 @@ class Lighthouse::Search::Result
142
142
  # Is it the search of `field` that `adapter` runs for `query`? Its options aside,
143
143
  # which may hold an @builder's procs, made again for each search.
144
144
  def same_search?(adapter, query, field)
145
- @adapter.equal?(adapter) && @field == field && query.model.equal?(@query.model) &&
145
+ @adapter.equal?(adapter) && @field == field && query.model.equal?(@query.model) && query.models == @query.models &&
146
146
  [query.term, query.index, query.wheres, query.trashed] == [@query.term, @query.index, @query.wheres, @query.trashed]
147
147
  end
148
148
 
@@ -176,6 +176,6 @@ class Lighthouse::Search::Result
176
176
 
177
177
  raise ::GraphQL::ExecutionError,
178
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."
179
+ "#{@query.name}), so offset #{offset} is past them. Narrow the search instead."
180
180
  end
181
181
  end
@@ -53,16 +53,20 @@ class Lighthouse::Search::Searchkick
53
53
  # [one entry per hit at `offset`, `limit` of them, in relevance order: the
54
54
  # record, or nil for a hit that did not load; the total that match].
55
55
  def page(query, offset:, limit:)
56
- response = query.model.searchkick_search(query.term, **search_options(query), offset: offset, limit: limit)
56
+ response = search(query, offset, limit)
57
57
  exact_total!(query, response)
58
58
  [entries(response), response.total_count]
59
59
  rescue StandardError => e
60
60
  raise unless past_window?(e)
61
61
 
62
62
  # The index's window is smaller than the model's options say: set on the server.
63
- raise ::GraphQL::ExecutionError, "The search reads no further than the index.max_result_window of #{query.model.name}. Narrow the search instead."
63
+ raise ::GraphQL::ExecutionError, "The search reads no further than the index.max_result_window of #{query.name}. Narrow the search instead."
64
64
  end
65
65
 
66
+ # Models searched together: those it searches alone, and why one cannot be as it is (model_error).
67
+ def searchable_together?(model) = searchable?(model)
68
+ def together_error(model) = model_error(model)
69
+
66
70
  # How many of the matches the index the search reads pages through
67
71
  # (Search::Result): its index.max_result_window, read from the engine
68
72
  # (Searchkick::Index). When the settings cannot be read, the model's own index
@@ -70,6 +74,8 @@ class Lighthouse::Search::Searchkick
70
74
  # its index `settings:`, whatever their keys; a billion with `deep_paging:
71
75
  # true`), and any index the engines' default, 10,000.
72
76
  def result_window(query)
77
+ return query.models.map { |model| result_window(query.for(model)) }.min if query.models
78
+
73
79
  @index.result_window(query.model, index_name(query)) ||
74
80
  (own_index?(query) && options_window(query.model)) || Lighthouse::Search::Result::MAX_WINDOW
75
81
  end
@@ -133,7 +139,7 @@ class Lighthouse::Search::Searchkick
133
139
  return unless total.is_a?(Hash) && total['relation'] && total['relation'] != 'eq'
134
140
 
135
141
  raise Lighthouse::DefinitionError,
136
- "@search on #{query.model.name}: the engine counted #{total['relation']} #{total['value']} matches, not every one, " \
142
+ "@search on #{query.name}: the engine counted #{total['relation']} #{total['value']} matches, not every one, " \
137
143
  'which totals, pages and @canQuery need (track_total_hits)'
138
144
  end
139
145
 
@@ -154,14 +160,51 @@ class Lighthouse::Search::Searchkick
154
160
  defined?(::Searchkick::InvalidQueryError) && error.is_a?(::Searchkick::InvalidQueryError) && error.message.include?('max_result_window')
155
161
  end
156
162
 
163
+ # The engine's response to the search, `limit` hits from `offset`: of the model, or of the
164
+ # models searched together, in one request over their indices.
165
+ def search(query, offset, limit)
166
+ return query.model.searchkick_search(query.term, **search_options(query), offset: offset, limit: limit) unless query.models
167
+
168
+ ::Searchkick.search(query.term, models: query.models, **together_options(query), offset: offset, limit: limit)
169
+ end
170
+
157
171
  def search_options(query)
158
172
  options = Lighthouse::Search::Searchkick::Options.builder!(query)
159
173
  wheres = query.wheres.map { |column, value| { column => value } }
160
174
  options[:where] = all_of(options[:where], engine_where(query), *wheres, trashed_filter(query))
161
175
  options[:index_name] = query.index if query.index
176
+ loaded(options, results_scope(query, options[:scope_results]))
177
+ end
178
+
179
+ # The options of a search of several models, in one request over their indices. What narrows
180
+ # each model holds in its own index only (`_index`, which the engines match against the
181
+ # alias Searchkick names): its search_where, and the records its default scope hides; what the
182
+ # client sends (@eq) holds in every index, matching nothing in one that does not hold the
183
+ # column. Each model's records load through its own narrowing (results_scope), which Searchkick
184
+ # calls with each model's relation.
185
+ def together_options(query)
186
+ options = Lighthouse::Search::Searchkick::Options.builder!(query)
187
+ wheres = query.wheres.map { |column, value| { column => value } }
188
+ each_own = query.models.map { |model| own_where(query.for(model)) }
189
+ options[:where] = all_of(options[:where], *wheres, { _or: each_own })
190
+ loads = query.models.index_with { |model| results_scope(query.for(model), options[:scope_results]) }.compact
191
+ loaded(options, loads.empty? ? nil : ->(records) { (loads[model_of(records)] || :itself.to_proc).call(records) })
192
+ end
193
+
194
+ # What Searchkick loads a model's hits through: the model itself, or a relation of it.
195
+ def model_of(records) = records.respond_to?(:klass) ? records.klass : records
196
+
197
+ # What one model's hits must hold in a search of several: being in its index, and what its own
198
+ # search would have the engine apply.
199
+ def own_where(query)
200
+ all_of({ _index: query.model.searchkick_index.name }, engine_where(query), trashed_filter(query))
201
+ end
202
+
203
+ # The options Lighthouse sets whatever the search: the records load, through `loading` (the
204
+ # `scope_results:` that narrows them) if any, and every match is counted.
205
+ def loaded(options, loading)
162
206
  options[:load] = true
163
207
  options[:body_options] = Lighthouse::Search::Searchkick::Options.counting(options)
164
- loading = results_scope(query, options[:scope_results])
165
208
  options[:scope_results] = loading if loading
166
209
  options
167
210
  end
@@ -0,0 +1,137 @@
1
+ # frozen_string_literal: true
2
+
3
+ # A field of a union or an interface (with @all or @paginate, and no `model:`) whose @search
4
+ # searches the models of its types together, in one search, checked when the schema is built
5
+ # (SchemaGenerator, before FieldCheck, which leaves the field to it), as FieldCheck checks a
6
+ # field of one model. It keeps the models for the field's resolvers (Search.together!).
7
+ #
8
+ # Each type is a model's (@model, or its name), one engine searches them all
9
+ # (`adapter_together`, `together_error`), the @search argument is a top-level, non-null one,
10
+ # with no `within:`, on a root field; and nothing a model would apply alone, but the engine
11
+ # across them cannot, is asked for: `scopes:`, @builder, @whereAuth, @orderBy(column:),
12
+ # @trashed, a default value the engine cannot apply, an @canQuery's `scopes:`.
13
+ class Lighthouse::Search::TogetherCheck < Lighthouse::Search::FieldCheck
14
+ # Every field of the schema's object types (`root_kinds`: SchemaGenerator#root_kinds).
15
+ def self.check_schema!(schema, root_kinds)
16
+ schema.types.each_value do |type|
17
+ next unless type.kind.object?
18
+
19
+ type.fields.each_value { |field_def| check!(schema, type, field_def, root_kinds[type.graphql_name]) if field_def.owner == type }
20
+ end
21
+ end
22
+
23
+ def self.check!(schema, type, field_def, root_kind)
24
+ return unless field_def.ast_node
25
+
26
+ new(schema, type, field_def, root_kind).check!
27
+ end
28
+
29
+ def initialize(schema, type, field_def, root_kind)
30
+ super(type, field_def, field_def.ast_node.directives || [], root_kind)
31
+ @schema = schema
32
+ end
33
+
34
+ def check!
35
+ searches = Lighthouse::Search.directives(@field_def)
36
+ together!(searches) if searches.any? && together?
37
+ end
38
+
39
+ private
40
+
41
+ # Does the field search the models of a union's or an interface's types together? One that a
42
+ # data directive resolves without a `model:` of its own.
43
+ def together?
44
+ !resolving_directive.nil? && returned&.kind&.abstract? &&
45
+ Lighthouse::Support::DirectiveArgs.literal(resolving_directive, 'model').nil?
46
+ end
47
+
48
+ # The type of what the field returns: of its items, for a paginated field (`PostPaginator`'s Post).
49
+ def returned
50
+ type = @field_def.type.unwrap
51
+ return type unless resolving_directive.name == 'paginate'
52
+
53
+ @schema.get_type(type.graphql_name.sub(Lighthouse::Support::ModelResolver::PAGINATION_SUFFIX, ''))
54
+ end
55
+
56
+ def together!(searches)
57
+ path = searches.first.first
58
+ searches.each { |each_path, directive| within_string!(each_path, directive) }
59
+ applied!(path)
60
+ together_field!(path, searches)
61
+ models = together_models(path)
62
+ adapter = together_adapter!(path, models)
63
+ together_constraints!(path, adapter)
64
+ can_queries.each { |can_query| together_can_query!(path, can_query) }
65
+ Lighthouse::Search.together!(@field_def, models)
66
+ end
67
+
68
+ # On a root field, with @all or @paginate, and one @search: a top-level, non-null argument
69
+ # (with no models to list, a field of several models answers a search, always), with no `within:`.
70
+ def together_field!(path, searches)
71
+ unless @root_kind && %w[all paginate].include?(resolving_directive.name)
72
+ fail!(path, "#{abstract_name} has several models, which only @all or @paginate on a root field search together " \
73
+ '(or give the directive the `model:` it searches)')
74
+ end
75
+ one_search!(path, searches)
76
+ return if Lighthouse::Support::DirectiveArgs.literal(searches.first.last, 'within').nil?
77
+
78
+ fail!(path, "`within:` names one model's index or scope, and #{abstract_name}'s models are searched together")
79
+ end
80
+
81
+ def one_search!(path, searches)
82
+ return if searches.size == 1 && @field_def.arguments[path]&.type&.non_null?
83
+
84
+ fail!(path, "a field that searches #{abstract_name}'s models together needs its search: one @search, on a non-null argument " \
85
+ '(`search: String!`), which there is no table to list without')
86
+ end
87
+
88
+ # The models of the union's or the interface's object types: each must be a model's.
89
+ def together_models(path)
90
+ types = @schema.possible_types(returned).sort_by(&:graphql_name)
91
+ types.map do |type|
92
+ Lighthouse::GraphQL::Directives::ModelDirective.model_for(type) ||
93
+ fail!(path, "#{abstract_name}'s type #{type.graphql_name} is no model's: give it @model(class:), or remove it")
94
+ end
95
+ end
96
+
97
+ def together_adapter!(path, models)
98
+ adapter = Lighthouse::Search.adapter_together(models) || no_engine!(path, models)
99
+ models.each do |model|
100
+ error = adapter.together_error(model)
101
+ fail!(path, error) if error
102
+ end
103
+ adapter
104
+ end
105
+
106
+ # What one model's search would apply, and a search of several cannot: the field's own query
107
+ # directives and `scopes:`, @trashed (each model has its own soft-delete column) and default
108
+ # values the engine cannot apply.
109
+ def together_constraints!(path, adapter)
110
+ name = adapter.class.name.demodulize
111
+ dropped = dropped_constraints
112
+ fail!(path, "#{name} cannot apply #{dropped.join(', ')} to models searched together; remove them") unless dropped.empty?
113
+ trashed = Lighthouse::Support::ArgumentWalk.field_definitions(@field_def).find do |_, argument|
114
+ Lighthouse::Support::ArgumentWalk.directives(argument).any? { |directive| directive.name == 'trashed' }
115
+ end
116
+ fail!(path, "@trashed on #{trashed.first} cannot filter models searched together, each with its own soft deletes") if trashed
117
+ defaulted_constraints.each do |constraint|
118
+ fail!(path, "#{name} cannot apply #{constraint}, whose default value would apply to every search; remove the default")
119
+ end
120
+ end
121
+
122
+ def no_engine!(path, models)
123
+ engines = models.map { |model| "#{model.name} (#{engine_name(model)})" }
124
+ fail!(path, "no engine searches #{abstract_name}'s models together: #{engines.join(', ')}. One engine searches them " \
125
+ 'all: Searchkick, or pg_search through its multisearch documents')
126
+ end
127
+
128
+ def engine_name(model) = Lighthouse::Search.adapter_for(model)&.class&.name&.demodulize || 'not searchable'
129
+
130
+ def together_can_query!(path, can_query)
131
+ return if Lighthouse::Support::DirectiveArgs.literal(can_query, 'scopes').blank?
132
+
133
+ fail!(path, 'the `scopes:` of @canQuery cannot narrow models searched together; remove them')
134
+ end
135
+
136
+ def abstract_name = returned.graphql_name
137
+ end
@@ -8,6 +8,12 @@
8
8
  # relational? is the search a scope on the model's relation (pg_search)?
9
9
  # scope(relation, query) relational: the relation narrowed to the matches, ranked
10
10
  # page(query, offset:, limit:) otherwise: [records, total] of the engine's response
11
+ # searchable_together?(model) can it search the model with others (optional, with together_error)?
12
+ # together_error(model) why it cannot search the model with others as it is, or nil
13
+ #
14
+ # Models searched together (a field of a union or an interface: FieldCheck) are one search
15
+ # over all of them, a Query with `models`, which the adapter answers with `page`, whatever it
16
+ # is: Searchkick over their indices, pg_search through its multisearch documents.
11
17
  #
12
18
  # `query` is a Search::Query. A relational adapter lets every other directive of
13
19
  # the field apply on top of the scope; the others (Searchkick) answer on their own
@@ -75,4 +81,21 @@ module Lighthouse::Search
75
81
 
76
82
  # Does the field have an argument or input field with @search?
77
83
  def searching?(field_def) = directives(field_def).any?
84
+
85
+ # The models the field searches together (Search::FieldCheck keeps them when the schema is
86
+ # built: the models of its union's or interface's types), or nil for a field of one model.
87
+ def together(field_def) = Lighthouse::Support::Memo.on(field_def, :searched_together) { nil }
88
+
89
+ def together!(field_def, models)
90
+ field_def.instance_variable_set(:@lighthouse_searched_together, models.freeze)
91
+ end
92
+
93
+ # The adapter that searches `models` together (`searchable_together?` each of them, and
94
+ # `together_error`), or nil when none does.
95
+ def adapter_together(models)
96
+ adapter = self.adapter
97
+ return adapter.adapter_together(models) if adapter.respond_to?(:adapter_together)
98
+
99
+ adapter if adapter.respond_to?(:together_error) && models.all? { |model| adapter.searchable_together?(model) }
100
+ end
78
101
  end
@@ -60,8 +60,11 @@ module Lighthouse::GraphQL::TypeResolution
60
60
  value.nil? ? type : [type, value]
61
61
  end
62
62
 
63
- # What the directive's class or the type class says, or nil.
63
+ # What the directive's class or the type class says, or nil; for a
64
+ # subgraph's _Entity, the type `_entities` found the object as.
64
65
  def custom(abstract_type, object, context)
66
+ return Lighthouse::GraphQL::Federation::Entities.type_of(object, context) if abstract_type.graphql_name == '_Entity'
67
+
65
68
  resolver = directive_class(abstract_type) || type_class(abstract_type)
66
69
  return unless resolver
67
70
 
@@ -125,9 +128,7 @@ module Lighthouse::GraphQL::TypeResolution
125
128
  end
126
129
 
127
130
  def typename_of(object)
128
- return unless object.is_a?(Hash)
129
-
130
- (object['__typename'] || object[:__typename])&.to_s
131
+ (object['__typename'] || object[:__typename])&.to_s if object.is_a?(Hash)
131
132
  end
132
133
 
133
134
  # Admin, User, ApplicationRecord, ... for an Admin < User record.
@@ -0,0 +1,51 @@
1
+ # Apollo Federation: a schema linking the Federation spec is a subgraph. See docs/federation.md.
2
+ module Lighthouse
3
+ module GraphQL
4
+ module Federation
5
+ # Posts a callback message (a JSON body) and answers the router's status.
6
+ interface _CallbackClient
7
+ def post: (String url, String body) -> Integer
8
+ end
9
+
10
+ # Federated subscriptions: a graphql-ruby subscription backend (graphql-ruby
11
+ # ships no RBS, so its superclass, GraphQL::Subscriptions, is not declared)
12
+ # that delivers updates to Apollo Router through its HTTP callback protocol.
13
+ # `use Lighthouse::GraphQL::Federation::CallbackSubscriptions` in the schema class.
14
+ class CallbackSubscriptions
15
+ CONTEXT_KEY: Symbol
16
+ HEADERS: Hash[String, String]
17
+
18
+ # The router's `extensions.subscription` for a subscription it asks for.
19
+ class Callback
20
+ attr_accessor url: String
21
+ attr_accessor id: String
22
+ attr_accessor verifier: String
23
+ attr_accessor heartbeat_ms: Integer
24
+
25
+ def self.from_extensions: (untyped extensions) -> Callback?
26
+ def self.subscription_of: (untyped extensions) -> Hash[String, untyped]?
27
+ def message: (String action, **json rest) -> Hash[Symbol, json]
28
+ end
29
+
30
+ class NetHttpClient
31
+ include _CallbackClient
32
+
33
+ def initialize: (?open_timeout: Numeric, ?read_timeout: Numeric) -> void
34
+ end
35
+
36
+ # The context of an operation the router sent (its request's `extensions`); empty for any other.
37
+ def self.context_for: (untyped extensions) -> Hash[Symbol, Callback]
38
+ # The body and status that answer the router's request; nil for any other operation.
39
+ def self.response: (untyped result) -> [Hash[String, json], Integer]?
40
+
41
+ # `action_cable:` and the rest are ActionCableSubscriptions' options.
42
+ def initialize: (?client: _CallbackClient, ?tick: Numeric, ?action_cable: untyped, **untyped options) -> void
43
+ # Sends the heartbeats that are due (a timer calls it every `tick` seconds).
44
+ def heartbeat!: (?Float now) -> void
45
+ # The ids of the subscriptions this process keeps.
46
+ def subscription_ids: () -> Array[String]
47
+ def delete_subscription: (String subscription_id) -> void
48
+ end
49
+ end
50
+ end
51
+ end
@@ -296,6 +296,35 @@ module Lighthouse
296
296
  end
297
297
  end
298
298
 
299
+ # GraphQL over Server-Sent Events (graphql-sse's distinct connections mode): an operation's
300
+ # result as a `next` event and, a subscription, each update as another, until `complete`. The
301
+ # stream is the operation's Action Cable channel (`channel` in its context).
302
+ class EventStream
303
+ CONTENT_TYPE: String
304
+ KEEP_ALIVE: String
305
+ COMPLETE: String
306
+
307
+ # Does an Accept header take an event stream?
308
+ def self.accepted?: (String? accept) -> bool
309
+ def self.headers: () -> Hash[String, String]
310
+ # The `next` event of a result.
311
+ def self.event: (Hash[String, untyped] result) -> String
312
+ # Each `next` event's result, as a client reads a stream's body.
313
+ def self.parse: (String? body) -> Array[Hash[String, json]]
314
+
315
+ attr_reader subscription_id: String?
316
+
317
+ def initialize: (_Schema schema, ?pubsub: _PubSub?, ?keep_alive: Numeric, ?on_error: (^(StandardError) -> void)?,
318
+ ?on_end: (^() -> void)?) -> void
319
+ # The operation's result: the first event, and whether the stream stays open.
320
+ def start: (_Result result) -> self
321
+ # What ActionCableSubscriptions asks of a channel.
322
+ def stream_from: (String broadcasting, ?untyped callback, ?coder: untyped) ?{ (untyped message) -> void } -> void
323
+ def each: () { (String event) -> void } -> void
324
+ def finish: () -> void
325
+ def open?: () -> bool
326
+ end
327
+
299
328
  # The opt-in cache of the manipulated schema document, in tmp/cache.
300
329
  module SchemaCache
301
330
  PREFIX: String
@@ -21,9 +21,18 @@ module Lighthouse
21
21
  # Does the field have an argument or input field with @search? Walked once per field.
22
22
  def self.searching?: (_FieldDefinition field_def) -> bool
23
23
 
24
+ # The models the field searches together (a union's or an interface's), kept when the schema is built; nil for one model.
25
+ def self.together: (_FieldDefinition field_def) -> Array[Class]?
26
+ def self.together!: (_FieldDefinition field_def, Array[Class] models) -> Array[Class]
27
+ # The adapter that searches the models together, or nil.
28
+ def self.adapter_together: (Array[Class] models) -> _TogetherSearchAdapter?
29
+
24
30
  # What a search holds: the model, the term and what narrows it.
25
31
  class Query
26
- attr_reader model: Class
32
+ # nil for models searched together.
33
+ attr_reader model: Class?
34
+ # The models searched together, or nil for a search of one.
35
+ attr_reader models: Array[Class]?
27
36
  attr_reader term: String
28
37
  attr_reader index: String?
29
38
  # [column, value] pairs, from @eq, all of which hold.
@@ -33,8 +42,12 @@ module Lighthouse
33
42
  # :with, :only or :without, from @trashed; nil: not given.
34
43
  attr_accessor trashed: (:with | :only | :without)?
35
44
 
36
- def initialize: (Class model, String term, ?String? index) -> void
45
+ def initialize: (Class? model, String term, ?String? index, ?models: Array[Class]?) -> void
37
46
  def where: (String | Symbol column, untyped value) -> self
47
+ # The model's name, or the names of the models searched together.
48
+ def name: () -> String
49
+ # The same search of one model.
50
+ def for: (Class model) -> Query
38
51
  end
39
52
 
40
53
  # A search an engine answers: the page it is asked for and its total.
@@ -74,6 +87,7 @@ module Lighthouse
74
87
  # Searchkick (Elasticsearch / OpenSearch), outside the database.
75
88
  class Searchkick
76
89
  include _EngineSearchAdapter
90
+ include _TogetherSearchAdapter
77
91
 
78
92
  DEFAULT_TRASHED_COLUMN: String
79
93
  DEEP_PAGING_WINDOW: Integer
@@ -114,9 +128,14 @@ module Lighthouse
114
128
  # pg_search (PostgreSQL full-text search), in the database.
115
129
  class PgSearch
116
130
  include _RelationalSearchAdapter
131
+ include _EngineSearchAdapter
132
+ include _TogetherSearchAdapter
117
133
 
118
134
  DEFAULT_SCOPE: Symbol
119
135
 
136
+ # Models searched together, through multisearch's documents (page), which have no result window.
137
+ def result_window: (Query query) -> nil
138
+
120
139
  # Does `index` name one of the model's pg_search scopes?
121
140
  def named?: (Class model, String? index) -> bool
122
141
  end
@@ -127,6 +146,8 @@ module Lighthouse
127
146
 
128
147
  def initialize: (?Array[_SearchAdapter]? adapters) -> void
129
148
  def searchable?: (Class model, ?String? index) -> bool
149
+ # The first adapter that searches every one of the models together, or nil.
150
+ def adapter_together: (Array[Class] models) -> _TogetherSearchAdapter?
130
151
  end
131
152
  end
132
153
  end
data/sig/lighthouse.rbs CHANGED
@@ -166,6 +166,12 @@ module Lighthouse
166
166
  def error: (?String? message) -> void
167
167
  end
168
168
 
169
+ # An Action Cable pubsub adapter (ActionCable.server.pubsub: async, redis, solid_cable, ...).
170
+ interface _PubSub
171
+ def subscribe: (String channel, ^(String) -> void message_callback, ?(^() -> void)? success_callback) -> void
172
+ def unsubscribe: (String channel, ^(String) -> void message_callback) -> void
173
+ end
174
+
169
175
  # --- Values ----------------------------------------------------------------
170
176
 
171
177
  # A field's arguments, with snake_case Symbol keys as graphql-ruby passes them.
@@ -280,6 +286,14 @@ module Lighthouse
280
286
  def adapter_for: (Class model, ?String? index) -> _SearchAdapter?
281
287
  end
282
288
 
289
+ # An adapter that searches models together (a union's or an interface's): its `page` (as
290
+ # _EngineSearchAdapter's) answers a Search::Query with `models`, and `together_error(model)`
291
+ # why one cannot be searched with others as it is (a schema error).
292
+ interface _TogetherSearchAdapter
293
+ def searchable_together?: (Class model) -> boolish
294
+ def together_error: (Class model) -> String?
295
+ end
296
+
283
297
  # What DirectiveRegistry#register_manipulator takes: it transforms the SDL
284
298
  # before the schema is built.
285
299
  interface _SdlManipulator
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: lighthouse-graphql
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.9.0
4
+ version: 0.10.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Vaz Innovation
@@ -175,12 +175,20 @@ files:
175
175
  - lib/lighthouse/document_ast.rb
176
176
  - lib/lighthouse/enum_directive.rb
177
177
  - lib/lighthouse/errors.rb
178
+ - lib/lighthouse/event_stream.rb
178
179
  - lib/lighthouse/events.rb
179
180
  - lib/lighthouse/events/execution_trace.rb
180
181
  - lib/lighthouse/events/lifecycle.rb
181
182
  - lib/lighthouse/events/schema_build.rb
182
183
  - lib/lighthouse/feature_directive.rb
183
- - lib/lighthouse/federation_directives.rb
184
+ - lib/lighthouse/federation.rb
185
+ - lib/lighthouse/federation/any.rb
186
+ - lib/lighthouse/federation/apollo_federation_check.rb
187
+ - lib/lighthouse/federation/callback_subscriptions.rb
188
+ - lib/lighthouse/federation/definitions.rb
189
+ - lib/lighthouse/federation/entities.rb
190
+ - lib/lighthouse/federation/link.rb
191
+ - lib/lighthouse/federation/printer.rb
184
192
  - lib/lighthouse/field_resolver_stack.rb
185
193
  - lib/lighthouse/generated_resolvers.rb
186
194
  - lib/lighthouse/global_id.rb
@@ -220,7 +228,6 @@ files:
220
228
  - lib/lighthouse/persisted_queries/manifest.rb
221
229
  - lib/lighthouse/railtie.rb
222
230
  - lib/lighthouse/rb_lighthouse.rb
223
- - lib/lighthouse/reference_resolver.rb
224
231
  - lib/lighthouse/relation_batch.rb
225
232
  - lib/lighthouse/relation_counts.rb
226
233
  - lib/lighthouse/resolve_info.rb
@@ -245,6 +252,7 @@ files:
245
252
  - lib/lighthouse/search/searchkick.rb
246
253
  - lib/lighthouse/search/searchkick_index.rb
247
254
  - lib/lighthouse/search/searchkick_options.rb
255
+ - lib/lighthouse/search/together_check.rb
248
256
  - lib/lighthouse/server_directives.rb
249
257
  - lib/lighthouse/subscription.rb
250
258
  - lib/lighthouse/support/argument_walk.rb
@@ -276,6 +284,7 @@ files:
276
284
  - sig/lighthouse/controller.rbs
277
285
  - sig/lighthouse/directives.rbs
278
286
  - sig/lighthouse/events.rbs
287
+ - sig/lighthouse/federation.rbs
279
288
  - sig/lighthouse/graphql.rbs
280
289
  - sig/lighthouse/search.rbs
281
290
  homepage: https://github.com/Vaz-Innovation/lighthouse-graphql
@@ -1,53 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- # Object-level Apollo Federation directives (@key, @extends, @shareable,
4
- # @inaccessible, @interfaceObject) applied to the types built from SDL, so the
5
- # Apollo Federation helper can detect entities and add _entities. Only types
6
- # that know federation (they answer `key`) are touched.
7
- module Lighthouse::GraphQL::FederationDirectives
8
- # Directive => the type's federation method it calls. Each is called only
9
- # when the type has it, so a partial federation setup still builds.
10
- FLAGS = {
11
- 'extends' => :extend_type,
12
- 'shareable' => :shareable,
13
- 'inaccessible' => :inaccessible,
14
- 'interfaceObject' => :interface_object
15
- }.freeze
16
-
17
- module_function
18
-
19
- def apply(type)
20
- return unless type.ast_node && type.respond_to?(:key)
21
-
22
- type.ast_node.directives.each do |dnode|
23
- if dnode.name == 'key'
24
- apply_key(type, dnode)
25
- elsif (flag = FLAGS[dnode.name]) && type.respond_to?(flag)
26
- type.public_send(flag)
27
- end
28
- end
29
- end
30
-
31
- def apply_key(type, dnode)
32
- arg = dnode.arguments.find { |a| a.name == 'fields' }
33
- type.key(fields: arg.value) if arg.respond_to?(:value)
34
-
35
- resolve_references(type)
36
- end
37
-
38
- # Setup federation resolve_reference: prefer a custom resolver
39
- # registered with ReferenceResolver, but fall back to the base
40
- # type's own resolve_reference (e.g. BaseObject's default model
41
- # lookup) so direct-model entities resolve without a custom
42
- # handler. Capture the inherited implementation before shadowing.
43
- def resolve_references(type)
44
- default_resolve_reference = type.respond_to?(:resolve_reference) ? type.method(:resolve_reference) : nil
45
-
46
- type.define_singleton_method(:resolve_reference) do |reference, context|
47
- resolved = ::Lighthouse::ReferenceResolver.resolve(type.graphql_name, reference, context)
48
- next resolved unless resolved.nil?
49
-
50
- default_resolve_reference&.call(reference, context)
51
- end
52
- end
53
- end
@@ -1,22 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- class Lighthouse::ReferenceResolver
4
- @resolvers = {}
5
-
6
- class << self
7
- def register(typename, &block)
8
- @resolvers[typename.to_s] = block
9
- end
10
-
11
- def resolve(typename, reference, context)
12
- resolver = @resolvers[typename.to_s]
13
- return nil unless resolver
14
-
15
- resolver.call(reference, context)
16
- end
17
-
18
- def clear!
19
- @resolvers.clear
20
- end
21
- end
22
- end