lighthouse-graphql 0.8.0 → 0.8.2
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 +45 -1
- data/docs/authorization.md +13 -2
- data/docs/directives.md +4 -1
- data/docs/events.md +17 -3
- data/docs/relay.md +27 -0
- data/lib/lighthouse/config.rb +1 -3
- data/lib/lighthouse/directive_resolvers/argument_transforms_resolver.rb +14 -4
- data/lib/lighthouse/directive_resolvers/auth_resolver.rb +1 -1
- data/lib/lighthouse/directive_resolvers/global_id_resolver.rb +6 -0
- data/lib/lighthouse/directive_resolvers/global_id_types.rb +84 -0
- data/lib/lighthouse/directive_resolvers/guard_resolver.rb +4 -3
- data/lib/lighthouse/errors.rb +17 -3
- data/lib/lighthouse/events/execution_trace.rb +2 -2
- data/lib/lighthouse/events/lifecycle.rb +20 -6
- data/lib/lighthouse/global_id.rb +28 -0
- data/lib/lighthouse/graphql/version.rb +1 -1
- data/lib/lighthouse/rb_lighthouse.rb +1 -0
- data/sig/lighthouse/controller.rbs +6 -5
- data/sig/lighthouse/directives.rbs +31 -25
- data/sig/lighthouse/events.rbs +52 -27
- data/sig/lighthouse/graphql.rbs +29 -24
- data/sig/lighthouse.rbs +200 -22
- metadata +2 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: b2ab527dcb3edd5db81b2cbd6c9bbec9c3e57a563ef226ec45ed4f44aab44f40
|
|
4
|
+
data.tar.gz: e3965b3baa2fc998bce3ecbfafeaf7de724d5715957f35384e63cdbb484df5dd
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 436960e077273eef766bcd648ba709d16eaf263e12bb76c5447c730353822c8e3d334bf214bc252bcc01af3a23234b81987ef29d8e103c5f1f85d6c28abf1ea6
|
|
7
|
+
data.tar.gz: 8008508abc330c3fffb6725655bd445af480812b17d953db62fbb6f0aefa61700d63b46fc88171cfd74ad11536ee5e93bcfd598370453f0fd18ce012b14baff2
|
data/CHANGELOG.md
CHANGED
|
@@ -6,6 +6,48 @@ project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
|
|
6
6
|
|
|
7
7
|
## [Unreleased]
|
|
8
8
|
|
|
9
|
+
## [0.8.2] - 2026-10-30
|
|
10
|
+
|
|
11
|
+
### Fixed
|
|
12
|
+
|
|
13
|
+
- **`@globalId(decode: ID)` checks the type of the id** (#6). It kept the id and dropped the
|
|
14
|
+
type, so another type's global id found the record with the same number:
|
|
15
|
+
`user(id: <a Post's id>)` answered user 1, and `@delete` / `@forceDelete` could act on a
|
|
16
|
+
record the client did not name. An argument that filters the field's model by its key
|
|
17
|
+
(`@whereKey`, `@eq`/`@in` on the primary key, or `id`), on the field or in an input it
|
|
18
|
+
takes, now fails with `"…" is not a valid global id for User.` given another type's id;
|
|
19
|
+
a subclass (STI) or a type whose `@model` is the field's model passes. The new
|
|
20
|
+
`@globalId(type: "User")` names the type anywhere else. PHP Lighthouse does not check.
|
|
21
|
+
|
|
22
|
+
## [0.8.1] - 2026-10-30
|
|
23
|
+
|
|
24
|
+
### Changed
|
|
25
|
+
|
|
26
|
+
- **The RBS signatures are typed.** What Lighthouse takes from graphql-ruby, Active Record
|
|
27
|
+
and Rails, which ship no RBS, was `untyped`; it is now declared as interfaces of the methods
|
|
28
|
+
Lighthouse uses (`_Schema`, `_Result`, `_Context`, `_Query`, `_Relation`, `_DirectiveNode`,
|
|
29
|
+
`_Request`, ...), so Steep checks code that calls Lighthouse and `rake rbs:test` checks the
|
|
30
|
+
suite's real values against them. Results are `Hash[String, json]`, directive arguments
|
|
31
|
+
`literal`, `Events.listen` has an overload per event class, with the block's event and
|
|
32
|
+
answer typed. `untyped` is left for the app's own values (a user, a client's argument
|
|
33
|
+
value): 169 became 76. Typing found `BuilderApplier.apply` called without a context, which
|
|
34
|
+
the signature now allows.
|
|
35
|
+
|
|
36
|
+
### Removed
|
|
37
|
+
|
|
38
|
+
- `Config.directive_map`, a setting nothing read.
|
|
39
|
+
|
|
40
|
+
### Added
|
|
41
|
+
|
|
42
|
+
- **`Unauthenticated.` and `This action is unauthorized.` carry an `extensions.code`** (#5):
|
|
43
|
+
`UNAUTHENTICATED` from `@guard` and `@auth` (the new `Lighthouse::AuthenticationError`), and
|
|
44
|
+
`FORBIDDEN` from the `@can*` directives (`Lighthouse::AuthorizationError`), as Apollo names
|
|
45
|
+
them, so a client can react without matching the message. PHP Lighthouse's have no code.
|
|
46
|
+
- **`EndExecution` says which operation ended** (#4): `operation_name`, `context`, and the
|
|
47
|
+
`GraphQL::Query` as `graphql_query`, which `StartExecution` carries too. In a batch every
|
|
48
|
+
`StartExecution` comes before any `EndExecution`, so a listener could not tell the results
|
|
49
|
+
apart.
|
|
50
|
+
|
|
9
51
|
## [0.8.0] - 2026-10-29
|
|
10
52
|
|
|
11
53
|
### Added
|
|
@@ -824,7 +866,9 @@ had been running inside a private monorepo as a path gem before being extracted.
|
|
|
824
866
|
- Config-driven extension seams: `Lighthouse.configure`, `resolver_namespaces`,
|
|
825
867
|
`on_error`, and `DirectiveRegistry.register` for app-defined directives.
|
|
826
868
|
|
|
827
|
-
[Unreleased]: https://github.com/Vaz-Innovation/lighthouse-graphql/compare/v0.8.
|
|
869
|
+
[Unreleased]: https://github.com/Vaz-Innovation/lighthouse-graphql/compare/v0.8.2...HEAD
|
|
870
|
+
[0.8.2]: https://github.com/Vaz-Innovation/lighthouse-graphql/compare/v0.8.1...v0.8.2
|
|
871
|
+
[0.8.1]: https://github.com/Vaz-Innovation/lighthouse-graphql/compare/v0.8.0...v0.8.1
|
|
828
872
|
[0.8.0]: https://github.com/Vaz-Innovation/lighthouse-graphql/compare/v0.7.3...v0.8.0
|
|
829
873
|
[0.7.3]: https://github.com/Vaz-Innovation/lighthouse-graphql/compare/v0.7.2...v0.7.3
|
|
830
874
|
[0.7.2]: https://github.com/Vaz-Innovation/lighthouse-graphql/compare/v0.7.1...v0.7.2
|
data/docs/authorization.md
CHANGED
|
@@ -33,8 +33,9 @@ three-argument authorizer keeps working for checks without them.
|
|
|
33
33
|
|
|
34
34
|
## `@guard` — require authentication
|
|
35
35
|
|
|
36
|
-
Raises `Unauthenticated.` when there is no user
|
|
37
|
-
|
|
36
|
+
Raises `Unauthenticated.` when there is no user, with `extensions.code`
|
|
37
|
+
`UNAUTHENTICATED`, Apollo's code, so a client can send the user to sign in without
|
|
38
|
+
matching the message. It runs before everything else on the field.
|
|
38
39
|
|
|
39
40
|
```graphql
|
|
40
41
|
type Query {
|
|
@@ -134,6 +135,16 @@ post(id: ID!): Post @canFind(ability: "show", find: "id", action: EXCEPTION_NOT_
|
|
|
134
135
|
`action` applies to the directive's own checks only. Errors from the resolver
|
|
135
136
|
itself are untouched.
|
|
136
137
|
|
|
138
|
+
`This action is unauthorized.` carries `extensions.code` `FORBIDDEN`, as Apollo
|
|
139
|
+
names it (`@guard`'s `Unauthenticated.` carries `UNAUTHENTICATED`):
|
|
140
|
+
|
|
141
|
+
```json
|
|
142
|
+
{ "message": "This action is unauthorized.", "path": ["post"], "extensions": { "code": "FORBIDDEN" } }
|
|
143
|
+
```
|
|
144
|
+
|
|
145
|
+
PHP Lighthouse's errors have no code. Raise `Lighthouse::AuthenticationError` and
|
|
146
|
+
`Lighthouse::AuthorizationError` from your own resolvers to answer the same.
|
|
147
|
+
|
|
137
148
|
### Who may use an argument
|
|
138
149
|
|
|
139
150
|
Some arguments are for some users only: `trashed` for admins, a filter on drafts
|
data/docs/directives.md
CHANGED
|
@@ -286,7 +286,10 @@ type User {
|
|
|
286
286
|
### `@globalId` and `@node`
|
|
287
287
|
|
|
288
288
|
`@globalId` encodes a field's value as a Relay global id (base64 of `Type:id`), and
|
|
289
|
-
decodes an argument or input field (`decode: ARRAY | TYPE | ID`).
|
|
289
|
+
decodes an argument or input field (`decode: ARRAY | TYPE | ID`). A decoded id must be
|
|
290
|
+
of the type it is for: `type:` names it, and with `decode: ID` an argument filtering
|
|
291
|
+
the field's model by its key is checked against that model, so another type's id
|
|
292
|
+
fails the field ([relay.md](relay.md#the-type-of-a-decoded-id)). `@node` on a type
|
|
290
293
|
makes it implement `Node` and adds `Query.node(id:)`, which finds the type's model
|
|
291
294
|
record, or calls `@node(resolver:)`. See [relay.md](relay.md).
|
|
292
295
|
|
data/docs/events.md
CHANGED
|
@@ -51,10 +51,10 @@ created at (UTC).
|
|
|
51
51
|
| --- | --- | --- |
|
|
52
52
|
| `StartRequest` | The controller action begins | `request`, `moment` |
|
|
53
53
|
| `StartOperationOrOperations` | The operation(s) were read, before they run | `operation_or_operations` |
|
|
54
|
-
| `StartExecution` | A query is about to run (once per operation of a batch) | `schema`, `query`, `variables`, `operation_name`, `context`, `moment` |
|
|
54
|
+
| `StartExecution` | A query is about to run (once per operation of a batch) | `schema`, `query`, `variables`, `operation_name`, `context`, `graphql_query`, `moment` |
|
|
55
55
|
| `BuildExtensionsResponse` | A query ran | `result`, `query_complexity` |
|
|
56
56
|
| `ManipulateResult` | Before the result is returned | `result` (+ writer) |
|
|
57
|
-
| `EndExecution` | The result is final | `result`, `moment` |
|
|
57
|
+
| `EndExecution` | The result is final | `result`, `operation_name`, `context`, `graphql_query`, `moment` |
|
|
58
58
|
| `EndOperationOrOperations` | The operation(s) ran | `result_or_results` |
|
|
59
59
|
| `EndRequest` | The response is rendered | `response`, `moment` |
|
|
60
60
|
|
|
@@ -95,7 +95,21 @@ it builds, so they fire however the query was started: the controller,
|
|
|
95
95
|
With no listener, and no `ActiveSupport::Notifications` subscriber, they cost
|
|
96
96
|
one lookup per request.
|
|
97
97
|
|
|
98
|
-
`StartExecution#query` is the parsed `GraphQL::Language::Nodes::Document
|
|
98
|
+
`StartExecution#query` is the parsed `GraphQL::Language::Nodes::Document`, and
|
|
99
|
+
`operation_name` the one the client gave. Both events carry the `GraphQL::Query` as
|
|
100
|
+
`graphql_query`, and `EndExecution` its `operation_name` (the operation that ran,
|
|
101
|
+
`nil` for an anonymous one) and `context`. In a batch, graphql-ruby runs the
|
|
102
|
+
operations together, so every `StartExecution` comes before any `EndExecution`:
|
|
103
|
+
read what you need from `EndExecution`, or pair the two by `graphql_query`, not by
|
|
104
|
+
order. PHP Lighthouse's `EndExecution` has the result only, as it runs a batch's
|
|
105
|
+
operations one after the other.
|
|
106
|
+
|
|
107
|
+
```ruby
|
|
108
|
+
Lighthouse::Events.listen(Lighthouse::Events::EndExecution) do |event|
|
|
109
|
+
errors = event.result['errors']
|
|
110
|
+
Rails.logger.warn("GraphQL #{event.operation_name || 'an anonymous operation'} answered with errors") if errors.present?
|
|
111
|
+
end
|
|
112
|
+
```
|
|
99
113
|
|
|
100
114
|
**`BuildExtensionsResponse`**: return an `ExtensionsResponse` (or an Array of
|
|
101
115
|
them) to add to the result's `extensions`:
|
data/docs/relay.md
CHANGED
|
@@ -47,6 +47,33 @@ type Mutation {
|
|
|
47
47
|
Lists of ids decode item by item. A value that is not a global id makes the field
|
|
48
48
|
fail with `"…" is not a valid global id.`
|
|
49
49
|
|
|
50
|
+
### The type of a decoded id
|
|
51
|
+
|
|
52
|
+
Relay ids are unique across types, so a decoded id is checked to be of the type it is
|
|
53
|
+
for: another type's id makes the field fail with `"…" is not a valid global id for
|
|
54
|
+
User.`, where it would find the record that happens to have the same number (a
|
|
55
|
+
`Post`'s id finding user 1, or `@delete` removing it). PHP Lighthouse does not check.
|
|
56
|
+
|
|
57
|
+
- **Inferred** with `decode: ID`, when the argument filters the field's model by its
|
|
58
|
+
key: `@whereKey`, an `@eq` or `@in` on the primary key, or an argument named after
|
|
59
|
+
it (`id`). That is an argument of the field, or a field of an input one of its
|
|
60
|
+
arguments takes (`user(by: UserBy!)`), unless that argument is an association of
|
|
61
|
+
the model. The field's model is its `model:`, else its return type's.
|
|
62
|
+
- **Explicit** with `type:`, a GraphQL type or model name, for any other argument and
|
|
63
|
+
any `decode:`:
|
|
64
|
+
|
|
65
|
+
```graphql
|
|
66
|
+
type Query {
|
|
67
|
+
user(id: ID! @globalId(decode: ID) @eq): User @find # a User's id, inferred
|
|
68
|
+
posts(authorId: ID! @globalId(decode: ID, type: "User") @eq(key: "author_id")): [Post!]! @all
|
|
69
|
+
}
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
An id of a subclass (STI) passes, and so does the id of a type whose
|
|
73
|
+
[`@model`](directives.md#model) is the field's model. `authorId` above, without
|
|
74
|
+
`type:`, is not checked: it holds another model's id, as the inputs of nested
|
|
75
|
+
mutations do.
|
|
76
|
+
|
|
50
77
|
## Nodes
|
|
51
78
|
|
|
52
79
|
`@node` registers a type for refetching:
|
data/lib/lighthouse/config.rb
CHANGED
|
@@ -26,7 +26,7 @@ module Lighthouse
|
|
|
26
26
|
SETTINGS = %i[
|
|
27
27
|
schema_path ide_helper resolver_namespaces namespaces transactional_mutations pagination_default_count pagination_max_count
|
|
28
28
|
schema_cache schema_cache_path watch_sdl persisted_queries persisted_queries_store persisted_queries_ttl
|
|
29
|
-
persisted_queries_manifest persisted_queries_only on_error
|
|
29
|
+
persisted_queries_manifest persisted_queries_only on_error authenticated_user policy_user authorizer
|
|
30
30
|
feature_checker feature_actor
|
|
31
31
|
convert_empty_strings_to_null
|
|
32
32
|
soft_deletes
|
|
@@ -78,8 +78,6 @@ module Lighthouse
|
|
|
78
78
|
File.exist?(::Rails.root.join('graphql.config.yml'))
|
|
79
79
|
end
|
|
80
80
|
|
|
81
|
-
def directive_map = @directive_map ||= {}
|
|
82
|
-
|
|
83
81
|
# Namespaces tried (in order) when resolving a class name from a directive
|
|
84
82
|
# argument, e.g. `@field(resolver: "ContactPerson")` or `@all(model: "X")`.
|
|
85
83
|
# Keeping this in config means the library holds no app-specific namespace
|
|
@@ -160,8 +160,13 @@ class Lighthouse::GraphQL::DirectiveResolvers::ArgumentTransformsResolver < Ligh
|
|
|
160
160
|
send(TRANSFORMS.fetch(directive.name), directive, argument, value, ctx)
|
|
161
161
|
end
|
|
162
162
|
|
|
163
|
-
def decode_global_id(directive,
|
|
164
|
-
|
|
163
|
+
def decode_global_id(directive, argument, value, ctx)
|
|
164
|
+
mode = Lighthouse::Support::DirectiveArgs.literal(directive, 'decode') || 'ARRAY'
|
|
165
|
+
decode(value, mode, ctx, global_id_types.model(directive, argument, mode, ctx))
|
|
166
|
+
end
|
|
167
|
+
|
|
168
|
+
def global_id_types
|
|
169
|
+
@global_id_types ||= Lighthouse::GraphQL::DirectiveResolvers::GlobalIdTypes.new(self, @field_def)
|
|
165
170
|
end
|
|
166
171
|
|
|
167
172
|
def trim_value(_directive, _argument, value, _ctx) = trim(value)
|
|
@@ -175,10 +180,15 @@ class Lighthouse::GraphQL::DirectiveResolvers::ArgumentTransformsResolver < Ligh
|
|
|
175
180
|
Lighthouse::GraphQL::UploadStore.call(value, argument: argument.graphql_name, **options)
|
|
176
181
|
end
|
|
177
182
|
|
|
178
|
-
|
|
179
|
-
|
|
183
|
+
# `model`: the class the id must be of, if any (GlobalIdTypes).
|
|
184
|
+
def decode(value, mode, ctx = nil, model = nil)
|
|
185
|
+
return value.map { |item| decode(item, mode, ctx, model) } if value.is_a?(Array)
|
|
180
186
|
|
|
181
187
|
type, id = Lighthouse::GraphQL::GlobalId.decode!(value, ctx)
|
|
188
|
+
if model && !Lighthouse::GraphQL::GlobalId.of_model?(value, type, model, ctx)
|
|
189
|
+
raise ::GraphQL::ExecutionError, "#{value.inspect} is not a valid global id for #{model.name}."
|
|
190
|
+
end
|
|
191
|
+
|
|
182
192
|
{ 'TYPE' => type, 'ID' => id }.fetch(mode.to_s, [type, id])
|
|
183
193
|
end
|
|
184
194
|
|
|
@@ -18,7 +18,7 @@ class Lighthouse::GraphQL::DirectiveResolvers::AuthDirectiveResolver < Lighthous
|
|
|
18
18
|
|
|
19
19
|
install(target_type_class, lambda do |obj, arguments, ctx|
|
|
20
20
|
user = ctx && (ctx[:current_user] || ctx['current_user'])
|
|
21
|
-
raise
|
|
21
|
+
raise Lighthouse::AuthenticationError unless user
|
|
22
22
|
|
|
23
23
|
inner.call(obj, arguments, ctx)
|
|
24
24
|
end)
|
|
@@ -6,6 +6,10 @@ require_relative 'base'
|
|
|
6
6
|
# encodes the value with the type's name (`id: ID! @globalId` answers
|
|
7
7
|
# "VXNlcjox" for User 1); on an argument or input field it decodes what the
|
|
8
8
|
# client sent (ArgumentTransformsResolver). Config.global_id does the encoding.
|
|
9
|
+
# Unlike PHP Lighthouse's, a decoded id is checked to be of the type it is
|
|
10
|
+
# for: `type:`, or with decode: ID the field's model when the argument filters
|
|
11
|
+
# its key (GlobalIdTypes), so another type's id fails rather than finds the
|
|
12
|
+
# record of the same number.
|
|
9
13
|
class Lighthouse::GraphQL::DirectiveResolvers::GlobalIdDirectiveResolver < Lighthouse::GraphQL::DirectiveResolvers::BaseDirectiveResolver
|
|
10
14
|
NAME = 'globalId'
|
|
11
15
|
|
|
@@ -25,6 +29,8 @@ class Lighthouse::GraphQL::DirectiveResolvers::GlobalIdDirectiveResolver < Light
|
|
|
25
29
|
This setting controls which of those is passed along.
|
|
26
30
|
"""
|
|
27
31
|
decode: GlobalIdDecode = ARRAY
|
|
32
|
+
"The type (GraphQL type or model) a decoded id must be of. Inferred for a key of the field's model."
|
|
33
|
+
type: String
|
|
28
34
|
) on FIELD_DEFINITION | INPUT_FIELD_DEFINITION | ARGUMENT_DEFINITION
|
|
29
35
|
GRAPHQL
|
|
30
36
|
end
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
# The model a field's @globalId arguments decode ids of, so another type's
|
|
4
|
+
# id fails the field instead of finding the record with the same number
|
|
5
|
+
# (`user(id: <a Post's id>)`). PHP Lighthouse checks nothing.
|
|
6
|
+
#
|
|
7
|
+
# - `@globalId(type: "User")`: that type (a GraphQL type or model name), any decode mode.
|
|
8
|
+
# - With decode: ID and no type:, the field's model (its `model:`, else its
|
|
9
|
+
# return type's), for an argument that filters that model by its key: one
|
|
10
|
+
# with @whereKey, an @eq / @in on the primary key, or one named after it
|
|
11
|
+
# (`id`). That is a field's argument, or a field of an input object one of
|
|
12
|
+
# its arguments takes, unless that argument is an association of the model
|
|
13
|
+
# (whose own ids it holds). Elsewhere (`authorId`, the inputs of nested
|
|
14
|
+
# mutations) the id is of another model, which `type:` names.
|
|
15
|
+
#
|
|
16
|
+
# Models are looked up per call, so code reloading works.
|
|
17
|
+
class Lighthouse::GraphQL::DirectiveResolvers::GlobalIdTypes
|
|
18
|
+
KEY_FILTERS = %w[eq in].freeze
|
|
19
|
+
|
|
20
|
+
def initialize(resolver, field_def)
|
|
21
|
+
@resolver = resolver
|
|
22
|
+
@keys = key_arguments(field_def)
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
# The class the ids `argument` decodes must be of; nil: any.
|
|
26
|
+
def model(directive, argument, mode, ctx)
|
|
27
|
+
explicit = Lighthouse::Support::DirectiveArgs.literal(directive, 'type')
|
|
28
|
+
return explicit_model(explicit, ctx) if explicit
|
|
29
|
+
return unless mode.to_s == 'ID' && @keys.key?(argument)
|
|
30
|
+
|
|
31
|
+
column, parent = @keys[argument]
|
|
32
|
+
model = field_model
|
|
33
|
+
model if model && keyed?(model, column, parent)
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
private
|
|
37
|
+
|
|
38
|
+
def explicit_model(name, ctx)
|
|
39
|
+
Lighthouse::GraphQL::GlobalId.type_model(name, ctx) ||
|
|
40
|
+
raise(Lighthouse::DefinitionError, "@globalId(type: #{name.inspect}): no such type or model.")
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
def keyed?(model, column, parent)
|
|
44
|
+
return false if parent && model.reflect_on_association(Lighthouse::Support::Naming.attribute(parent))
|
|
45
|
+
|
|
46
|
+
column == :primary_key || column == model.primary_key.to_s
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
# The field's model, when it has one: a field returning a scalar has none.
|
|
50
|
+
def field_model
|
|
51
|
+
model = @resolver.send(:model_class)
|
|
52
|
+
model if defined?(::ActiveRecord::Base) && model < ::ActiveRecord::Base
|
|
53
|
+
rescue Lighthouse::DefinitionError
|
|
54
|
+
nil
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
# { argument => [the column it filters, the argument whose input it is a field of] },
|
|
58
|
+
# by identity, for the @globalId(decode: ID) arguments and their inputs' fields.
|
|
59
|
+
def key_arguments(field_def)
|
|
60
|
+
keys = {}.compare_by_identity
|
|
61
|
+
field_def.arguments.each_value do |argument|
|
|
62
|
+
keys[argument] = [column(argument), nil] if decodes_id?(argument)
|
|
63
|
+
type = argument.type.unwrap
|
|
64
|
+
next unless type.kind.input_object?
|
|
65
|
+
|
|
66
|
+
type.arguments.each_value { |field| keys[field] = [column(field), argument.graphql_name] if decodes_id?(field) }
|
|
67
|
+
end
|
|
68
|
+
keys
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
def decodes_id?(argument)
|
|
72
|
+
directives(argument).any? { |d| d.name == 'globalId' && Lighthouse::Support::DirectiveArgs.literal(d, 'decode') == 'ID' }
|
|
73
|
+
end
|
|
74
|
+
|
|
75
|
+
def column(argument)
|
|
76
|
+
nodes = directives(argument)
|
|
77
|
+
return :primary_key if nodes.any? { |d| d.name == 'whereKey' }
|
|
78
|
+
|
|
79
|
+
filter = nodes.find { |d| KEY_FILTERS.include?(d.name) }
|
|
80
|
+
(Lighthouse::Support::DirectiveArgs.literal(filter, 'key') || Lighthouse::Support::Naming.attribute(argument.graphql_name)).to_s
|
|
81
|
+
end
|
|
82
|
+
|
|
83
|
+
def directives(argument) = argument.ast_node&.directives || []
|
|
84
|
+
end
|
|
@@ -3,8 +3,9 @@
|
|
|
3
3
|
require_relative 'base'
|
|
4
4
|
|
|
5
5
|
# @guard — require an authenticated user before the field resolves.
|
|
6
|
-
# Wraps the field's resolver; raises "Unauthenticated."
|
|
7
|
-
#
|
|
6
|
+
# Wraps the field's resolver; raises "Unauthenticated." (AuthenticationError,
|
|
7
|
+
# extensions.code UNAUTHENTICATED) when no user is present (per
|
|
8
|
+
# Config.authenticated_user). Mirrors PHP Lighthouse's @guard.
|
|
8
9
|
class Lighthouse::GraphQL::DirectiveResolvers::GuardDirectiveResolver < Lighthouse::GraphQL::DirectiveResolvers::BaseDirectiveResolver
|
|
9
10
|
def self.directive_name = 'guard'
|
|
10
11
|
|
|
@@ -24,7 +25,7 @@ class Lighthouse::GraphQL::DirectiveResolvers::GuardDirectiveResolver < Lighthou
|
|
|
24
25
|
inner = Lighthouse::GraphQL::DirectiveResolvers::Resolution.capture(target_type_class, method_name, @field_def, field_name)
|
|
25
26
|
|
|
26
27
|
wrapper = lambda do |obj, arguments, ctx|
|
|
27
|
-
raise
|
|
28
|
+
raise Lighthouse::AuthenticationError unless Lighthouse::Support::Authorization.authenticated?(ctx)
|
|
28
29
|
|
|
29
30
|
inner.call(obj, arguments, ctx)
|
|
30
31
|
end
|
data/lib/lighthouse/errors.rb
CHANGED
|
@@ -6,10 +6,24 @@ module Lighthouse
|
|
|
6
6
|
# missing @validator class. A server bug, so not a client-facing error.
|
|
7
7
|
class DefinitionError < StandardError; end
|
|
8
8
|
|
|
9
|
-
# Raised by
|
|
9
|
+
# Raised by @guard and @auth when no user is signed in. PHP Lighthouse's
|
|
10
|
+
# message; `extensions.code` is Apollo's UNAUTHENTICATED, so a client can
|
|
11
|
+
# tell it apart without matching the text (send the user to sign in).
|
|
12
|
+
class AuthenticationError < ::GraphQL::ExecutionError
|
|
13
|
+
CODE = 'UNAUTHENTICATED'
|
|
14
|
+
|
|
15
|
+
def initialize(message = 'Unauthenticated.', extensions: {}, **options)
|
|
16
|
+
super(message, extensions: { 'code' => CODE }.merge(extensions), **options)
|
|
17
|
+
end
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
# Raised by the @can* family when the policy denies the ability, with
|
|
21
|
+
# Apollo's FORBIDDEN as `extensions.code`.
|
|
10
22
|
class AuthorizationError < ::GraphQL::ExecutionError
|
|
11
|
-
|
|
12
|
-
|
|
23
|
+
CODE = 'FORBIDDEN'
|
|
24
|
+
|
|
25
|
+
def initialize(message = 'This action is unauthorized.', extensions: {}, **options)
|
|
26
|
+
super(message, extensions: { 'code' => CODE }.merge(extensions), **options)
|
|
13
27
|
end
|
|
14
28
|
end
|
|
15
29
|
|
|
@@ -23,7 +23,7 @@ module Lighthouse::Events::ExecutionTrace
|
|
|
23
23
|
class << self
|
|
24
24
|
def start(schema, query)
|
|
25
25
|
Lighthouse::Events.dispatch(
|
|
26
|
-
Lighthouse::Events::StartExecution.new(schema, query.document, query.provided_variables, query.operation_name, query.context)
|
|
26
|
+
Lighthouse::Events::StartExecution.new(schema, query.document, query.provided_variables, query.operation_name, query.context, query)
|
|
27
27
|
)
|
|
28
28
|
end
|
|
29
29
|
|
|
@@ -38,7 +38,7 @@ module Lighthouse::Events::ExecutionTrace
|
|
|
38
38
|
manipulate = Lighthouse::Events::ManipulateResult.new(result)
|
|
39
39
|
Lighthouse::Events.dispatch(manipulate)
|
|
40
40
|
result.replace(manipulate.result) unless manipulate.result.equal?(result)
|
|
41
|
-
Lighthouse::Events.dispatch(Lighthouse::Events::EndExecution.new(result))
|
|
41
|
+
Lighthouse::Events.dispatch(Lighthouse::Events::EndExecution.new(result, query))
|
|
42
42
|
end
|
|
43
43
|
|
|
44
44
|
def merge_extensions(result, responses)
|
|
@@ -55,17 +55,23 @@ module Lighthouse::Events
|
|
|
55
55
|
end
|
|
56
56
|
|
|
57
57
|
# A query is about to run. `query` is its parsed GraphQL::Language::Nodes::Document
|
|
58
|
-
# (nil if it does not parse)
|
|
58
|
+
# (nil if it does not parse), `operation_name` the one the client gave.
|
|
59
|
+
# Fires once per operation of a multiplex: in a batch, every StartExecution
|
|
60
|
+
# comes before any EndExecution, as graphql-ruby runs the operations
|
|
61
|
+
# together. `graphql_query` (the GraphQL::Query) is the one the operation's
|
|
62
|
+
# EndExecution carries too, to pair them.
|
|
59
63
|
class StartExecution < Event
|
|
60
|
-
attr_reader :schema, :query, :variables, :operation_name, :context
|
|
64
|
+
attr_reader :schema, :query, :variables, :operation_name, :context, :graphql_query
|
|
61
65
|
|
|
62
|
-
|
|
66
|
+
# PHP Lighthouse's five, positional as there, then the query.
|
|
67
|
+
def initialize(schema, query, variables, operation_name, context, graphql_query = nil) # rubocop:disable Metrics/ParameterLists
|
|
63
68
|
super()
|
|
64
69
|
@schema = schema
|
|
65
70
|
@query = query
|
|
66
71
|
@variables = variables
|
|
67
72
|
@operation_name = operation_name
|
|
68
73
|
@context = context
|
|
74
|
+
@graphql_query = graphql_query
|
|
69
75
|
end
|
|
70
76
|
end
|
|
71
77
|
|
|
@@ -95,14 +101,22 @@ module Lighthouse::Events
|
|
|
95
101
|
end
|
|
96
102
|
end
|
|
97
103
|
|
|
98
|
-
# A query ran and its result is final.
|
|
104
|
+
# A query ran and its result is final. `graphql_query` is the GraphQL::Query
|
|
105
|
+
# its StartExecution carried; `operation_name` the operation that ran (nil
|
|
106
|
+
# for an anonymous one), and `context` its context. PHP Lighthouse's carries
|
|
107
|
+
# the result only, where a batch runs its operations one after the other.
|
|
99
108
|
class EndExecution < Event
|
|
100
|
-
attr_reader :result
|
|
109
|
+
attr_reader :result, :graphql_query
|
|
101
110
|
|
|
102
|
-
def initialize(result)
|
|
111
|
+
def initialize(result, graphql_query = nil)
|
|
103
112
|
super()
|
|
104
113
|
@result = result
|
|
114
|
+
@graphql_query = graphql_query
|
|
105
115
|
end
|
|
116
|
+
|
|
117
|
+
def operation_name = graphql_query&.selected_operation_name
|
|
118
|
+
|
|
119
|
+
def context = graphql_query&.context
|
|
106
120
|
end
|
|
107
121
|
|
|
108
122
|
# The operation(s) ran: the result, or for a batch an Array of results.
|
data/lib/lighthouse/global_id.rb
CHANGED
|
@@ -60,6 +60,34 @@ module Lighthouse::GraphQL::GlobalId
|
|
|
60
60
|
gid ? [gid.model_name.demodulize, gid.model_id] : decode_object(global_id, context)
|
|
61
61
|
end
|
|
62
62
|
|
|
63
|
+
# Is `global_id`, decoded as `type`, an id of `model` or of a subclass (STI)?
|
|
64
|
+
# A Rails GlobalID names its model class; another names a GraphQL type, whose
|
|
65
|
+
# model is its @model(class:), else the class of its name.
|
|
66
|
+
def of_model?(global_id, type, model, context)
|
|
67
|
+
klass = rails_model(global_id) || type_model(type, context)
|
|
68
|
+
klass.is_a?(Class) && klass <= model
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
def rails_model(global_id)
|
|
72
|
+
return unless defined?(::GlobalID) && global_id.is_a?(String)
|
|
73
|
+
|
|
74
|
+
::GlobalID.parse(global_id)&.model_class
|
|
75
|
+
rescue NameError
|
|
76
|
+
nil
|
|
77
|
+
end
|
|
78
|
+
|
|
79
|
+
# The model of the GraphQL type (or model) named `name`.
|
|
80
|
+
def type_model(name, context)
|
|
81
|
+
Lighthouse::Support::ModelResolver.constantize(declared_model(name, context) || name)
|
|
82
|
+
end
|
|
83
|
+
|
|
84
|
+
# The @model(class:) of the schema's type of that name.
|
|
85
|
+
def declared_model(name, context)
|
|
86
|
+
schema_type = context.schema.get_type(name.to_s) if context.respond_to?(:schema) && context.schema
|
|
87
|
+
directives = schema_type.respond_to?(:ast_node) ? schema_type.ast_node&.directives : nil
|
|
88
|
+
Lighthouse::Support::DirectiveArgs.literal(directives&.find { |d| d.name == 'model' }, 'class')
|
|
89
|
+
end
|
|
90
|
+
|
|
63
91
|
# [type, id] of the object the schema's object_from_id finds.
|
|
64
92
|
def decode_object(global_id, context)
|
|
65
93
|
object = context.schema.object_from_id(global_id, context)
|
|
@@ -132,6 +132,7 @@ require_relative 'namespaces'
|
|
|
132
132
|
directive_resolvers/async_resolver
|
|
133
133
|
directive_resolvers/global_id_resolver
|
|
134
134
|
directive_resolvers/argument_binding
|
|
135
|
+
directive_resolvers/global_id_types
|
|
135
136
|
directive_resolvers/argument_transforms_resolver
|
|
136
137
|
directives/arguments/base
|
|
137
138
|
directives/arguments/filters
|
|
@@ -6,13 +6,14 @@ module Lighthouse
|
|
|
6
6
|
module ClassMethods
|
|
7
7
|
# The schema the operations run on: a schema class, its name (constantized
|
|
8
8
|
# per request) or a block (evaluated in the controller, per request).
|
|
9
|
-
def lighthouse_schema: (?(
|
|
9
|
+
def lighthouse_schema: (?(_Schema | String | Symbol)? schema) ?{ () -> _Schema } -> (_Schema | String | Symbol | Proc)?
|
|
10
10
|
end
|
|
11
11
|
|
|
12
12
|
private
|
|
13
13
|
|
|
14
14
|
# The operation's result, or nil when a @throttle performed the response.
|
|
15
|
-
def lighthouse_execute: (String? query, ?variables:
|
|
15
|
+
def lighthouse_execute: (String? query, ?variables: (Hash[String | Symbol, untyped] | String | _Parameters)?, ?operation_name: String?,
|
|
16
|
+
?extensions: GraphQL::PersistedQueries::extensions?, ?context: Hash[Symbol, untyped]) -> Hash[String, json]?
|
|
16
17
|
|
|
17
18
|
# Dispatch StartRequest, yield, dispatch EndRequest; only the outermost call does.
|
|
18
19
|
def lighthouse_request: [T] () { () -> T } -> T
|
|
@@ -24,10 +25,10 @@ module Lighthouse
|
|
|
24
25
|
def lighthouse_context: () -> Hash[Symbol, untyped]
|
|
25
26
|
|
|
26
27
|
# The schema class `lighthouse_schema` names.
|
|
27
|
-
def graphql_schema: () ->
|
|
28
|
+
def graphql_schema: () -> _Schema
|
|
28
29
|
|
|
29
30
|
# The `variables` parameter as a Hash: from a JSON String, a Hash or parameters.
|
|
30
|
-
def graphql_variables: (untyped param) -> Hash[String, untyped]
|
|
31
|
+
def graphql_variables: ((Hash[String | Symbol, untyped] | String | _Parameters)? param) -> Hash[String | Symbol, untyped]
|
|
31
32
|
end
|
|
32
33
|
|
|
33
34
|
module Controller
|
|
@@ -35,7 +36,7 @@ module Lighthouse
|
|
|
35
36
|
|
|
36
37
|
# The action: dispatches StartRequest, runs the operation (or a batch),
|
|
37
38
|
# sets Cache-Control, renders JSON, dispatches EndRequest.
|
|
38
|
-
def execute: () ->
|
|
39
|
+
def execute: () -> void
|
|
39
40
|
end
|
|
40
41
|
end
|
|
41
42
|
end
|