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.
- checksums.yaml +4 -4
- data/CHANGELOG.md +80 -1
- data/README.md +14 -8
- data/docs/configuration.md +4 -10
- data/docs/directives.md +4 -0
- data/docs/federation.md +250 -75
- data/docs/filtering-and-ordering.md +79 -0
- data/docs/getting-started.md +2 -4
- data/docs/subscriptions.md +142 -2
- data/lib/generators/lighthouse/install/templates/initializer.rb.tt +2 -4
- data/lib/lighthouse/controller.rb +35 -8
- data/lib/lighthouse/directive_resolvers/all_resolver.rb +2 -2
- data/lib/lighthouse/directive_resolvers/base.rb +17 -0
- data/lib/lighthouse/directive_resolvers/can_resolvers.rb +2 -2
- data/lib/lighthouse/directive_resolvers/paginate_resolver.rb +2 -0
- data/lib/lighthouse/directives/pagination_manipulator.rb +7 -1
- data/lib/lighthouse/event_stream.rb +188 -0
- data/lib/lighthouse/federation/any.rb +19 -0
- data/lib/lighthouse/federation/apollo_federation_check.rb +89 -0
- data/lib/lighthouse/federation/callback_subscriptions.rb +261 -0
- data/lib/lighthouse/federation/definitions.rb +83 -0
- data/lib/lighthouse/federation/entities.rb +265 -0
- data/lib/lighthouse/federation/link.rb +89 -0
- data/lib/lighthouse/federation/printer.rb +44 -0
- data/lib/lighthouse/federation.rb +145 -0
- data/lib/lighthouse/graphql/version.rb +1 -1
- data/lib/lighthouse/graphql_ruby_patches.rb +7 -1
- data/lib/lighthouse/namespaces.rb +1 -0
- data/lib/lighthouse/rb_lighthouse.rb +10 -2
- data/lib/lighthouse/scalar_coercion.rb +3 -2
- data/lib/lighthouse/schema_generator.rb +14 -17
- data/lib/lighthouse/schema_printer.rb +3 -1
- data/lib/lighthouse/search/auto.rb +5 -0
- data/lib/lighthouse/search/enhancer.rb +25 -1
- data/lib/lighthouse/search/field_check.rb +3 -1
- data/lib/lighthouse/search/pg_search.rb +70 -0
- data/lib/lighthouse/search/query.rb +20 -1
- data/lib/lighthouse/search/result.rb +2 -2
- data/lib/lighthouse/search/searchkick.rb +47 -4
- data/lib/lighthouse/search/together_check.rb +137 -0
- data/lib/lighthouse/search.rb +23 -0
- data/lib/lighthouse/type_resolution.rb +5 -4
- data/sig/lighthouse/federation.rbs +51 -0
- data/sig/lighthouse/graphql.rbs +29 -0
- data/sig/lighthouse/search.rbs +23 -2
- data/sig/lighthouse.rbs +14 -0
- metadata +12 -3
- data/lib/lighthouse/federation_directives.rb +0 -53
- data/lib/lighthouse/reference_resolver.rb +0 -22
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
# The schema's link to the Apollo Federation spec, as a subgraph declares it:
|
|
4
|
+
#
|
|
5
|
+
# extend schema @link(url: "https://specs.apollo.dev/federation/v2.9", import: ["@key", "@shareable"])
|
|
6
|
+
#
|
|
7
|
+
# It says which version of Federation the subgraph speaks, and what the spec's
|
|
8
|
+
# directives and types are called in it: an imported one keeps its name (or
|
|
9
|
+
# the one `{ name: "@key", as: "@primaryKey" }` gives it), any other is
|
|
10
|
+
# prefixed with the link's namespace (`@federation__key`, or `as:`'s). A
|
|
11
|
+
# schema without the link is a Federation 1 subgraph (`federation: true`),
|
|
12
|
+
# whose directives have no prefix, as in PHP Lighthouse.
|
|
13
|
+
class Lighthouse::GraphQL::Federation::Link
|
|
14
|
+
URL = %r{\Ahttps://specs\.apollo\.dev/federation/v(\d+)\.(\d+)\z}
|
|
15
|
+
|
|
16
|
+
Nodes = ::GraphQL::Language::Nodes
|
|
17
|
+
|
|
18
|
+
attr_reader :version, :namespace
|
|
19
|
+
|
|
20
|
+
# The link in the document's `schema` / `extend schema` directives, or nil.
|
|
21
|
+
def self.from_document(document)
|
|
22
|
+
document.definitions.each do |definition|
|
|
23
|
+
next unless definition.is_a?(Nodes::SchemaDefinition) || definition.is_a?(Nodes::SchemaExtension)
|
|
24
|
+
|
|
25
|
+
definition.directives.each do |directive|
|
|
26
|
+
link = from_directive(directive)
|
|
27
|
+
return link if link
|
|
28
|
+
end
|
|
29
|
+
end
|
|
30
|
+
nil
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
def self.from_directive(directive)
|
|
34
|
+
return unless directive.name == 'link'
|
|
35
|
+
|
|
36
|
+
url = Lighthouse::Support::DirectiveArgs.literal(directive, 'url').to_s
|
|
37
|
+
match = URL.match(url)
|
|
38
|
+
return unless match
|
|
39
|
+
|
|
40
|
+
new(version: [match[1].to_i, match[2].to_i],
|
|
41
|
+
namespace: Lighthouse::Support::DirectiveArgs.literal(directive, 'as') || 'federation',
|
|
42
|
+
imports: Lighthouse::Support::DirectiveArgs.literal(directive, 'import'))
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
# A Federation 1 subgraph: no link, every name as the spec writes it.
|
|
46
|
+
def self.v1
|
|
47
|
+
new(version: [1, 0], namespace: nil, imports: nil)
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
def initialize(version:, namespace:, imports:)
|
|
51
|
+
@version = version
|
|
52
|
+
@namespace = namespace
|
|
53
|
+
@imports = parse_imports(imports)
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
def v2? = version.first >= 2
|
|
57
|
+
|
|
58
|
+
# The name the spec's directive `name` (no `@`) has in this schema.
|
|
59
|
+
def directive(name)
|
|
60
|
+
local(name, "@#{name}")&.delete_prefix('@')
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
# The name the spec's type `name` (`FieldSet`) has in this schema.
|
|
64
|
+
def type(name)
|
|
65
|
+
return "_#{name}" unless v2?
|
|
66
|
+
|
|
67
|
+
local(name, name)
|
|
68
|
+
end
|
|
69
|
+
|
|
70
|
+
private
|
|
71
|
+
|
|
72
|
+
def local(name, element)
|
|
73
|
+
return name unless v2?
|
|
74
|
+
|
|
75
|
+
@imports.fetch(element) { "#{namespace}__#{name}" }
|
|
76
|
+
end
|
|
77
|
+
|
|
78
|
+
# { "@key" => "@key", "@shareable" => "@share", "FieldSet" => "FieldSet" }
|
|
79
|
+
def parse_imports(imports)
|
|
80
|
+
Array(imports).to_h do |import|
|
|
81
|
+
if import.is_a?(Hash)
|
|
82
|
+
import = import.transform_keys(&:to_s)
|
|
83
|
+
[import['name'].to_s, (import['as'] || import['name']).to_s]
|
|
84
|
+
else
|
|
85
|
+
[import.to_s, import.to_s]
|
|
86
|
+
end
|
|
87
|
+
end
|
|
88
|
+
end
|
|
89
|
+
end
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
# The subgraph's SDL, which `Query._service { sdl }` answers and the gateway
|
|
4
|
+
# composes, as PHP Lighthouse's FederationPrinter prints it: the schema as
|
|
5
|
+
# clients see it (Lighthouse's directives are not in it, see ServerDirectives),
|
|
6
|
+
# with its `extend schema @link(...)` and every directive it applies, on every
|
|
7
|
+
# kind of member, but without what the spec adds or declares: `_Any`,
|
|
8
|
+
# `_Entity`, `_Service`, `Query._entities` and `_service` (and Query itself,
|
|
9
|
+
# when it has no other field), and the definitions of the spec's directives and
|
|
10
|
+
# types, which the gateway knows from the link. Directives the app declares
|
|
11
|
+
# (one it composes with @composeDirective) keep their definitions.
|
|
12
|
+
module Lighthouse::GraphQL::Federation::Printer
|
|
13
|
+
Nodes = ::GraphQL::Language::Nodes
|
|
14
|
+
|
|
15
|
+
module_function
|
|
16
|
+
|
|
17
|
+
def print(schema)
|
|
18
|
+
"#{::GraphQL::Language::Printer.new.print(document(schema)).strip}\n"
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
def document(schema)
|
|
22
|
+
link = Lighthouse::GraphQL::Federation.schema_link(schema)
|
|
23
|
+
document = ::GraphQL::Language::DocumentFromSchemaDefinition.new(schema).document
|
|
24
|
+
spec_directives = Lighthouse::GraphQL::Federation::Definitions.directives(link).keys.to_set
|
|
25
|
+
spec_types = Lighthouse::GraphQL::Federation::Definitions.types(link).keys.to_set + Lighthouse::GraphQL::Federation::TYPES
|
|
26
|
+
|
|
27
|
+
definitions = document.definitions.filter_map do |definition|
|
|
28
|
+
case definition
|
|
29
|
+
when Nodes::DirectiveDefinition then definition unless spec_directives.include?(definition.name)
|
|
30
|
+
when Nodes::SchemaDefinition, Nodes::SchemaExtension then definition
|
|
31
|
+
else root_query(schema, definition) unless spec_types.include?(definition.name)
|
|
32
|
+
end
|
|
33
|
+
end
|
|
34
|
+
document.merge(definitions: definitions)
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
# Query without the spec's fields; nil when nothing else is left.
|
|
38
|
+
def root_query(schema, definition)
|
|
39
|
+
return definition unless definition.is_a?(Nodes::ObjectTypeDefinition) && definition.name == schema.query&.graphql_name
|
|
40
|
+
|
|
41
|
+
fields = definition.fields.reject { |field| Lighthouse::GraphQL::Federation::FIELDS.include?(field.name) }
|
|
42
|
+
definition.merge(fields: fields) if fields.any?
|
|
43
|
+
end
|
|
44
|
+
end
|
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
# Apollo Federation: the schema becomes a subgraph a gateway (Apollo Router)
|
|
4
|
+
# composes with others. Ported from PHP Lighthouse's Federation, on graphql-ruby:
|
|
5
|
+
#
|
|
6
|
+
# extend schema @link(url: "https://specs.apollo.dev/federation/v2.9", import: ["@key", "@shareable"])
|
|
7
|
+
#
|
|
8
|
+
# type User @key(fields: "id") { id: ID! name: String! }
|
|
9
|
+
#
|
|
10
|
+
# A schema linking the Federation 2 spec is a subgraph; `federation: true`
|
|
11
|
+
# makes one without the link a Federation 1 subgraph. Then, while the schema
|
|
12
|
+
# is built:
|
|
13
|
+
#
|
|
14
|
+
# - the spec's directives it uses are declared for it (Definitions), under the
|
|
15
|
+
# names the link gives them;
|
|
16
|
+
# - the subgraph's types and fields are added, as the spec asks: `scalar _Any`,
|
|
17
|
+
# `type _Service { sdl: String! }`, `Query._service`, and, when some object
|
|
18
|
+
# type has @key, `union _Entity` of them and `Query._entities(representations:)`;
|
|
19
|
+
# - the types @paginate generates are @shareable (PaginationManipulator), since
|
|
20
|
+
# every subgraph that paginates generates the same ones.
|
|
21
|
+
#
|
|
22
|
+
# `Query._service { sdl }` answers the subgraph's SDL (Printer), and
|
|
23
|
+
# `Query._entities` the entities the gateway asks for (Entities).
|
|
24
|
+
module Lighthouse::GraphQL::Federation
|
|
25
|
+
Nodes = ::GraphQL::Language::Nodes
|
|
26
|
+
|
|
27
|
+
# What `federation: true` prefixes to a schema without a link, so the build
|
|
28
|
+
# (and its cache key) knows it is a Federation 1 subgraph.
|
|
29
|
+
V1_MARKER = "# lighthouse: federation v1\n"
|
|
30
|
+
|
|
31
|
+
ENTITIES_FIELD = '_entities'
|
|
32
|
+
SERVICE_FIELD = '_service'
|
|
33
|
+
FIELDS = [ENTITIES_FIELD, SERVICE_FIELD].freeze
|
|
34
|
+
|
|
35
|
+
# The types the spec adds to a subgraph, which its SDL does not show.
|
|
36
|
+
TYPES = %w[_Any _Entity _Service].freeze
|
|
37
|
+
|
|
38
|
+
module_function
|
|
39
|
+
|
|
40
|
+
# The SDL as `get_schema(options: { federation: })` asks for it: marked as a
|
|
41
|
+
# Federation 1 subgraph when it has no link of its own.
|
|
42
|
+
def prepare(sdl, federation)
|
|
43
|
+
return sdl unless federation && Link.from_document(parse(sdl)).nil?
|
|
44
|
+
|
|
45
|
+
"#{V1_MARKER}#{sdl}"
|
|
46
|
+
rescue ::GraphQL::ParseError
|
|
47
|
+
sdl
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
# The schema's Link, for a subgraph; nil for any other schema. `marked`: the
|
|
51
|
+
# schema is a Federation 1 subgraph without a link of its own (V1_MARKER).
|
|
52
|
+
def link_for(document, marked: false)
|
|
53
|
+
Link.from_document(document) || (Link.v1 if marked)
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
# The manipulation phase's last step (SchemaGenerator.manipulate): the
|
|
57
|
+
# subgraph's directives, types and root fields, on the manipulated SDL.
|
|
58
|
+
# `marked`: the SDL as it came, which carries V1_MARKER for `federation: true`.
|
|
59
|
+
def manipulate(sdl, marked = sdl)
|
|
60
|
+
document_ast = Lighthouse::GraphQL::DocumentAST.from_source(sdl)
|
|
61
|
+
link = link_for(document_ast.to_document, marked: marked.to_s.start_with?(V1_MARKER))
|
|
62
|
+
return sdl unless link
|
|
63
|
+
|
|
64
|
+
declare(document_ast, sdl, link)
|
|
65
|
+
add_service(document_ast, link)
|
|
66
|
+
document_ast.to_sdl
|
|
67
|
+
end
|
|
68
|
+
|
|
69
|
+
# The definitions of the spec's directives the SDL uses and does not
|
|
70
|
+
# declare, and of the types they refer to.
|
|
71
|
+
def declare(document_ast, sdl, link)
|
|
72
|
+
used = sdl.scan(/@(\w+)/).flatten.to_set
|
|
73
|
+
used << 'link' if link.v2?
|
|
74
|
+
definitions = Definitions.directives(link).select { |name, _| used.include?(name) && !document_ast.directives.key?(name) }
|
|
75
|
+
definitions.each_value { |definition| document_ast.add(parse(definition).definitions.first) }
|
|
76
|
+
|
|
77
|
+
text = definitions.values.join("\n")
|
|
78
|
+
Definitions.types(link).each do |name, definition|
|
|
79
|
+
document_ast.define_type(name, definition) if text.match?(/\b#{name}\b/)
|
|
80
|
+
end
|
|
81
|
+
end
|
|
82
|
+
|
|
83
|
+
def add_service(document_ast, link)
|
|
84
|
+
document_ast.define_type('_Any', 'scalar _Any')
|
|
85
|
+
document_ast.define_type('_Service', 'type _Service { sdl: String! }')
|
|
86
|
+
query = document_ast.types['Query'] || Nodes::ObjectTypeDefinition.new(name: 'Query', fields: [], interfaces: [], directives: [])
|
|
87
|
+
fields = query.fields.reject { |field| FIELDS.include?(field.name) }
|
|
88
|
+
entities = entity_types(document_ast, link)
|
|
89
|
+
if entities.any?
|
|
90
|
+
document_ast.put_type(parse("union _Entity = #{entities.join(' | ')}").definitions.first)
|
|
91
|
+
fields << root_field("#{ENTITIES_FIELD}(representations: [_Any!]!): [_Entity]!")
|
|
92
|
+
end
|
|
93
|
+
fields << root_field("#{SERVICE_FIELD}: _Service!")
|
|
94
|
+
document_ast.put_type(query.merge(fields: fields))
|
|
95
|
+
end
|
|
96
|
+
|
|
97
|
+
# The object types with @key, which `_entities` may answer.
|
|
98
|
+
def entity_types(document_ast, link)
|
|
99
|
+
key = link.directive('key')
|
|
100
|
+
document_ast.types.values.select do |type|
|
|
101
|
+
type.is_a?(Nodes::ObjectTypeDefinition) && type.directives.any? { |directive| directive.name == key }
|
|
102
|
+
end.map(&:name)
|
|
103
|
+
end
|
|
104
|
+
|
|
105
|
+
def root_field(sdl)
|
|
106
|
+
parse("type Query { #{sdl} }").definitions.first.fields.first
|
|
107
|
+
end
|
|
108
|
+
|
|
109
|
+
def parse(sdl)
|
|
110
|
+
::GraphQL::Language::Parser.parse(sdl)
|
|
111
|
+
end
|
|
112
|
+
|
|
113
|
+
# Is `schema` a subgraph Lighthouse built?
|
|
114
|
+
def subgraph?(schema)
|
|
115
|
+
!!schema.query&.get_field(SERVICE_FIELD)
|
|
116
|
+
end
|
|
117
|
+
|
|
118
|
+
# The Link of a subgraph Lighthouse built (Federation 1 without one).
|
|
119
|
+
def schema_link(schema)
|
|
120
|
+
schema.respond_to?(:federation_link) ? schema.federation_link : Link.v1
|
|
121
|
+
end
|
|
122
|
+
|
|
123
|
+
# After the schema is built from `document`: resolve `_service` and
|
|
124
|
+
# `_entities`, and answer `federation_sdl` as subgraph schemas do.
|
|
125
|
+
def apply(schema, document)
|
|
126
|
+
return unless subgraph?(schema)
|
|
127
|
+
|
|
128
|
+
link = Link.from_document(document) || Link.v1
|
|
129
|
+
schema.define_singleton_method(:federation_link) { link }
|
|
130
|
+
query = schema.query
|
|
131
|
+
query.define_singleton_method("resolve_field_#{SERVICE_FIELD}") do |_obj, _args, ctx|
|
|
132
|
+
{ sdl: Lighthouse::GraphQL::Federation::Printer.print(ctx.schema) }
|
|
133
|
+
end
|
|
134
|
+
if query.get_field(ENTITIES_FIELD)
|
|
135
|
+
query.define_singleton_method("resolve_field_#{ENTITIES_FIELD}") do |_obj, args, ctx|
|
|
136
|
+
Lighthouse::GraphQL::Federation::Entities.resolve(Lighthouse::Support::Naming.fetch_arg(args, 'representations'), ctx)
|
|
137
|
+
end
|
|
138
|
+
end
|
|
139
|
+
FIELDS.each do |name|
|
|
140
|
+
field = query.get_field(name)
|
|
141
|
+
Lighthouse::GraphQL::GeneratedResolvers.route_runtime_resolver(query, field) if field
|
|
142
|
+
end
|
|
143
|
+
schema.define_singleton_method(:federation_sdl) { |**| Lighthouse::GraphQL::Federation::Printer.print(self) }
|
|
144
|
+
end
|
|
145
|
+
end
|
|
@@ -5,10 +5,16 @@ module Lighthouse::GraphQL
|
|
|
5
5
|
# `@paginate(maxCount: null)` — to the directive as a NullValue node instead
|
|
6
6
|
# of nil, and building the schema then fails to coerce it. Its own
|
|
7
7
|
# build_default_value already maps NullValue to nil; do the same here.
|
|
8
|
+
# An object literal given to a scalar argument (the Federation link's
|
|
9
|
+
# `import: [{ name: "@key", as: "@id" }]`, a `link__Import`) is read as an
|
|
10
|
+
# input object, whose arguments the scalar has none of: it is its Hash.
|
|
8
11
|
module NullDirectiveArguments
|
|
9
12
|
# graphql 2.6.14 added a third parameter (the type resolver): pass on whatever it takes.
|
|
10
13
|
def args_to_kwargs(arg_owner, node, *rest)
|
|
11
|
-
node.is_a?(::GraphQL::Language::Nodes::NullValue)
|
|
14
|
+
return if node.is_a?(::GraphQL::Language::Nodes::NullValue)
|
|
15
|
+
return node.to_h if node.is_a?(::GraphQL::Language::Nodes::InputObject) && !arg_owner.respond_to?(:get_argument)
|
|
16
|
+
|
|
17
|
+
super
|
|
12
18
|
end
|
|
13
19
|
end
|
|
14
20
|
|
|
@@ -21,7 +21,6 @@ require_relative 'namespaces'
|
|
|
21
21
|
config
|
|
22
22
|
errors
|
|
23
23
|
graphql_ruby_patches
|
|
24
|
-
reference_resolver
|
|
25
24
|
support/naming
|
|
26
25
|
support/count
|
|
27
26
|
support/directive_args
|
|
@@ -49,7 +48,14 @@ require_relative 'namespaces'
|
|
|
49
48
|
document_ast
|
|
50
49
|
generated_resolvers
|
|
51
50
|
default_resolve_dispatcher
|
|
52
|
-
|
|
51
|
+
federation
|
|
52
|
+
federation/link
|
|
53
|
+
federation/definitions
|
|
54
|
+
federation/any
|
|
55
|
+
federation/entities
|
|
56
|
+
federation/printer
|
|
57
|
+
federation/callback_subscriptions
|
|
58
|
+
federation/apollo_federation_check
|
|
53
59
|
field_resolver_stack
|
|
54
60
|
events
|
|
55
61
|
events/lifecycle
|
|
@@ -82,6 +88,7 @@ require_relative 'namespaces'
|
|
|
82
88
|
incremental/body
|
|
83
89
|
incremental/run
|
|
84
90
|
defer
|
|
91
|
+
event_stream
|
|
85
92
|
operations
|
|
86
93
|
controller
|
|
87
94
|
testing
|
|
@@ -116,6 +123,7 @@ require_relative 'namespaces'
|
|
|
116
123
|
search/auto
|
|
117
124
|
search/enhancer
|
|
118
125
|
search/field_check
|
|
126
|
+
search/together_check
|
|
119
127
|
relation_batch
|
|
120
128
|
relation_counts
|
|
121
129
|
nested_mutations
|
|
@@ -10,7 +10,8 @@
|
|
|
10
10
|
# Lighthouse's directive.
|
|
11
11
|
# 2. Its type class, Types::<Name>Type including Lighthouse::GraphQL::Type: a
|
|
12
12
|
# GraphQL::Schema::Scalar, as graphql-ruby's `rails g graphql:scalar` writes it.
|
|
13
|
-
# 3. Lighthouse's own, for `scalar Upload` (Lighthouse::GraphQL::Upload)
|
|
13
|
+
# 3. Lighthouse's own, for `scalar Upload` (Lighthouse::GraphQL::Upload) and a
|
|
14
|
+
# subgraph's `scalar _Any` (Lighthouse::GraphQL::Federation::Any).
|
|
14
15
|
#
|
|
15
16
|
# A scalar with none passes its values through unchanged. A class raising
|
|
16
17
|
# GraphQL::CoercionError from coerce_input (or returning nil for a value) makes
|
|
@@ -42,7 +43,7 @@ module Lighthouse::GraphQL::ScalarCoercion
|
|
|
42
43
|
end
|
|
43
44
|
|
|
44
45
|
# Scalars Lighthouse brings a class for, when nothing else names one.
|
|
45
|
-
BUILT_IN = { 'Upload' => 'Lighthouse::GraphQL::Upload' }.freeze
|
|
46
|
+
BUILT_IN = { 'Upload' => 'Lighthouse::GraphQL::Upload', '_Any' => 'Lighthouse::GraphQL::Federation::Any' }.freeze
|
|
46
47
|
|
|
47
48
|
def class_for(scalar_type, base = nil)
|
|
48
49
|
directive_class(scalar_type) || type_class(scalar_type, base) || BUILT_IN[scalar_type.graphql_name]&.constantize
|
|
@@ -39,6 +39,10 @@ module Lighthouse::GraphQL::SchemaGenerator
|
|
|
39
39
|
module_function
|
|
40
40
|
|
|
41
41
|
def build(sdl_string, **opts)
|
|
42
|
+
# `federation: true` makes a schema without a Federation 2 @link a Federation 1 subgraph.
|
|
43
|
+
sdl_string = Lighthouse::GraphQL::Federation.prepare(sdl_string, opts.delete(:federation))
|
|
44
|
+
# The apollo-federation gem's mixins would federate nothing here (the SDL does).
|
|
45
|
+
Lighthouse::GraphQL::Federation::ApolloFederationCheck.check_types!(opts[:base_types] || {})
|
|
42
46
|
# Decided on the app's SDL: once definitions are injected, every schema mentions it.
|
|
43
47
|
uses_cache_control = sdl_string.to_s.gsub(/directive\s+@cacheControl/, '').include?('@cacheControl')
|
|
44
48
|
# The manipulated document — from the schema cache when it is enabled
|
|
@@ -51,10 +55,12 @@ module Lighthouse::GraphQL::SchemaGenerator
|
|
|
51
55
|
schema = Lighthouse::GraphQL::RootTypes.build(document, bases[:roots]) { from_document(document, impl, opts) }
|
|
52
56
|
|
|
53
57
|
Lighthouse::GraphQL::ServerDirectives.hide!(schema)
|
|
54
|
-
delegate_to_federation_sdl(schema)
|
|
55
58
|
Lighthouse::GraphQL::GeneratedResolvers.install_dispatcher(schema)
|
|
56
59
|
attach_schema(impl, schema)
|
|
57
60
|
post_process(schema, impl, bases: bases, **opts)
|
|
61
|
+
# A subgraph's `_service` and `_entities`; the app's schema class may not take the gem's instead.
|
|
62
|
+
Lighthouse::GraphQL::Federation.apply(schema, document)
|
|
63
|
+
Lighthouse::GraphQL::Federation::ApolloFederationCheck.guard!(schema)
|
|
58
64
|
schema.trace_with(Lighthouse::Events::ExecutionTrace)
|
|
59
65
|
|
|
60
66
|
# @cacheControl hints become the response's Cache-Control header,
|
|
@@ -72,8 +78,8 @@ module Lighthouse::GraphQL::SchemaGenerator
|
|
|
72
78
|
defined?(::SchemaImplementation) ? ::SchemaImplementation.new : FallbackImplementation.new
|
|
73
79
|
end
|
|
74
80
|
|
|
75
|
-
# Build using the provided base class so
|
|
76
|
-
#
|
|
81
|
+
# Build using the provided base class so custom schema behavior is
|
|
82
|
+
# preserved through class hierarchy.
|
|
77
83
|
# The document is handed over through `parser:`, graphql-ruby's public
|
|
78
84
|
# hook, so it is not parsed again. Takes the options it uses out of `opts`;
|
|
79
85
|
# the rest are post_process's.
|
|
@@ -86,15 +92,6 @@ module Lighthouse::GraphQL::SchemaGenerator
|
|
|
86
92
|
base_types: base_types, using: using)
|
|
87
93
|
end
|
|
88
94
|
|
|
89
|
-
# Make to_definition delegate to federation_sdl when available.
|
|
90
|
-
def delegate_to_federation_sdl(schema)
|
|
91
|
-
return unless schema.respond_to?(:federation_sdl)
|
|
92
|
-
|
|
93
|
-
schema.define_singleton_method(:to_definition) do
|
|
94
|
-
federation_sdl
|
|
95
|
-
end
|
|
96
|
-
end
|
|
97
|
-
|
|
98
95
|
# Give the implementation object access to the built schema so its
|
|
99
96
|
# runtime dispatcher can look up generated singleton resolvers.
|
|
100
97
|
def attach_schema(impl, schema)
|
|
@@ -126,22 +123,22 @@ module Lighthouse::GraphQL::SchemaGenerator
|
|
|
126
123
|
# The SDL as the schema is built from it: directive definitions injected
|
|
127
124
|
# (so apps don't hand-declare Lighthouse's directives), then the
|
|
128
125
|
# manipulation phase (ASTBuilder: type extensions merged, pagination types,
|
|
129
|
-
# whereConditions / orderBy inputs, ...)
|
|
126
|
+
# whereConditions / orderBy inputs, ...), and last, for a subgraph, what
|
|
127
|
+
# Federation adds.
|
|
130
128
|
def manipulate(sdl)
|
|
131
|
-
Lighthouse::GraphQL::ASTBuilder.build(inject_directive_definitions(sdl))
|
|
129
|
+
Lighthouse::GraphQL::Federation.manipulate(Lighthouse::GraphQL::ASTBuilder.build(inject_directive_definitions(sdl)), sdl)
|
|
132
130
|
end
|
|
133
131
|
|
|
134
132
|
# `bases`: { root kind => the base class its fields' classes inherit from } (ResolverBases).
|
|
135
133
|
def post_process(schema, _impl, bases: {}, **_opts)
|
|
136
134
|
root_kinds = root_kinds(schema)
|
|
135
|
+
# A field of a union or an interface with @search searches its types' models together.
|
|
136
|
+
Lighthouse::Search::TogetherCheck.check_schema!(schema, root_kinds)
|
|
137
137
|
|
|
138
138
|
# Inspect types and fields for directives and attach generated resolvers
|
|
139
139
|
schema.types.each_value do |type|
|
|
140
140
|
next unless type.kind.object?
|
|
141
141
|
|
|
142
|
-
# Apply object-level federation directives to generated types so the
|
|
143
|
-
# Apollo Federation helper can detect entities and add _entities.
|
|
144
|
-
Lighthouse::GraphQL::FederationDirectives.apply(type)
|
|
145
142
|
root_kind = root_kinds[type.graphql_name]
|
|
146
143
|
type_class = Lighthouse::GraphQL::DirectiveResolvers::TypeMethodResolver.type_class(type.graphql_name, bases, root_kind)
|
|
147
144
|
# A field a code-first base declares (RootTypes) resolves as graphql-ruby resolves it.
|
|
@@ -40,7 +40,9 @@ module Lighthouse::GraphQL::SchemaPrinter
|
|
|
40
40
|
end
|
|
41
41
|
|
|
42
42
|
def federation(schema)
|
|
43
|
-
|
|
43
|
+
unless schema.respond_to?(:federation_sdl)
|
|
44
|
+
raise ArgumentError, 'The federation format needs a subgraph: a schema that @links the Federation spec, or federation: true.'
|
|
45
|
+
end
|
|
44
46
|
|
|
45
47
|
"#{schema.federation_sdl.chomp}\n"
|
|
46
48
|
end
|
|
@@ -18,4 +18,9 @@ class Lighthouse::Search::Auto
|
|
|
18
18
|
end
|
|
19
19
|
|
|
20
20
|
def searchable?(model, index = nil) = !adapter_for(model, index).nil?
|
|
21
|
+
|
|
22
|
+
# The first adapter that searches every one of `models` together, or nil.
|
|
23
|
+
def adapter_together(models)
|
|
24
|
+
@adapters.find { |adapter| adapter.respond_to?(:together_error) && models.all? { |model| adapter.searchable_together?(model) } }
|
|
25
|
+
end
|
|
21
26
|
end
|
|
@@ -11,6 +11,8 @@
|
|
|
11
11
|
# write to a Search::Query (Contracts::SearchBuilder), and the answer is a
|
|
12
12
|
# Search::Result instead of a relation. Any other argument directive the client
|
|
13
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.
|
|
14
16
|
class Lighthouse::Search::Enhancer
|
|
15
17
|
# An argument directive of the field: its AST nodes, the builder class (or :builder), the client's value.
|
|
16
18
|
Argument = Struct.new(:node, :directive, :builder, :value, :present)
|
|
@@ -28,18 +30,21 @@ class Lighthouse::Search::Enhancer
|
|
|
28
30
|
# nil, a relation only; :model, a Search::Result for the model's own relation it
|
|
29
31
|
# built (with the `scopes:` Search::FieldCheck refuses for an engine), searched
|
|
30
32
|
# whole without a check; :given, one for a relation the app's code gave, checked.
|
|
31
|
-
# `field`: "Type.field", for the client's errors.
|
|
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).
|
|
32
35
|
def initialize(arguments, relation, search:, context:, field: 'the field')
|
|
33
36
|
@arguments = arguments
|
|
34
37
|
@relation = relation
|
|
35
38
|
@search = search
|
|
36
39
|
@context = context
|
|
37
40
|
@field = field
|
|
41
|
+
@models = search if search.is_a?(Array)
|
|
38
42
|
end
|
|
39
43
|
|
|
40
44
|
# The relation, searched; or a Search::Result. The relation itself if the client sent no search.
|
|
41
45
|
def enhance
|
|
42
46
|
search = the_search
|
|
47
|
+
return together(search) if @models
|
|
43
48
|
return @relation unless search
|
|
44
49
|
|
|
45
50
|
model = @relation.klass
|
|
@@ -73,6 +78,25 @@ class Lighthouse::Search::Enhancer
|
|
|
73
78
|
|
|
74
79
|
searchable!(adapter, query.model)
|
|
75
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)
|
|
76
100
|
reject_incompatible
|
|
77
101
|
query = @arguments.reject { |argument| search?(argument) }.reduce(query) { |narrowed, argument| narrow(narrowed, argument) }
|
|
78
102
|
Lighthouse::Search::Result.checked(adapter, query, @field) || Lighthouse::Search::Result.new(adapter, query, context: @context, field: @field)
|
|
@@ -18,6 +18,8 @@
|
|
|
18
18
|
# user must not see. A relational adapter (pg_search) applies them.
|
|
19
19
|
# - An @canQuery, which runs the search to check the records it finds, searches
|
|
20
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.
|
|
21
23
|
class Lighthouse::Search::FieldCheck
|
|
22
24
|
HINT = 'it needs searchkick or pg_search, or an adapter in Config.search'
|
|
23
25
|
def self.check!(...) = new(...).check!
|
|
@@ -33,7 +35,7 @@ class Lighthouse::Search::FieldCheck
|
|
|
33
35
|
|
|
34
36
|
def check!
|
|
35
37
|
searches = Lighthouse::Search.directives(@field_def)
|
|
36
|
-
return if searches.empty?
|
|
38
|
+
return if searches.empty? || Lighthouse::Search.together(@field_def)
|
|
37
39
|
|
|
38
40
|
searches.each { |path, directive| within_string!(path, directive) }
|
|
39
41
|
applied!(searches.first.first)
|
|
@@ -13,6 +13,14 @@
|
|
|
13
13
|
# The model's `search_results` scope, if it has one, narrows the search too
|
|
14
14
|
# (Lighthouse::Search).
|
|
15
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
|
+
#
|
|
16
24
|
# The gem is not a dependency of Lighthouse: the adapter is used when the
|
|
17
25
|
# model includes PgSearch::Model.
|
|
18
26
|
class Lighthouse::Search::PgSearch
|
|
@@ -42,8 +50,70 @@ class Lighthouse::Search::PgSearch
|
|
|
42
50
|
orders.empty? ? searched : searched.reorder(*orders)
|
|
43
51
|
end
|
|
44
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
|
+
|
|
45
76
|
private
|
|
46
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
|
+
|
|
47
117
|
def scope_name(index) = (index || DEFAULT_SCOPE).to_sym
|
|
48
118
|
|
|
49
119
|
# Is the model's class method `name` a pg_search scope? pg_search keeps no list
|