lighthouse-graphql 0.9.1 → 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 +64 -1
- data/README.md +14 -8
- data/docs/configuration.md +4 -10
- data/docs/federation.md +250 -75
- 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/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 +9 -2
- data/lib/lighthouse/scalar_coercion.rb +3 -2
- data/lib/lighthouse/schema_generator.rb +12 -17
- data/lib/lighthouse/schema_printer.rb +3 -1
- 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.rbs +6 -0
- metadata +11 -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
|
|
@@ -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,9 +123,10 @@ 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).
|
|
@@ -141,9 +139,6 @@ module Lighthouse::GraphQL::SchemaGenerator
|
|
|
141
139
|
schema.types.each_value do |type|
|
|
142
140
|
next unless type.kind.object?
|
|
143
141
|
|
|
144
|
-
# Apply object-level federation directives to generated types so the
|
|
145
|
-
# Apollo Federation helper can detect entities and add _entities.
|
|
146
|
-
Lighthouse::GraphQL::FederationDirectives.apply(type)
|
|
147
142
|
root_kind = root_kinds[type.graphql_name]
|
|
148
143
|
type_class = Lighthouse::GraphQL::DirectiveResolvers::TypeMethodResolver.type_class(type.graphql_name, bases, root_kind)
|
|
149
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
|
|
@@ -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
|
-
|
|
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
|
data/sig/lighthouse/graphql.rbs
CHANGED
|
@@ -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
|
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.
|
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.
|
|
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/
|
|
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
|
|
@@ -277,6 +284,7 @@ files:
|
|
|
277
284
|
- sig/lighthouse/controller.rbs
|
|
278
285
|
- sig/lighthouse/directives.rbs
|
|
279
286
|
- sig/lighthouse/events.rbs
|
|
287
|
+
- sig/lighthouse/federation.rbs
|
|
280
288
|
- sig/lighthouse/graphql.rbs
|
|
281
289
|
- sig/lighthouse/search.rbs
|
|
282
290
|
homepage: https://github.com/Vaz-Innovation/lighthouse-graphql
|