lighthouse-graphql 0.7.1 → 0.7.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (42) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +58 -1
  3. data/docs/directives.md +7 -4
  4. data/docs/filtering-and-ordering.md +52 -0
  5. data/docs/getting-started.md +44 -11
  6. data/lib/lighthouse/ast_builder.rb +16 -4
  7. data/lib/lighthouse/cache_control.rb +14 -2
  8. data/lib/lighthouse/complexity.rb +11 -1
  9. data/lib/lighthouse/directive_resolvers/aggregate_resolver.rb +17 -1
  10. data/lib/lighthouse/directive_resolvers/all_resolver.rb +13 -1
  11. data/lib/lighthouse/directive_resolvers/association_resolvers.rb +70 -16
  12. data/lib/lighthouse/directive_resolvers/auth_resolver.rb +10 -1
  13. data/lib/lighthouse/directive_resolvers/base.rb +44 -22
  14. data/lib/lighthouse/directive_resolvers/broadcast_resolver.rb +12 -1
  15. data/lib/lighthouse/directive_resolvers/cache_resolvers.rb +28 -3
  16. data/lib/lighthouse/directive_resolvers/can_resolvers.rb +62 -9
  17. data/lib/lighthouse/directive_resolvers/count_resolver.rb +16 -1
  18. data/lib/lighthouse/directive_resolvers/event_resolver.rb +13 -1
  19. data/lib/lighthouse/directive_resolvers/field_resolver.rb +6 -1
  20. data/lib/lighthouse/directive_resolvers/find_resolver.rb +13 -1
  21. data/lib/lighthouse/directive_resolvers/first_resolver.rb +13 -1
  22. data/lib/lighthouse/directive_resolvers/guard_resolver.rb +10 -1
  23. data/lib/lighthouse/directive_resolvers/method_resolver.rb +13 -1
  24. data/lib/lighthouse/directive_resolvers/model_mutation_resolver.rb +75 -16
  25. data/lib/lighthouse/directive_resolvers/paginate_resolver.rb +17 -2
  26. data/lib/lighthouse/directive_resolvers/rename_resolver.rb +14 -1
  27. data/lib/lighthouse/directive_resolvers/subscription_resolver.rb +13 -1
  28. data/lib/lighthouse/directive_resolvers/validator_resolver.rb +13 -1
  29. data/lib/lighthouse/directives/arguments/base.rb +18 -0
  30. data/lib/lighthouse/directives/arguments/filters.rb +16 -7
  31. data/lib/lighthouse/directives/definitions.rb +43 -6
  32. data/lib/lighthouse/document_ast.rb +12 -0
  33. data/lib/lighthouse/enum_directive.rb +6 -1
  34. data/lib/lighthouse/feature_directive.rb +13 -1
  35. data/lib/lighthouse/graphql/version.rb +1 -1
  36. data/lib/lighthouse/graphql_ruby_patches.rb +26 -2
  37. data/lib/lighthouse/ide_helper.rb +7 -6
  38. data/lib/lighthouse/scalar_coercion.rb +4 -1
  39. data/lib/lighthouse/tasks/lighthouse.rake +8 -0
  40. data/lib/lighthouse/type_resolution.rb +8 -2
  41. data/sig/lighthouse/graphql.rbs +4 -0
  42. metadata +1 -1
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 504295e8c73043df4b45703bb304b02205a40298e5bc686eb7d3b93dd678478e
4
- data.tar.gz: f5e1bc31e13856b2f636978eaed1c8d00c5fe6246bba46f4fad513d8ee247dfc
3
+ metadata.gz: cf8e8162adf7eff6d40299e3e8ecc947b8958f032323d97b78596578030f3bfa
4
+ data.tar.gz: 3b4fcda74e4e0b4eeed1baf5853fb72a753cd7ff401a25a25cf234efabdb581f
5
5
  SHA512:
6
- metadata.gz: 5aa2a3bcb218dc305b68cbb0d2ac1b437fdcd801a0965bc0e9c69de994b8028febb7c6a46fe2edff8e4b39d386b94369a63d603f6899fe1b9065d1a7196fd750
7
- data.tar.gz: 1dd8ef4b1426c9c4555a087da98aaee17cdbbf3133bd375ee6e2d83e738a4911d5e091545b7b65a3262bf823a8bd7f5e034943cf2124077a9d54c42930ee0b7a
6
+ metadata.gz: 06c2d9e4a484d3bd54556ef758bccb41759f28f8ac39aaf2b857f5107a364f74b4a701e3cc426ec8a72ad12b5aa96a4406c139bd273542dc746725db71489e59
7
+ data.tar.gz: d9eddb09ca7fc8acacbaeca60fa646d9a655a9a96a83a3be25aeeba418cd902cd64b9a2f9b0dd4c1108a258208a4a300b759837c8dca1300d8a39af1e74ae249
data/CHANGELOG.md CHANGED
@@ -6,6 +6,61 @@ project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
6
6
 
7
7
  ## [Unreleased]
8
8
 
9
+ ## [0.7.3] - 2026-10-29
10
+
11
+ ### Changed
12
+
13
+ - **Filters on input fields apply without `@nest`, as in PHP Lighthouse.** The filter
14
+ directives (`@eq`, `@where`, `@like`, ...) on an input object's fields apply whenever the
15
+ client sends it: at any depth, for every input of a list (together), and through `@spread`,
16
+ as PHP Lighthouse's `applyArgBuilderDirectives` goes through nested inputs. They used to
17
+ apply only under an argument marked `@nest`, which is still accepted and changes nothing.
18
+ `posts(filter: PostFilter): [Post!]! @all` filters by the `@eq` inside `PostFilter`.
19
+
20
+ ### Fixed
21
+
22
+ - **A definition of a built-in directive in the SDL no longer disables it.** graphql-ruby
23
+ replaced its `@oneOf`, `@skip`, `@include`, `@deprecated` and `@specifiedBy` with a plain
24
+ directive of the same name when the SDL declared one (`directive @oneOf on INPUT_OBJECT`),
25
+ so a `@oneOf` input accepted any number of fields while introspection still reported
26
+ `isOneOf: true`, and `@skip` skipped nothing. Lighthouse now drops those definitions and
27
+ keeps graphql-ruby's directives.
28
+ - A relation field (`@hasMany`, ...) filtered only through an input object's fields took the
29
+ batched load and ignored the filters, and `@delete`, `@forceDelete` and `@restore` refused
30
+ such an argument as no filter ("Would modify all models"). Both now see nested filters.
31
+
32
+ ### Added
33
+
34
+ - `@oneOf` input objects are documented and specced
35
+ ([Filtering & Ordering](docs/filtering-and-ordering.md#one-filter-of-several-oneof)):
36
+ `user(by: UserBy!): User @find` finds by whichever one field the client gives, and
37
+ graphql-ruby rejects a query giving none or more than one.
38
+
39
+ ## [0.7.2] - 2026-10-29
40
+
41
+ ### Added
42
+
43
+ - `bin/rails lighthouse:print_schema` prints the final schema, as PHP Lighthouse's
44
+ `lighthouse:print-schema` does: to stdout, from the current configuration
45
+ (`SCHEMA=AppSchema` names another schema). Redirect it to keep it:
46
+ `bin/rails lighthouse:print_schema > generated.graphql`.
47
+
48
+ ### Changed
49
+
50
+ - **`schema-directives.graphql` documents everything.** Every directive, argument, enum value
51
+ and input field Lighthouse defines carries a description, so the IDE shows it on hover and in
52
+ autocomplete. A spec fails when a new one is added without.
53
+ - `programmatic-types.graphql` always declares the `_` placeholder scalar (unless your SDL
54
+ declares it), whether or not the SDL uses it yet, so the IDE never reports `Unknown type "_"`
55
+ the moment you write `where: _ @whereConditions`.
56
+ - The `_` placeholder exists only for the IDE: a `scalar _` in your SDL is dropped once the
57
+ schema builds, so the executable schema (and its introspection) has no `_` type.
58
+
59
+ ### Fixed
60
+
61
+ - Builds on graphql-ruby 2.6.14, whose changed internals made the root types
62
+ (`Types::QueryType`, ...) lose the app's code-first base.
63
+
9
64
  ## [0.7.1] - 2026-10-18
10
65
 
11
66
  ### Added
@@ -681,7 +736,9 @@ had been running inside a private monorepo as a path gem before being extracted.
681
736
  - Config-driven extension seams: `Lighthouse.configure`, `resolver_namespaces`,
682
737
  `on_error`, and `DirectiveRegistry.register` for app-defined directives.
683
738
 
684
- [Unreleased]: https://github.com/Vaz-Innovation/lighthouse-graphql/compare/v0.7.1...HEAD
739
+ [Unreleased]: https://github.com/Vaz-Innovation/lighthouse-graphql/compare/v0.7.3...HEAD
740
+ [0.7.3]: https://github.com/Vaz-Innovation/lighthouse-graphql/compare/v0.7.2...v0.7.3
741
+ [0.7.2]: https://github.com/Vaz-Innovation/lighthouse-graphql/compare/v0.7.1...v0.7.2
685
742
  [0.7.1]: https://github.com/Vaz-Innovation/lighthouse-graphql/compare/v0.7.0...v0.7.1
686
743
  [0.7.0]: https://github.com/Vaz-Innovation/lighthouse-graphql/compare/v0.6.0...v0.7.0
687
744
  [0.6.0]: https://github.com/Vaz-Innovation/lighthouse-graphql/compare/v0.5.0...v0.6.0
data/docs/directives.md CHANGED
@@ -545,7 +545,7 @@ type Query {
545
545
  createdAt: DateRange @whereBetween(key: "created_at")
546
546
  tagged: [String!] @whereJsonContains(key: "tags")
547
547
  trending: Boolean @scope
548
- filter: PostFilter @nest
548
+ filter: PostFilter
549
549
  limit: Int @limit
550
550
  ): [Post!]! @all
551
551
  }
@@ -554,11 +554,14 @@ input DateRange { from: DateTime! to: DateTime! }
554
554
  input PostFilter { title: String @like, authorId: ID @eq(key: "author_id") }
555
555
  ```
556
556
 
557
+ The filters on an input object's fields (`filter: PostFilter` above) apply when
558
+ the client sends it, at any depth and for each input of a list, as PHP
559
+ Lighthouse applies them. Those of a list's inputs all apply together.
560
+
557
561
  ### `@nest`
558
562
 
559
- On an argument whose type is an input object, applies the filters of the input's
560
- fields (at any depth, for those that carry `@nest` too), as if they were the
561
- field's own arguments.
563
+ Accepted as PHP Lighthouse's SDL writes it, on an argument or input field of an
564
+ input object type. Filters on nested input fields apply without it.
562
565
 
563
566
  ### `@limit`
564
567
 
@@ -54,6 +54,58 @@ type Query {
54
54
  }
55
55
  ```
56
56
 
57
+ ### Filters inside input objects
58
+
59
+ Filter directives also go on the fields of an input object. Its fields' filters
60
+ apply when the client sends it, at any depth, as in PHP Lighthouse:
61
+
62
+ ```graphql
63
+ input PostFilter {
64
+ title: String @like
65
+ author: AuthorFilter
66
+ }
67
+
68
+ input AuthorFilter { id: ID @eq(key: "author_id") }
69
+
70
+ type Query {
71
+ posts(filter: PostFilter): [Post!]! @all
72
+ }
73
+ ```
74
+
75
+ `posts(filter: { author: { id: 1 } })` keeps the posts of author 1. In a list of
76
+ inputs (`filters: [PostFilter!]`), every input's filters apply, together. An
77
+ argument with `@spread` filters the same way.
78
+
79
+ ### One filter of several: `@oneOf`
80
+
81
+ An input object marked [`@oneOf`](https://spec.graphql.org/draft/#sec-OneOf-Input-Objects)
82
+ takes exactly one of its fields: to find a user by id **or** by email, but not
83
+ both. Its fields' filters apply to the query:
84
+
85
+ ```graphql
86
+ input UserBy @oneOf {
87
+ id: ID @eq
88
+ email: String @eq
89
+ }
90
+
91
+ type Query {
92
+ user(by: UserBy!): User @find
93
+ }
94
+ ```
95
+
96
+ ```graphql
97
+ { user(by: { email: "ada@example.com" }) { id } }
98
+ ```
99
+
100
+ graphql-ruby validates it: a query giving two fields, none, or a `null` is rejected
101
+ before anything resolves, and introspection reports the type's `isOneOf`. Every
102
+ field of a `@oneOf` input is nullable and has no default value; the schema fails to
103
+ build otherwise.
104
+
105
+ `@oneOf` is built in, so the SDL needn't declare it. A `directive @oneOf on
106
+ INPUT_OBJECT` copied from elsewhere is ignored, like a definition of `@skip`,
107
+ `@include`, `@deprecated` or `@specifiedBy`: graphql-ruby's own directive stays.
108
+
57
109
  ## Ordering
58
110
 
59
111
  `@orderBy` works as in PHP Lighthouse. On an argument, the client picks the
@@ -5,6 +5,12 @@ GraphQL endpoint.
5
5
 
6
6
  ## Install
7
7
 
8
+ Lighthouse is built on [graphql-ruby](https://graphql-ruby.org), and the two are
9
+ set up together, in three commands:
10
+
11
+ **The gems.** `lighthouse-graphql` depends on `graphql ~> 2.5`, so adding it
12
+ installs graphql-ruby too:
13
+
8
14
  ```ruby
9
15
  # Gemfile
10
16
  gem 'lighthouse-graphql'
@@ -14,19 +20,36 @@ gem 'lighthouse-graphql'
14
20
  bundle install
15
21
  ```
16
22
 
17
- Runtime dependencies (`graphql ~> 2.5`, `activesupport`) come with the gem. If you
18
- want federation, add `apollo-federation` to your app — it is intentionally **not**
19
- a hard dependency of Lighthouse so non-federated apps stay lean.
23
+ You can also list `gem 'graphql'` yourself, to pin its version. On graphql 2.5, keep
24
+ the `json` gem below 3: graphql 2.5 calls `JSON.generate` with an option json 3
25
+ removed (graphql 2.6 no longer does). If you want federation, add `apollo-federation`
26
+ to your app — it is intentionally **not** a hard dependency of Lighthouse so
27
+ non-federated apps stay lean.
20
28
 
21
- ## The install generator
29
+ **graphql-ruby's install** (optional). This writes the schema class, the base
30
+ classes (`Types::BaseObject`, ...), the root types and the controller. Choose its
31
+ options here, as for any graphql-ruby app (`--relay`, `--batch`, `--api`,
32
+ `--schema=ApiSchema`, `--skip-mutation-root-type`, ...):
33
+
34
+ ```shell
35
+ bin/rails generate graphql:install
36
+ ```
37
+
38
+ **Lighthouse's install.** It adapts what graphql-ruby wrote and adds its own files
39
+ (see below). If you skipped graphql-ruby's install, it runs `graphql:install` for you,
40
+ with graphql-ruby's defaults, so the two commands are never both required:
22
41
 
23
42
  ```shell
24
43
  bin/rails generate lighthouse:install
25
44
  ```
26
45
 
27
- does steps 1–5 below for you. It builds on graphql-ruby's own install,
28
- `rails g graphql:install`, with whichever of its options the app chose (`--relay`,
29
- `--batch`, `--api`, ...). When the app has no graphql-ruby schema class yet, it runs
46
+ Run `graphql:install` yourself only to pass it options; Lighthouse then keeps them.
47
+
48
+ ## The install generator
49
+
50
+ `bin/rails generate lighthouse:install` does steps 1–5 below for you. It builds on
51
+ graphql-ruby's own install, `rails g graphql:install`, with whichever of its options
52
+ the app chose (`--relay`, `--batch`, `--api`, ...). When the app has no graphql-ruby schema class yet, it runs
30
53
  `graphql:install` first, with graphql-ruby's defaults. Then it adapts what
31
54
  graphql-ruby wrote, and adds Lighthouse's own:
32
55
 
@@ -303,10 +326,11 @@ It writes two files:
303
326
  - **`programmatic-types.graphql`** — the types Lighthouse adds to the executable
304
327
  schema: `<Type>Paginator`, `<Type>SimplePaginator`, `<Type>Connection`/`Edge`,
305
328
  `PaginatorInfo`, `SimplePaginatorInfo`, `PageInfo`, the `@whereConditions` and
306
- `@orderBy` inputs and enums, and the `_` placeholder scalar (without it, the
307
- IDE reports `Unknown type "_"`). It leaves out every type the SDL or
308
- `schema-directives.graphql` defines, because graphql-js, which the IDE plugins
309
- use, rejects a type defined twice.
329
+ `@orderBy` inputs and enums, and the `_` placeholder scalar (always declared,
330
+ even before the SDL uses it; without it, the IDE reports `Unknown type "_"`).
331
+ It is for the IDE only: the schema Lighthouse builds has no `_` type. It leaves
332
+ out every type the SDL or `schema-directives.graphql` defines, because
333
+ graphql-js, which the IDE plugins use, rejects a type defined twice.
310
334
 
311
335
  Point your IDE's [graphql-config](https://the-guild.dev/graphql/config) at them
312
336
  next to your SDL — the install generator writes this `graphql.config.yml`:
@@ -324,6 +348,15 @@ VS Code. The files are derived from your schema, so the generator adds them to
324
348
  registering a directive. The IDE reads every `.graphql` file the glob matches, as
325
349
  one schema: it does not follow `#import`.
326
350
 
351
+ To see the schema clients get, with every type Lighthouse generated and the
352
+ fields rewritten (`users { data { … } }`), print it. The task writes the SDL to
353
+ stdout and nothing else, so redirect it to keep it:
354
+
355
+ ```shell
356
+ bin/rails lighthouse:print_schema # AppSchema
357
+ SCHEMA=Admin::Schema bin/rails lighthouse:print_schema > generated.graphql
358
+ ```
359
+
327
360
  > The IDE reads fields as you wrote them: `users: [User!]! @paginate` still looks
328
361
  > like a list to it, although clients query `users { data { … } }`. Field
329
362
  > rewrites are not something an extra file can express.
@@ -4,14 +4,17 @@
4
4
  # stitched schema:
5
5
  #
6
6
  # 1. DocumentAST.from_source: every type defined once.
7
- # 2. Type extensions merged into the types they extend. An extension of a
7
+ # 2. The SDL's definitions of graphql-ruby's built-in directives (@skip,
8
+ # @include, @deprecated, @oneOf, @specifiedBy) dropped: see BUILT_IN_DIRECTIVES.
9
+ # 3. Type extensions merged into the types they extend. An extension of a
8
10
  # root type that has no definition (`extend type Query` alone) defines it.
9
- # 3. The type and field manipulators: @hide/@show, @model, @paginate (and the
11
+ # 4. The type and field manipulators: @hide/@show, @model, @paginate (and the
10
12
  # relation directives' `type:`), @softDeletes, @node.
11
- # 4. The argument manipulators: each argument directive whose class includes
13
+ # 5. The argument manipulators: each argument directive whose class includes
12
14
  # Contracts::ArgManipulator rewrites its argument (@whereConditions,
13
15
  # @whereHasConditions, @orderBy turn `_` into the input types they generate).
14
- # 5. The definitions of directives the generated types use (`@enum`).
16
+ # 6. `scalar _`, if the SDL declares it for the IDE, dropped: nothing is left of the placeholder.
17
+ # 7. The definitions of directives the generated types use (`@enum`).
15
18
  #
16
19
  # Returns the manipulated SDL; ManipulateAST listeners then see its document.
17
20
  module Lighthouse::GraphQL::ASTBuilder
@@ -40,14 +43,23 @@ module Lighthouse::GraphQL::ASTBuilder
40
43
 
41
44
  OBJECT_LIKE = [Nodes::ObjectTypeDefinition, Nodes::InterfaceTypeDefinition].freeze
42
45
 
46
+ # graphql-ruby's classes for these do the work: @skip skips, @oneOf makes the
47
+ # input object accept exactly one field. A definition in the SDL (copied from
48
+ # a printed schema, or from a graphql-js project, where it is harmless) would
49
+ # replace the class with a plain directive of the same name, which does nothing.
50
+ BUILT_IN_DIRECTIVES = ::GraphQL::Schema.default_directives.keys.freeze
51
+
43
52
  module_function
44
53
 
45
54
  def build(sdl, registry: Lighthouse::DirectiveRegistry.default)
46
55
  document_ast = Lighthouse::GraphQL::DocumentAST.from_source(sdl)
56
+ BUILT_IN_DIRECTIVES.each { |name| document_ast.remove_directive(name) }
47
57
  extend_types(document_ast)
48
58
 
49
59
  document_ast = Lighthouse::GraphQL::DocumentAST.from_source(registry.manipulate_sdl(document_ast.to_sdl))
50
60
  apply_arg_manipulators(document_ast, registry)
61
+ # `scalar _` is for the IDE (IdeHelper); the executable schema has no placeholder.
62
+ document_ast.remove_type(Lighthouse::GraphQL::Directives::GeneratedTypes::PLACEHOLDER)
51
63
 
52
64
  Lighthouse::GraphQL::SchemaGenerator.inject_directive_definitions(document_ast.to_sdl)
53
65
  end
@@ -21,8 +21,20 @@ module Lighthouse::GraphQL::CacheControl
21
21
  # Arguments with a default are nullable, not `Type! = default` as in PHP: graphql-ruby
22
22
  # 2.5 does not fill in a directive argument's default, so a non-null one would be
23
23
  # refused wherever it is left out. Left out or null, it takes its default.
24
- DEFINITION = 'directive @cacheControl(maxAge: Int = 0, scope: CacheControlScope = PUBLIC, ' \
25
- 'inheritMaxAge: Boolean = false) on FIELD_DEFINITION | OBJECT | INTERFACE | UNION'
24
+ DEFINITION = <<~GRAPHQL.chomp
25
+ """
26
+ Set the HTTP Cache-Control header of the response, from the fields and types a query selects.
27
+ The response may be cached for the lowest maxAge among them, and is private if any scope is.
28
+ """
29
+ directive @cacheControl(
30
+ "The maximum amount of time, in seconds, the field's value may be cached. 0 means `no-cache`."
31
+ maxAge: Int = 0
32
+ "Who a cached response may be shared with."
33
+ scope: CacheControlScope = PUBLIC
34
+ "Use the maxAge of the parent field. The `maxAge` argument is ignored."
35
+ inheritMaxAge: Boolean = false
36
+ ) on FIELD_DEFINITION | OBJECT | INTERFACE | UNION
37
+ GRAPHQL
26
38
 
27
39
  TYPE_DEFINITIONS = {
28
40
  'CacheControlScope' => <<~GRAPHQL.chomp
@@ -28,7 +28,17 @@
28
28
  # Analyzer scores each query for BuildExtensionsResponse#query_complexity while
29
29
  # something listens to that event.
30
30
  module Lighthouse::GraphQL::Complexity
31
- DEFINITION = 'directive @complexity(resolver: String) on FIELD_DEFINITION'
31
+ DEFINITION = <<~GRAPHQL.chomp
32
+ """
33
+ Customize the cost of a field in the complexity score of a query, which graphql-ruby checks
34
+ against the schema's `max_complexity`. Without a resolver, the field costs 1 plus its selection,
35
+ once per item asked for on a paginated field.
36
+ """
37
+ directive @complexity(
38
+ "The analyzer class that gives the cost, looked up in Config.namespaces[:analyzers]. It includes Lighthouse::GraphQL::ComplexityAnalyzer and declares `complexity`."
39
+ resolver: String
40
+ ) on FIELD_DEFINITION
41
+ GRAPHQL
32
42
 
33
43
  CONTEXT_KEY = :lighthouse_query_complexity
34
44
 
@@ -16,7 +16,23 @@ class Lighthouse::GraphQL::DirectiveResolvers::AggregateDirectiveResolver < Ligh
16
16
  }.freeze
17
17
 
18
18
  def self.definition
19
- 'directive @aggregate(function: String!, column: String, model: String, relation: String) on FIELD_DEFINITION'
19
+ <<~GRAPHQL.chomp
20
+ """
21
+ Return an aggregate of a column over the records of a model, or of a relation of the parent.
22
+ The records are narrowed by the field's argument filters (@eq, @where, ...).
23
+ An unknown function, or a failing aggregation, answers null.
24
+ """
25
+ directive @aggregate(
26
+ "The aggregate to compute: SUM, AVG, MIN, MAX or COUNT (case-insensitive)."
27
+ function: String!
28
+ "The column to aggregate. COUNT does not use it."
29
+ column: String
30
+ "The class name of the model whose records to aggregate. Takes precedence over `relation`."
31
+ model: String
32
+ "The relation of the parent to aggregate over."
33
+ relation: String
34
+ ) on FIELD_DEFINITION
35
+ GRAPHQL
20
36
  end
21
37
 
22
38
  def define_resolver(target_type_class)
@@ -5,7 +5,19 @@ require_relative 'builder_applier'
5
5
 
6
6
  class Lighthouse::GraphQL::DirectiveResolvers::AllDirectiveResolver < Lighthouse::GraphQL::DirectiveResolvers::BaseDirectiveResolver
7
7
  def self.directive_name = 'all'
8
- def self.definition = 'directive @all(model: String, scopes: [String!]) on FIELD_DEFINITION'
8
+
9
+ def self.definition
10
+ <<~GRAPHQL.chomp
11
+ """
12
+ Get a collection of models as the field result: all the model's records,
13
+ narrowed by `scopes`, @builder and the field's argument filters (@eq, @where, ...).
14
+ """
15
+ directive @all(
16
+ #{MODEL_ARGUMENT}
17
+ #{SCOPES_ARGUMENT}
18
+ ) on FIELD_DEFINITION
19
+ GRAPHQL
20
+ end
9
21
 
10
22
  def define_resolver(target_type_class)
11
23
  model_class = constantize_model(extract_model_name || @field_def.type.unwrap.graphql_name)
@@ -20,6 +20,21 @@ module Lighthouse::GraphQL::DirectiveResolvers
20
20
 
21
21
  def self.collection? = false
22
22
 
23
+ # The directive's SDL: `description`, then the arguments of a relationship —
24
+ # and, on a collection, the pagination ones.
25
+ def self.definition(name = directive_name, description = self::DESCRIPTION)
26
+ arguments = [RELATION_ARGUMENT, SCOPES_ARGUMENT]
27
+ arguments += PAGINATION_ARGUMENTS if collection?
28
+ <<~GRAPHQL.chomp
29
+ """
30
+ #{description}
31
+ """
32
+ directive @#{name}(
33
+ #{arguments.join("\n")}
34
+ ) on FIELD_DEFINITION
35
+ GRAPHQL
36
+ end
37
+
23
38
  def define_resolver(target_type_class)
24
39
  me = self
25
40
  install(target_type_class, lambda do |obj, args, ctx|
@@ -59,18 +74,16 @@ module Lighthouse::GraphQL::DirectiveResolvers
59
74
  directive_arg('scopes') || Lighthouse::GraphQL::DirectiveResolvers::BuilderApplier.any?(@field_def.ast_node) || filtered?(args)
60
75
  end
61
76
 
77
+ # A supplied argument, or input field at any depth, with a filter directive
78
+ # (@eq, @where, ...), or a supplied where-conditions argument.
62
79
  def filtered?(args)
63
- registry = Lighthouse::DirectiveRegistry.default
80
+ return true if filtering?(args)
81
+
64
82
  (@field_def.ast_node&.arguments || []).any? do |argument|
65
- args.key?(argument.name.underscore.to_sym) && argument.directives.any? { |d| filter_directive?(registry, d) }
83
+ args.key?(argument.name.underscore.to_sym) && argument.directives.any? { |d| CONDITION_DIRECTIVES.include?(d.name) }
66
84
  end
67
85
  end
68
86
 
69
- # An argument builder (@eq, @where, ...) or a where-conditions filter.
70
- def filter_directive?(registry, directive)
71
- registry.arg_builder_class(directive.name) || CONDITION_DIRECTIVES.include?(directive.name)
72
- end
73
-
74
87
  def constrained(relation, args, ctx)
75
88
  relation = apply_where_filters(scoped(relation), args, ctx)
76
89
  self.class.collection? ? relation : relation.first
@@ -116,49 +129,90 @@ module Lighthouse::GraphQL::DirectiveResolvers
116
129
  end
117
130
  end
118
131
 
119
- COLLECTION_ARGUMENTS = 'relation: String, scopes: [String!], type: PaginateType, defaultCount: Int, maxCount: Int'
132
+ PAGINATION_ARGUMENTS = [
133
+ BaseDirectiveResolver.argument_sdl(
134
+ 'type: PaginateType',
135
+ 'How to paginate the list: PAGINATOR, SIMPLE or CONNECTION. Without it, the whole list is returned.'
136
+ ),
137
+ BaseDirectiveResolver.argument_sdl(
138
+ 'defaultCount: Int',
139
+ 'The page size when the client omits `first`. Defaults to Config.pagination_default_count.'
140
+ ),
141
+ BaseDirectiveResolver.argument_sdl(
142
+ 'maxCount: Int',
143
+ 'The most records the client may ask for with `first`; null for no limit. Defaults to Config.pagination_max_count.'
144
+ )
145
+ ].freeze
120
146
 
121
147
  class HasManyDirectiveResolver < AssociationDirectiveResolver
122
148
  def self.collection? = true
123
149
  def self.directive_name = 'hasMany'
124
- def self.definition = "directive @hasMany(#{COLLECTION_ARGUMENTS}) on FIELD_DEFINITION"
150
+ DESCRIPTION = <<~TEXT.chomp
151
+ Resolve a one-to-many association of the parent (`has_many`) as a list,
152
+ batch-loaded so that listing it for many parents is not an N+1.
153
+ With `type`, the list is paginated.
154
+ TEXT
125
155
  end
126
156
 
127
157
  class BelongsToManyDirectiveResolver < AssociationDirectiveResolver
128
158
  def self.collection? = true
129
159
  def self.directive_name = 'belongsToMany'
130
- def self.definition = "directive @belongsToMany(#{COLLECTION_ARGUMENTS}) on FIELD_DEFINITION"
160
+ DESCRIPTION = <<~TEXT.chomp
161
+ Resolve a many-to-many association of the parent (`has_and_belongs_to_many`) as a list,
162
+ batch-loaded so that listing it for many parents is not an N+1.
163
+ With `type`, the list is paginated.
164
+ TEXT
131
165
  end
132
166
 
133
167
  class MorphManyDirectiveResolver < AssociationDirectiveResolver
134
168
  def self.collection? = true
135
169
  def self.directive_name = 'morphMany'
136
- def self.definition = "directive @morphMany(#{COLLECTION_ARGUMENTS}) on FIELD_DEFINITION"
170
+ DESCRIPTION = <<~TEXT.chomp
171
+ Resolve a polymorphic one-to-many association of the parent (`has_many ..., as:`) as a list,
172
+ batch-loaded so that listing it for many parents is not an N+1.
173
+ With `type`, the list is paginated.
174
+ TEXT
137
175
  end
138
176
 
139
177
  class MorphToManyDirectiveResolver < AssociationDirectiveResolver
140
178
  def self.collection? = true
141
179
  def self.directive_name = 'morphToMany'
142
- def self.definition = "directive @morphToMany(#{COLLECTION_ARGUMENTS}) on FIELD_DEFINITION"
180
+ DESCRIPTION = <<~TEXT.chomp
181
+ Resolve a polymorphic many-to-many association of the parent as a list,
182
+ batch-loaded so that listing it for many parents is not an N+1.
183
+ With `type`, the list is paginated.
184
+ TEXT
143
185
  end
144
186
 
145
187
  class HasOneDirectiveResolver < AssociationDirectiveResolver
146
188
  def self.directive_name = 'hasOne'
147
- def self.definition = 'directive @hasOne(relation: String, scopes: [String!]) on FIELD_DEFINITION'
189
+ DESCRIPTION = <<~TEXT.chomp
190
+ Resolve a one-to-one association of the parent (`has_one`),
191
+ batch-loaded so that loading it for many parents is not an N+1.
192
+ TEXT
148
193
  end
149
194
 
150
195
  class MorphOneDirectiveResolver < AssociationDirectiveResolver
151
196
  def self.directive_name = 'morphOne'
152
- def self.definition = 'directive @morphOne(relation: String, scopes: [String!]) on FIELD_DEFINITION'
197
+ DESCRIPTION = <<~TEXT.chomp
198
+ Resolve a polymorphic one-to-one association of the parent (`has_one ..., as:`),
199
+ batch-loaded so that loading it for many parents is not an N+1.
200
+ TEXT
153
201
  end
154
202
 
155
203
  class BelongsToDirectiveResolver < AssociationDirectiveResolver
156
204
  def self.directive_name = 'belongsTo'
157
- def self.definition = 'directive @belongsTo(relation: String, scopes: [String!]) on FIELD_DEFINITION'
205
+ DESCRIPTION = <<~TEXT.chomp
206
+ Resolve the inverse of a one-to-one or one-to-many association (`belongs_to`),
207
+ batch-loaded so that loading it for many parents is not an N+1.
208
+ TEXT
158
209
  end
159
210
 
160
211
  class MorphToDirectiveResolver < AssociationDirectiveResolver
161
212
  def self.directive_name = 'morphTo'
162
- def self.definition = 'directive @morphTo(relation: String, scopes: [String!]) on FIELD_DEFINITION'
213
+ DESCRIPTION = <<~TEXT.chomp
214
+ Resolve a polymorphic inverse association of the parent (`belongs_to ..., polymorphic: true`),
215
+ batch-loaded so that loading it for many parents is not an N+1.
216
+ TEXT
163
217
  end
164
218
  end
@@ -2,7 +2,16 @@
2
2
 
3
3
  class Lighthouse::GraphQL::DirectiveResolvers::AuthDirectiveResolver < Lighthouse::GraphQL::DirectiveResolvers::BaseDirectiveResolver
4
4
  def self.directive_name = 'auth'
5
- def self.definition = 'directive @auth on FIELD_DEFINITION'
5
+
6
+ def self.definition
7
+ <<~GRAPHQL.chomp
8
+ """
9
+ Require an authenticated user (`context[:current_user]`) before the field resolves,
10
+ else fail with "Unauthenticated.". @guard is the configurable equivalent.
11
+ """
12
+ directive @auth on FIELD_DEFINITION
13
+ GRAPHQL
14
+ end
6
15
 
7
16
  def define_resolver(target_type_class)
8
17
  inner = original_resolver(target_type_class)