lighthouse-graphql 0.7.3 → 0.8.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +120 -1
- data/docs/authorization.md +46 -3
- data/docs/configuration.md +38 -0
- data/docs/directives.md +44 -0
- data/docs/events.md +17 -3
- data/docs/getting-started.md +63 -10
- data/docs/performance.md +9 -0
- data/docs/relay.md +6 -2
- data/docs/uploads.md +28 -2
- data/lib/lighthouse/config.rb +19 -5
- data/lib/lighthouse/controller.rb +6 -67
- data/lib/lighthouse/directive_resolvers/argument_transforms_resolver.rb +7 -1
- data/lib/lighthouse/directive_resolvers/attachment_resolver.rb +139 -0
- data/lib/lighthouse/directive_resolvers/auth_resolver.rb +1 -1
- data/lib/lighthouse/directive_resolvers/can_resolvers.rb +29 -5
- data/lib/lighthouse/directive_resolvers/guard_resolver.rb +4 -3
- data/lib/lighthouse/directives/registry.rb +1 -1
- data/lib/lighthouse/directives/soft_deletes_directive.rb +23 -6
- 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/field_resolver_stack.rb +32 -0
- data/lib/lighthouse/global_id.rb +16 -5
- data/lib/lighthouse/graphql/version.rb +1 -1
- data/lib/lighthouse/operations.rb +111 -0
- data/lib/lighthouse/pagination.rb +21 -1
- data/lib/lighthouse/persisted_queries/manifest.rb +40 -0
- data/lib/lighthouse/persisted_queries.rb +9 -27
- data/lib/lighthouse/railtie.rb +8 -1
- data/lib/lighthouse/rb_lighthouse.rb +6 -0
- data/lib/lighthouse/relation_batch.rb +1 -1
- data/lib/lighthouse/schema_generator.rb +2 -0
- data/lib/lighthouse/schema_printer.rb +100 -0
- data/lib/lighthouse/schema_report.rb +161 -0
- data/lib/lighthouse/server_directives.rb +63 -0
- data/lib/lighthouse/tasks/lighthouse.rake +25 -6
- data/lib/lighthouse/upload.rb +2 -1
- data/lib/lighthouse/upload_store.rb +53 -2
- data/sig/lighthouse/controller.rbs +23 -11
- data/sig/lighthouse/directives.rbs +51 -18
- data/sig/lighthouse/events.rbs +52 -27
- data/sig/lighthouse/graphql.rbs +29 -24
- data/sig/lighthouse.rbs +207 -20
- metadata +7 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 9256c2a7c721e495cf5c3754da6b1f684a1d15822014435150c08612af4964e6
|
|
4
|
+
data.tar.gz: 8acfd179e8ede63d8d9f7addfb85e5472435078e9f389ca684d543203353ef88
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 066e474500d83f2c5d8809275819a96941fa4e303bb3d2984a8652a7e54f2bf08e0259ef40f7ddbf8105eb5dc40485588c1e760c368c7cd9205823e44fc1c50a
|
|
7
|
+
data.tar.gz: 013fffcb17f486024bcb94f3ba347973326bbbaf1cfdfac3b2d7ba9be90a808e64c21d37d0eac5c74d4c552f8b5ed74b40c0689715a6041ae5d232de3560bee4
|
data/CHANGELOG.md
CHANGED
|
@@ -6,6 +6,123 @@ project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
|
|
6
6
|
|
|
7
7
|
## [Unreleased]
|
|
8
8
|
|
|
9
|
+
## [0.8.1] - 2026-10-30
|
|
10
|
+
|
|
11
|
+
### Changed
|
|
12
|
+
|
|
13
|
+
- **The RBS signatures are typed.** What Lighthouse takes from graphql-ruby, Active Record
|
|
14
|
+
and Rails, which ship no RBS, was `untyped`; it is now declared as interfaces of the methods
|
|
15
|
+
Lighthouse uses (`_Schema`, `_Result`, `_Context`, `_Query`, `_Relation`, `_DirectiveNode`,
|
|
16
|
+
`_Request`, ...), so Steep checks code that calls Lighthouse and `rake rbs:test` checks the
|
|
17
|
+
suite's real values against them. Results are `Hash[String, json]`, directive arguments
|
|
18
|
+
`literal`, `Events.listen` has an overload per event class, with the block's event and
|
|
19
|
+
answer typed. `untyped` is left for the app's own values (a user, a client's argument
|
|
20
|
+
value): 169 became 76. Typing found `BuilderApplier.apply` called without a context, which
|
|
21
|
+
the signature now allows.
|
|
22
|
+
|
|
23
|
+
### Removed
|
|
24
|
+
|
|
25
|
+
- `Config.directive_map`, a setting nothing read.
|
|
26
|
+
|
|
27
|
+
### Added
|
|
28
|
+
|
|
29
|
+
- **`Unauthenticated.` and `This action is unauthorized.` carry an `extensions.code`** (#5):
|
|
30
|
+
`UNAUTHENTICATED` from `@guard` and `@auth` (the new `Lighthouse::AuthenticationError`), and
|
|
31
|
+
`FORBIDDEN` from the `@can*` directives (`Lighthouse::AuthorizationError`), as Apollo names
|
|
32
|
+
them, so a client can react without matching the message. PHP Lighthouse's have no code.
|
|
33
|
+
- **`EndExecution` says which operation ended** (#4): `operation_name`, `context`, and the
|
|
34
|
+
`GraphQL::Query` as `graphql_query`, which `StartExecution` carries too. In a batch every
|
|
35
|
+
`StartExecution` comes before any `EndExecution`, so a listener could not tell the results
|
|
36
|
+
apart.
|
|
37
|
+
|
|
38
|
+
## [0.8.0] - 2026-10-29
|
|
39
|
+
|
|
40
|
+
### Added
|
|
41
|
+
|
|
42
|
+
- **`config.schema_log` reports the schema on boot and on every reload in development**: a
|
|
43
|
+
pretty-printed `✓` with the build time, the number of types and root fields, what the reload
|
|
44
|
+
added and removed (`+ User.nickname − Post.legacyTitle`) and the files written, or a `✗`
|
|
45
|
+
with the error the next request would raise, plus the persisted operations the schema no
|
|
46
|
+
longer accepts. On by default in a development server (not rake tasks or the console),
|
|
47
|
+
through the Rails logger, and on the terminal from boot.
|
|
48
|
+
- **`@attachment` answers the URL of an ActiveStorage attachment**, or the URLs of a
|
|
49
|
+
`has_many_attached`: the read side of `@upload`. Every parent's attachments and blobs load
|
|
50
|
+
at once (two queries for any number of parents), the name defaults to the field's without
|
|
51
|
+
`Url`/`Urls` (`avatarUrl` reads `avatar`), and `url:` picks Rails' `REDIRECT` (default),
|
|
52
|
+
`PROXY`, or the service's `DIRECT` URL signed for `expiresIn`; `variant:` names a variant.
|
|
53
|
+
- **`@canModel` on an argument or input field: who may use it.** It checks its ability against
|
|
54
|
+
the model only when the client gives that argument a value, at any depth (in each input of
|
|
55
|
+
a list too), with the field's `@can*` directives and their `action:`.
|
|
56
|
+
`@softDeletes(ability: "view_trashed")` puts it on the `trashed` argument it adds, so
|
|
57
|
+
listing trashed records can be for admins only.
|
|
58
|
+
- **`lighthouse_execute` runs an operation from any controller action (#3)**, with the context the
|
|
59
|
+
GraphQL endpoint gives it (`graphql_context`, and the IP and controller `@throttle` counts
|
|
60
|
+
with), to render a page with its data loaded. It comes from the new
|
|
61
|
+
`Lighthouse::GraphQL::Operations` concern, which any controller can include (with
|
|
62
|
+
`lighthouse_schema`), and which `Lighthouse::GraphQL::Controller` includes. It returns the
|
|
63
|
+
result, or `nil` when a `@throttle` answered the request with a 429.
|
|
64
|
+
- `config.persisted_queries_manifest` takes a Hash of operations, or a callable answering a
|
|
65
|
+
Hash or a path (called per persisted query), besides a JSON file: a frontend build can keep
|
|
66
|
+
its trusted documents where it writes them. A Hash is checked once, until its content changes.
|
|
67
|
+
- **`config.schema_output` keeps the client schema in a file, in development**, for a codegen
|
|
68
|
+
to read, as NestJS's `autoSchemaFile`: rewritten on boot and on every reload, only when it
|
|
69
|
+
changed (atomically, so a watcher never reads half of it). A path (`.json` for the
|
|
70
|
+
introspection result), or `{ path:, schema: }`. `bin/rails lighthouse:schema_output:verify`
|
|
71
|
+
fails in CI when the committed file is out of date.
|
|
72
|
+
- `lighthouse:print_schema` takes PHP Lighthouse's options: `FORMAT=json` (the introspection
|
|
73
|
+
result), `FORMAT=federation` (a subgraph's SDL), and `WRITE=1` / `WRITE=path` to write a
|
|
74
|
+
file (schema_output's, else `lighthouse-schema.graphql`). graphql-ruby already prints in
|
|
75
|
+
alphabetical order, so there is no `--sort`. With no `SCHEMA=`, it and the other tasks
|
|
76
|
+
(`validate_schema`, `persisted_queries:verify`) take `<App>Schema`, the name graphql-ruby's
|
|
77
|
+
install gives, before trying `AppSchema`.
|
|
78
|
+
|
|
79
|
+
### Changed
|
|
80
|
+
|
|
81
|
+
- **Clients no longer see Lighthouse's directives.** Introspection listed every directive
|
|
82
|
+
Lighthouse injects to build the schema (`@all`, `@canFind`, `@inject`, `@hash`, ... 96 in
|
|
83
|
+
the example app), with the types only they use (`CanAction`, `FeatureState`), and
|
|
84
|
+
`to_definition` / `lighthouse:print_schema` printed their definitions and every use
|
|
85
|
+
(`author: User! @belongsTo`). As in PHP Lighthouse, whose schema holds only the standard
|
|
86
|
+
directives and those the SDL declares, they are now taken out of the built schema: a client
|
|
87
|
+
sees `@skip`, `@include`, `@deprecated`, `@oneOf`, `@specifiedBy` and the SDL's own, and an
|
|
88
|
+
operation using `@all` is invalid. Lighthouse reads its directives from the SDL, so nothing
|
|
89
|
+
else changes.
|
|
90
|
+
|
|
91
|
+
### Fixed
|
|
92
|
+
|
|
93
|
+
- **`@upload` purges what it stored when the field fails.** It stores a file before the
|
|
94
|
+
field resolves, and a model's validations run after: a `@create` the model refused left
|
|
95
|
+
its blobs in the bucket (#1). The blobs `@upload` stored for a field that raises, returns
|
|
96
|
+
an error or adds one to the context are now purged before the response (one the storage
|
|
97
|
+
service fails to delete is left to `purge_later`). The uploads guide now
|
|
98
|
+
recommends passing the file to the attachment attribute, which ActiveStorage uploads
|
|
99
|
+
only after commit.
|
|
100
|
+
- **Paginated queries end their order with the primary key.** Pages are cut by position, so
|
|
101
|
+
rows the field's order tied (`@orderBy(column: "created_at")`) could come back in another
|
|
102
|
+
order on the next page's query, and show on two pages or none; the same in the per-parent
|
|
103
|
+
row numbers of a batched relation. `@paginate` (every `type:`) and the paginated relation
|
|
104
|
+
directives now append the primary key unless the order names it already. PHP Lighthouse
|
|
105
|
+
leaves the order as it is.
|
|
106
|
+
- **`@globalId(decode:)` no longer loads the record to read its id (#2).** On a schema with
|
|
107
|
+
graphql-ruby's `object_from_id` (Rails' GlobalID, as its install writes it), decoding went
|
|
108
|
+
through `object_from_id`: a SELECT before `@find`'s own, under the model's default scope,
|
|
109
|
+
so a soft-deleted record's id did not decode for `@restore` / `@forceDelete` with an
|
|
110
|
+
`object_from_id` that kept the scope. A Rails GlobalID now decodes without the database,
|
|
111
|
+
as PHP Lighthouse's `decodeID` does; only an id of another format goes through
|
|
112
|
+
`object_from_id`.
|
|
113
|
+
- A directive the SDL uses that is neither built in, Lighthouse's nor declared fails the build
|
|
114
|
+
with ``No directive found for `@can` on Query.a.``, as PHP Lighthouse names it. On a root type
|
|
115
|
+
it failed with a `NoMethodError` from inside graphql-ruby.
|
|
116
|
+
|
|
117
|
+
- **An optional `Upload` takes an explicit `null`.** The scalar refused it ("Could not get
|
|
118
|
+
uploaded file ... Instead got: nil"), so a client had to leave the argument out. `null`
|
|
119
|
+
now reaches the resolver as `nil`, and `@upload` stores nothing for it.
|
|
120
|
+
- **`@canFind` fails when nothing is at its `find:` path, instead of passing unchecked.** A
|
|
121
|
+
misspelt path (`find: "inputt.id"`), or an optional argument the client left out, found
|
|
122
|
+
no ids, so no model was checked and the field resolved. It now fails with `Got no key to
|
|
123
|
+
find a model at the expected input path: …`, as PHP Lighthouse does (through `action:`,
|
|
124
|
+
like the other failures). A `*` over an empty list still checks no model.
|
|
125
|
+
|
|
9
126
|
## [0.7.3] - 2026-10-29
|
|
10
127
|
|
|
11
128
|
### Changed
|
|
@@ -736,7 +853,9 @@ had been running inside a private monorepo as a path gem before being extracted.
|
|
|
736
853
|
- Config-driven extension seams: `Lighthouse.configure`, `resolver_namespaces`,
|
|
737
854
|
`on_error`, and `DirectiveRegistry.register` for app-defined directives.
|
|
738
855
|
|
|
739
|
-
[Unreleased]: https://github.com/Vaz-Innovation/lighthouse-graphql/compare/v0.
|
|
856
|
+
[Unreleased]: https://github.com/Vaz-Innovation/lighthouse-graphql/compare/v0.8.1...HEAD
|
|
857
|
+
[0.8.1]: https://github.com/Vaz-Innovation/lighthouse-graphql/compare/v0.8.0...v0.8.1
|
|
858
|
+
[0.8.0]: https://github.com/Vaz-Innovation/lighthouse-graphql/compare/v0.7.3...v0.8.0
|
|
740
859
|
[0.7.3]: https://github.com/Vaz-Innovation/lighthouse-graphql/compare/v0.7.2...v0.7.3
|
|
741
860
|
[0.7.2]: https://github.com/Vaz-Innovation/lighthouse-graphql/compare/v0.7.1...v0.7.2
|
|
742
861
|
[0.7.1]: https://github.com/Vaz-Innovation/lighthouse-graphql/compare/v0.7.0...v0.7.1
|
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 {
|
|
@@ -107,7 +108,10 @@ Plus, per directive:
|
|
|
107
108
|
- `@canFind(find: String!, model: String, findOrFail: Boolean = true, scopes: [String!])`.
|
|
108
109
|
`find` is the argument holding the primary key or keys, and may be a dot path
|
|
109
110
|
(`"input.id"`). A key that matches no record fails with
|
|
110
|
-
`Couldn't find Post with 'id'=…`, unless `findOrFail: false`.
|
|
111
|
+
`Couldn't find Post with 'id'=…`, unless `findOrFail: false`. Nothing at the path (a
|
|
112
|
+
misspelt one, or an optional argument the client left out) fails with
|
|
113
|
+
`Got no key to find a model at the expected input path: …`, as in PHP Lighthouse;
|
|
114
|
+
a `*` over an empty list (`"inputs.*.id"`) checks no model. `scopes` are model
|
|
111
115
|
scopes applied to the lookup.
|
|
112
116
|
- `@canModel(model: String)`.
|
|
113
117
|
- `@canQuery(model: String, scopes: [String!])`. `scopes` narrow the records
|
|
@@ -131,6 +135,45 @@ post(id: ID!): Post @canFind(ability: "show", find: "id", action: EXCEPTION_NOT_
|
|
|
131
135
|
`action` applies to the directive's own checks only. Errors from the resolver
|
|
132
136
|
itself are untouched.
|
|
133
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
|
+
|
|
148
|
+
### Who may use an argument
|
|
149
|
+
|
|
150
|
+
Some arguments are for some users only: `trashed` for admins, a filter on drafts
|
|
151
|
+
for editors. `@canModel` on an argument, or on an input field at any depth, checks
|
|
152
|
+
its ability against the model only when the client gives that argument a value, and
|
|
153
|
+
denies the whole field otherwise, as `@canModel` on the field does (`action:` too):
|
|
154
|
+
|
|
155
|
+
```graphql
|
|
156
|
+
type Query {
|
|
157
|
+
posts(filter: PostFilter): [Post!]! @all
|
|
158
|
+
tasks(trashed: Trashed @trashed @canModel(ability: "view_trashed")): [Task!]! @all
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
input PostFilter {
|
|
162
|
+
title: String @like
|
|
163
|
+
status: PostStatus @eq @canModel(ability: "filter_by_status", model: "Post")
|
|
164
|
+
}
|
|
165
|
+
```
|
|
166
|
+
|
|
167
|
+
```ruby
|
|
168
|
+
class TaskPolicy < ApplicationPolicy
|
|
169
|
+
def view_trashed? = user&.admin?
|
|
170
|
+
end
|
|
171
|
+
```
|
|
172
|
+
|
|
173
|
+
It runs with the field's `@can*` directives, before the field resolves. The model is
|
|
174
|
+
the directive's `model:`, else the field's, as for `@canModel` on a field.
|
|
175
|
+
`@softDeletes(ability:)` writes it on the `trashed` argument for you.
|
|
176
|
+
|
|
134
177
|
## Ordering
|
|
135
178
|
|
|
136
179
|
The order you write directives in does not matter. They run as:
|
data/docs/configuration.md
CHANGED
|
@@ -38,6 +38,44 @@ content is unchanged is left alone. `nil` (the default) turns it on in
|
|
|
38
38
|
development when the app has a `graphql.config.yml`, which `lighthouse:install`
|
|
39
39
|
writes. Set `true` or `false` to decide yourself.
|
|
40
40
|
|
|
41
|
+
### `schema_log`
|
|
42
|
+
|
|
43
|
+
Log what development learns of the schema on boot and on every reload: it built (in
|
|
44
|
+
how long, how big, what the reload added and removed, the files it wrote), or why it
|
|
45
|
+
did not, and the persisted operations it no longer accepts:
|
|
46
|
+
|
|
47
|
+
```text
|
|
48
|
+
[Lighthouse] ✓ AppSchema built in 61 ms: 47 types, 16 queries, 9 mutations, 3 subscriptions
|
|
49
|
+
[Lighthouse] + User.nickname − Post.legacyTitle
|
|
50
|
+
[Lighthouse] wrote schema.graphql
|
|
51
|
+
|
|
52
|
+
[Lighthouse] ✗ The schema does not build: Lighthouse::DefinitionError
|
|
53
|
+
[Lighthouse] No directive found for `@can` on User.nickname.
|
|
54
|
+
[Lighthouse] The next GraphQL request raises it: save a fix to reload.
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
The schema is checked as `bin/rails lighthouse:validate_schema` checks it (the
|
|
58
|
+
`ValidateSchema` event, then every type), through the Rails logger, in colour when
|
|
59
|
+
`config.colorize_logging` is on. `nil` (the default) logs in a development server,
|
|
60
|
+
not in rake tasks or the console; `true` or `false` decide.
|
|
61
|
+
|
|
62
|
+
### `schema_output`
|
|
63
|
+
|
|
64
|
+
A file development keeps the schema clients see in, for a codegen to read, as
|
|
65
|
+
NestJS's `autoSchemaFile`: rewritten on boot and on every reload, and only when it
|
|
66
|
+
changed, so a watcher runs only then ([`schema_log`](#schema-log) says when it did). A path under the app's root, whose extension
|
|
67
|
+
picks the format (`.json` for the introspection result, else SDL), or a Hash naming
|
|
68
|
+
the schema class too (`<App>Schema`, graphql-ruby's install's name, by default):
|
|
69
|
+
|
|
70
|
+
```ruby
|
|
71
|
+
config.schema_output = 'schema.graphql'
|
|
72
|
+
config.schema_output = { path: 'app/javascript/graphql/schema.json', schema: 'Admin::Schema' }
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
Commit the file and check it in CI with `bin/rails lighthouse:schema_output:verify`,
|
|
76
|
+
which fails when it is out of date; `bin/rails lighthouse:print_schema WRITE=1`
|
|
77
|
+
rewrites it. See [printing the schema](getting-started.md#the-schema-clients-see).
|
|
78
|
+
|
|
41
79
|
### `namespaces`
|
|
42
80
|
|
|
43
81
|
Where each kind of class a schema names is looked up, by kind — they have
|
data/docs/directives.md
CHANGED
|
@@ -89,6 +89,11 @@ type Query {
|
|
|
89
89
|
- **The total** is only counted when the client selects `paginatorInfo` or
|
|
90
90
|
`pageInfo`.
|
|
91
91
|
- **Cursors** are base64 of the item's position.
|
|
92
|
+
- **The order** ends with the primary key: a page is cut by position, so rows the
|
|
93
|
+
field's order ties (`@orderBy(column: "created_at")` on posts created in the same
|
|
94
|
+
second), or a query with no order, would otherwise come back in another order on
|
|
95
|
+
the next page, and a row would show on two pages or none. An order that names
|
|
96
|
+
the key already, or a grouped query, is left as it is. PHP Lighthouse does not add it.
|
|
92
97
|
|
|
93
98
|
### `@count`
|
|
94
99
|
|
|
@@ -247,6 +252,35 @@ keywords (`def summary(words:)`), otherwise positionally, in the order the SDL
|
|
|
247
252
|
declares them, as PHP Lighthouse passes them (`def summary(words)`). The SDL's
|
|
248
253
|
defaults apply, and a method taking no parameters is called without them.
|
|
249
254
|
|
|
255
|
+
### `@attachment`
|
|
256
|
+
|
|
257
|
+
The URL of an ActiveStorage attachment of the parent, or the URLs of a
|
|
258
|
+
`has_many_attached`: the read side of [`@upload`](uploads.md). Lighthouse's own, as
|
|
259
|
+
Laravel has no ActiveStorage.
|
|
260
|
+
|
|
261
|
+
```graphql
|
|
262
|
+
type User {
|
|
263
|
+
avatarUrl: String @attachment # has_one_attached :avatar
|
|
264
|
+
avatarThumbUrl: String @attachment(name: "avatar", variant: "thumb")
|
|
265
|
+
photoUrls: [String!]! @attachment(url: PROXY) # has_many_attached :photos
|
|
266
|
+
}
|
|
267
|
+
```
|
|
268
|
+
|
|
269
|
+
- **`name`** defaults to the field's name without its `Url` / `Urls` suffix:
|
|
270
|
+
`avatarUrl` reads `avatar`, `photoUrls` reads `photos`. A missing attachment is
|
|
271
|
+
`null`, a model without one of that name an error.
|
|
272
|
+
- **Every parent's attachments and blobs load at once**, two queries for any
|
|
273
|
+
number of users (`avatar_attachment: :blob`, what `User.with_attached_avatar`
|
|
274
|
+
loads), through the Dataloader when the schema uses it.
|
|
275
|
+
- **`url`**: `REDIRECT` (default) is Rails' redirect to the storage service,
|
|
276
|
+
`PROXY` streams the file through the app, and `DIRECT` is the service's own URL,
|
|
277
|
+
signed for `expiresIn` seconds on a private one (`ActiveStorage.service_urls_expire_in`
|
|
278
|
+
by default). The URL is absolute with the request's host, else a path.
|
|
279
|
+
- **`variant`** names a variant of the attachment
|
|
280
|
+
(`has_one_attached :avatar { |attachable| attachable.variant :thumb, resize_to_limit: [100, 100] }`).
|
|
281
|
+
A file that is not an image has none: `null`. A `REDIRECT` or `PROXY` variant is
|
|
282
|
+
processed when its URL is first followed; `DIRECT` processes it while resolving.
|
|
283
|
+
|
|
250
284
|
## Relay
|
|
251
285
|
|
|
252
286
|
### `@globalId` and `@node`
|
|
@@ -767,6 +801,16 @@ enum Trashed {
|
|
|
767
801
|
}
|
|
768
802
|
```
|
|
769
803
|
|
|
804
|
+
Anyone may pass `trashed` unless you say who: `@softDeletes(ability: "view_trashed")`
|
|
805
|
+
puts [`@canModel`](authorization.md#who-may-use-an-argument) on the argument it adds,
|
|
806
|
+
so a client that passes `trashed` (any value) needs the ability on the model's policy.
|
|
807
|
+
Leaving `trashed` out lists the records as usual. PHP Lighthouse has no `ability:`.
|
|
808
|
+
|
|
809
|
+
```graphql
|
|
810
|
+
tasks: [Task!]! @all @softDeletes(ability: "view_trashed")
|
|
811
|
+
# tasks(trashed: Trashed @trashed @canModel(ability: "view_trashed")): [Task!]! @all
|
|
812
|
+
```
|
|
813
|
+
|
|
770
814
|
### `@trashed`
|
|
771
815
|
|
|
772
816
|
The argument builder `@softDeletes` adds; use it by hand to name the argument
|
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/getting-started.md
CHANGED
|
@@ -267,13 +267,48 @@ Route to it (`post '/graphql', to: 'graphql#execute'`, plus a `get` for
|
|
|
267
267
|
- renders the JSON; in development an error renders as JSON with its backtrace.
|
|
268
268
|
|
|
269
269
|
The hooks `graphql_context`, `graphql_schema` and `graphql_variables(param)` are
|
|
270
|
-
private methods you can override.
|
|
271
|
-
|
|
270
|
+
private methods you can override.
|
|
271
|
+
|
|
272
|
+
### Running an operation from another action
|
|
273
|
+
|
|
274
|
+
To render a page with its data already loaded, run the operation from the action
|
|
275
|
+
with `lighthouse_execute`, from `Lighthouse::GraphQL::Operations` (which
|
|
276
|
+
`Lighthouse::GraphQL::Controller` includes). It runs the operation as the endpoint
|
|
277
|
+
does, with the endpoint's context: `graphql_context`, and the client's IP and the
|
|
278
|
+
controller `@throttle` counts with. It returns the result, or `nil` when a
|
|
279
|
+
`@throttle` answered the request with a 429:
|
|
280
|
+
|
|
281
|
+
```ruby
|
|
282
|
+
class PagesController < ApplicationController
|
|
283
|
+
include Lighthouse::GraphQL::Operations
|
|
284
|
+
|
|
285
|
+
lighthouse_schema AppSchema
|
|
286
|
+
|
|
287
|
+
def search
|
|
288
|
+
@results = lighthouse_execute('query Search($term: String!) { searchPosts(term: $term) { title } }',
|
|
289
|
+
variables: { term: params[:term] })
|
|
290
|
+
return if performed? # @throttle answered 429
|
|
291
|
+
end
|
|
292
|
+
|
|
293
|
+
private
|
|
294
|
+
|
|
295
|
+
def graphql_context = { current_user: current_user }
|
|
296
|
+
end
|
|
297
|
+
```
|
|
298
|
+
|
|
299
|
+
`extensions:` runs a persisted query by its hash, and `context:` adds to the
|
|
300
|
+
context. The operation events (`StartOperationOrOperations`, `StartExecution`, ...)
|
|
301
|
+
fire as for a client's request. The request events (`StartRequest`, `EndRequest`)
|
|
302
|
+
are the GraphQL endpoint's, one per GraphQL request, as PHP Lighthouse's controller
|
|
303
|
+
dispatches them. `EndRequest` carries the response, which does not exist yet while
|
|
304
|
+
the action is still loading its data, so wrap the whole action in
|
|
305
|
+
`lighthouse_request { ... }` to dispatch them around it, with the page's response:
|
|
272
306
|
|
|
273
307
|
```ruby
|
|
274
308
|
def search
|
|
275
309
|
lighthouse_request do
|
|
276
|
-
|
|
310
|
+
results = lighthouse_execute(SEARCH, variables: { term: params[:term] })
|
|
311
|
+
render inertia: 'Search', props: { results: results } unless performed?
|
|
277
312
|
end
|
|
278
313
|
end
|
|
279
314
|
```
|
|
@@ -348,18 +383,36 @@ VS Code. The files are derived from your schema, so the generator adds them to
|
|
|
348
383
|
registering a directive. The IDE reads every `.graphql` file the glob matches, as
|
|
349
384
|
one schema: it does not follow `#import`.
|
|
350
385
|
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
386
|
+
> The IDE reads fields as you wrote them: `users: [User!]! @paginate` still looks
|
|
387
|
+
> like a list to it, although clients query `users { data { … } }`. Field
|
|
388
|
+
> rewrites are not something an extra file can express.
|
|
389
|
+
|
|
390
|
+
### The schema clients see
|
|
391
|
+
|
|
392
|
+
Print it, as PHP Lighthouse's `lighthouse:print-schema` does: every type Lighthouse
|
|
393
|
+
generated, the fields rewritten (`users { data { … } }`), and none of Lighthouse's
|
|
394
|
+
directives, which instruct the server and are not part of the schema clients get
|
|
395
|
+
(nor of introspection). The task writes the SDL to stdout and nothing else:
|
|
354
396
|
|
|
355
397
|
```shell
|
|
356
|
-
bin/rails lighthouse:print_schema
|
|
398
|
+
bin/rails lighthouse:print_schema # <App>Schema, else AppSchema
|
|
357
399
|
SCHEMA=Admin::Schema bin/rails lighthouse:print_schema > generated.graphql
|
|
400
|
+
FORMAT=json bin/rails lighthouse:print_schema # the introspection result
|
|
401
|
+
FORMAT=federation bin/rails lighthouse:print_schema # a subgraph's SDL, with @key, ...
|
|
402
|
+
bin/rails lighthouse:print_schema WRITE=1 # config.schema_output, else lighthouse-schema.graphql
|
|
403
|
+
bin/rails lighthouse:print_schema WRITE=schema.graphql # that file
|
|
358
404
|
```
|
|
359
405
|
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
406
|
+
graphql-ruby prints types, fields, arguments and enum values in alphabetical order,
|
|
407
|
+
so the output is stable (PHP Lighthouse's `--sort`). `WRITE` leaves an unchanged
|
|
408
|
+
file alone. For a codegen, let development keep the file up to date with
|
|
409
|
+
[`schema_output`](configuration.md#schema-output), and check it in CI with
|
|
410
|
+
`bin/rails lighthouse:schema_output:verify`.
|
|
411
|
+
|
|
412
|
+
The schema is the one of the environment it is printed in: a field with
|
|
413
|
+
`@show(env: ["development"])` is in development's and not in production's. Print
|
|
414
|
+
the file a production client relies on with `RAILS_ENV=production` when the schema
|
|
415
|
+
uses `@show` or `@hide`.
|
|
363
416
|
|
|
364
417
|
## Casing: camelCase ↔ snake_case
|
|
365
418
|
|
data/docs/performance.md
CHANGED
|
@@ -117,6 +117,15 @@ config.persisted_queries_only = Rails.env.production?
|
|
|
117
117
|
- The manifest's operations are served from memory, so a cache eviction can
|
|
118
118
|
never drop them. The file is reloaded when it changes, and an id that is not
|
|
119
119
|
the SHA-256 of its body is rejected.
|
|
120
|
+
- The manifest can also be a Hash of operations, or a callable answering a Hash
|
|
121
|
+
or a path, called for every persisted query: to read what a frontend build
|
|
122
|
+
(Vite, graphql-codegen's persisted documents) writes somewhere of its own. A Hash
|
|
123
|
+
is checked once, until its content changes; keep the callable cheap (memoize the
|
|
124
|
+
parsed file), as it runs per request.
|
|
125
|
+
|
|
126
|
+
```ruby
|
|
127
|
+
config.persisted_queries_manifest = -> { ViteManifest.persisted_documents } # a Hash, memoized
|
|
128
|
+
```
|
|
120
129
|
- With `persisted_queries_only`, nothing else runs, and arbitrary queries get
|
|
121
130
|
`Only persisted queries are allowed.` (`PERSISTED_QUERY_NOT_IN_LIST`).
|
|
122
131
|
|
data/docs/relay.md
CHANGED
|
@@ -156,8 +156,12 @@ directives then work on them. The schema has one kind of global id and one `Node
|
|
|
156
156
|
- `@globalId` on a field that resolves an object's own id encodes it with
|
|
157
157
|
`id_from_object`: `Tag.id` answers `tag.to_gid_param`, as graphql-ruby's `Node.id`
|
|
158
158
|
does. Any other value is encoded as before.
|
|
159
|
-
- `@globalId` on an argument decodes
|
|
160
|
-
`ID` gives its id, and `ARRAY` / `TYPE` its
|
|
159
|
+
- `@globalId` on an argument decodes a Rails GlobalID without reading the database,
|
|
160
|
+
as PHP Lighthouse decodes its ids: `ID` gives its id, and `ARRAY` / `TYPE` its
|
|
161
|
+
model's name (`["Tag", "1"]`, `"Tag"`). So `tag(id: ID! @globalId(decode: ID) @eq):
|
|
162
|
+
Tag @find` runs the one query of `@find`, and a soft-deleted record's id decodes for
|
|
163
|
+
`@restore`. An id of another format (an `id_from_object` of your own) is decoded
|
|
164
|
+
with `object_from_id`, which loads the object.
|
|
161
165
|
- `node:` is the SDL's `Node` interface: `interface Node`, which `@node` adds, and
|
|
162
166
|
`implements Node` refer to `Types::NodeType`. A `@node` type implements it.
|
|
163
167
|
- `Query.node` and `nodes` find objects with `object_from_id`, through the
|
data/docs/uploads.md
CHANGED
|
@@ -20,10 +20,16 @@ type Mutation {
|
|
|
20
20
|
|
|
21
21
|
Lighthouse reads its values itself (`Lighthouse::GraphQL::Upload`): the resolver gets
|
|
22
22
|
the uploaded file, an `ActionDispatch::Http::UploadedFile`. A value that is not a file
|
|
23
|
-
makes the argument invalid. `Upload`
|
|
23
|
+
makes the argument invalid. An optional `Upload` (`avatar: Upload`) also takes `null`,
|
|
24
|
+
which the resolver gets as `nil`; leaving the argument out is not the same as sending
|
|
25
|
+
`null`. `Upload` is for arguments only, never a field's result.
|
|
24
26
|
|
|
25
27
|
An ActiveStorage attachment takes the file as it is: with `has_one_attached :avatar`
|
|
26
|
-
on `User`, the mutation above attaches it.
|
|
28
|
+
on `User`, the mutation above attaches it. This is the way to attach a file to a model:
|
|
29
|
+
ActiveStorage uploads it once the record is saved and the transaction committed, so a
|
|
30
|
+
mutation the model's validations refuse uploads nothing. Reach for `@upload` when the
|
|
31
|
+
argument needs the stored blob itself (its signed id), or a `path:` or service of its
|
|
32
|
+
own.
|
|
27
33
|
|
|
28
34
|
## `@upload`
|
|
29
35
|
|
|
@@ -51,6 +57,15 @@ The signed id is what an attachment attribute attaches (`user.avatar = signed_id
|
|
|
51
57
|
`ActiveStorage::Blob.find_signed` finds; the blob keeps the file's name and content
|
|
52
58
|
type. Lists of files are stored one by one, and `null` stays `null`.
|
|
53
59
|
|
|
60
|
+
`@upload` stores the file before the field resolves (after `@guard`, the `@can*`
|
|
61
|
+
directives and `@validator`), and the model validates it after. When the field fails,
|
|
62
|
+
because the model refuses it, or the resolver raises or reports an error, the blobs
|
|
63
|
+
`@upload` stored for it are purged before the response (a blob the service fails to
|
|
64
|
+
delete is left to ActiveStorage's purge job), so no blob and no file is left behind. A blob
|
|
65
|
+
that is stored but never attached (the field succeeds and does not attach it) stays,
|
|
66
|
+
as ActiveStorage's own direct uploads do: purge those with a periodic job over
|
|
67
|
+
`ActiveStorage::Blob.unattached`.
|
|
68
|
+
|
|
54
69
|
ActiveStorage makes a whole service public (`public: true` in `config/storage.yml`)
|
|
55
70
|
or private, never one file. So `public:` must agree with the service's, and the schema
|
|
56
71
|
never says a file is private that anyone can read: `@upload(public: true)` on a private
|
|
@@ -76,6 +91,17 @@ updateAvatar(avatar: Upload! @upload(disk: "avatars", path: "users", public: tru
|
|
|
76
91
|
`@upload` runs once `@guard`, the `@can*` family and `@validator` have let the call
|
|
77
92
|
through, so a refused call stores nothing.
|
|
78
93
|
|
|
94
|
+
## Serving the files: `@attachment`
|
|
95
|
+
|
|
96
|
+
[`@attachment`](directives.md#attachment) answers the URL of an attachment, loading
|
|
97
|
+
every parent's at once:
|
|
98
|
+
|
|
99
|
+
```graphql
|
|
100
|
+
type User {
|
|
101
|
+
avatarUrl: String @attachment
|
|
102
|
+
}
|
|
103
|
+
```
|
|
104
|
+
|
|
79
105
|
## The request
|
|
80
106
|
|
|
81
107
|
```shell
|
data/lib/lighthouse/config.rb
CHANGED
|
@@ -26,19 +26,34 @@ 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
|
|
33
33
|
queue_broadcasts broadcast_queue
|
|
34
34
|
global_id
|
|
35
|
+
schema_output schema_log
|
|
35
36
|
].freeze
|
|
36
37
|
|
|
37
38
|
class << self
|
|
38
39
|
attr_writer :schema_path, :ide_helper, :resolver_namespaces, :on_error, :authenticated_user, :policy_user, :authorizer,
|
|
39
40
|
:transactional_mutations, :schema_cache, :watch_sdl, :persisted_queries, :persisted_queries_store, :persisted_queries_only,
|
|
40
41
|
:queue_broadcasts,
|
|
41
|
-
:convert_empty_strings_to_null, :feature_checker, :feature_actor, :global_id
|
|
42
|
+
:convert_empty_strings_to_null, :feature_checker, :feature_actor, :global_id, :schema_log
|
|
43
|
+
|
|
44
|
+
# A file development keeps the client schema in (SchemaPrinter): a path
|
|
45
|
+
# (under Rails.root; `.json` for the introspection result), or
|
|
46
|
+
# { path:, schema: 'AppSchema' }. nil (the default): none.
|
|
47
|
+
attr_accessor :schema_output
|
|
48
|
+
|
|
49
|
+
# Log the schema's build on boot and on every reload (SchemaReport): it
|
|
50
|
+
# built (in how long, what changed, the files written) or why not. nil
|
|
51
|
+
# (the default): in a development server, not in rake tasks or the console.
|
|
52
|
+
def schema_log
|
|
53
|
+
return @schema_log unless @schema_log.nil?
|
|
54
|
+
|
|
55
|
+
defined?(::Rails::Server) ? ::Rails.env.development? : false
|
|
56
|
+
end
|
|
42
57
|
|
|
43
58
|
# The schema's entrypoint, as PHP Lighthouse's `schema_path`: the one file
|
|
44
59
|
# the schema is read from, importing the others (`#import user.graphql`,
|
|
@@ -63,8 +78,6 @@ module Lighthouse
|
|
|
63
78
|
File.exist?(::Rails.root.join('graphql.config.yml'))
|
|
64
79
|
end
|
|
65
80
|
|
|
66
|
-
def directive_map = @directive_map ||= {}
|
|
67
|
-
|
|
68
81
|
# Namespaces tried (in order) when resolving a class name from a directive
|
|
69
82
|
# argument, e.g. `@field(resolver: "ContactPerson")` or `@all(model: "X")`.
|
|
70
83
|
# Keeping this in config means the library holds no app-specific namespace
|
|
@@ -100,7 +113,8 @@ module Lighthouse
|
|
|
100
113
|
# persisted_queries accept Apollo's Automatic Persisted Queries (PersistedQueries)
|
|
101
114
|
# persisted_queries_store where APQ keeps queries; default Rails.cache
|
|
102
115
|
# persisted_queries_ttl how long they live there (seconds; nil: until evicted)
|
|
103
|
-
# persisted_queries_manifest a manifest of trusted operations, served by hash
|
|
116
|
+
# persisted_queries_manifest a manifest of trusted operations, served by hash: a JSON file,
|
|
117
|
+
# a Hash, or a callable answering either
|
|
104
118
|
# persisted_queries_only reject any operation not in the manifest
|
|
105
119
|
attr_accessor :schema_cache_path, :persisted_queries_ttl, :persisted_queries_manifest
|
|
106
120
|
|