lighthouse-graphql 0.7.1 → 0.7.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.
Files changed (40) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +22 -1
  3. data/docs/getting-started.md +44 -11
  4. data/lib/lighthouse/ast_builder.rb +4 -1
  5. data/lib/lighthouse/cache_control.rb +14 -2
  6. data/lib/lighthouse/complexity.rb +11 -1
  7. data/lib/lighthouse/directive_resolvers/aggregate_resolver.rb +17 -1
  8. data/lib/lighthouse/directive_resolvers/all_resolver.rb +13 -1
  9. data/lib/lighthouse/directive_resolvers/association_resolvers.rb +65 -9
  10. data/lib/lighthouse/directive_resolvers/auth_resolver.rb +10 -1
  11. data/lib/lighthouse/directive_resolvers/base.rb +18 -0
  12. data/lib/lighthouse/directive_resolvers/broadcast_resolver.rb +12 -1
  13. data/lib/lighthouse/directive_resolvers/cache_resolvers.rb +28 -3
  14. data/lib/lighthouse/directive_resolvers/can_resolvers.rb +62 -9
  15. data/lib/lighthouse/directive_resolvers/count_resolver.rb +16 -1
  16. data/lib/lighthouse/directive_resolvers/event_resolver.rb +13 -1
  17. data/lib/lighthouse/directive_resolvers/field_resolver.rb +6 -1
  18. data/lib/lighthouse/directive_resolvers/find_resolver.rb +13 -1
  19. data/lib/lighthouse/directive_resolvers/first_resolver.rb +13 -1
  20. data/lib/lighthouse/directive_resolvers/guard_resolver.rb +10 -1
  21. data/lib/lighthouse/directive_resolvers/method_resolver.rb +13 -1
  22. data/lib/lighthouse/directive_resolvers/model_mutation_resolver.rb +74 -8
  23. data/lib/lighthouse/directive_resolvers/paginate_resolver.rb +17 -2
  24. data/lib/lighthouse/directive_resolvers/rename_resolver.rb +14 -1
  25. data/lib/lighthouse/directive_resolvers/subscription_resolver.rb +13 -1
  26. data/lib/lighthouse/directive_resolvers/validator_resolver.rb +13 -1
  27. data/lib/lighthouse/directives/arguments/base.rb +18 -0
  28. data/lib/lighthouse/directives/arguments/filters.rb +16 -7
  29. data/lib/lighthouse/directives/definitions.rb +43 -6
  30. data/lib/lighthouse/document_ast.rb +6 -0
  31. data/lib/lighthouse/enum_directive.rb +6 -1
  32. data/lib/lighthouse/feature_directive.rb +13 -1
  33. data/lib/lighthouse/graphql/version.rb +1 -1
  34. data/lib/lighthouse/graphql_ruby_patches.rb +26 -2
  35. data/lib/lighthouse/ide_helper.rb +7 -6
  36. data/lib/lighthouse/scalar_coercion.rb +4 -1
  37. data/lib/lighthouse/tasks/lighthouse.rake +8 -0
  38. data/lib/lighthouse/type_resolution.rb +8 -2
  39. data/sig/lighthouse/graphql.rbs +1 -0
  40. metadata +1 -1
@@ -11,7 +11,12 @@ class Lighthouse::GraphQL::DirectiveResolvers::FieldDirectiveResolver < Lighthou
11
11
  Assign a resolver to a field: a class, or a class and a method separated by `@`
12
12
  ("Statistics@users_total"). Without a method, the class resolves, or is called.
13
13
  """
14
- directive @field(resolver: String, class: String) on FIELD_DEFINITION
14
+ directive @field(
15
+ "The resolver: a class, or a class and a method separated by `@`. Required unless `class` is given."
16
+ resolver: String
17
+ "Alias of `resolver`, used when `resolver` is not given."
18
+ class: String
19
+ ) on FIELD_DEFINITION
15
20
  GRAPHQL
16
21
  end
17
22
 
@@ -12,7 +12,19 @@ require_relative 'base'
12
12
  # match is an error, as in PHP Lighthouse; none returns null.
13
13
  class Lighthouse::GraphQL::DirectiveResolvers::FindDirectiveResolver < Lighthouse::GraphQL::DirectiveResolvers::BaseDirectiveResolver
14
14
  def self.directive_name = 'find'
15
- def self.definition = 'directive @find(model: String, scopes: [String!]) on FIELD_DEFINITION'
15
+
16
+ def self.definition
17
+ <<~GRAPHQL.chomp
18
+ """
19
+ Find a single record by the field's argument filters (@eq, @where, ...), or null.
20
+ More than one match is an error. A lookup by `id` alone is batched.
21
+ """
22
+ directive @find(
23
+ #{MODEL_ARGUMENT}
24
+ #{SCOPES_ARGUMENT}
25
+ ) on FIELD_DEFINITION
26
+ GRAPHQL
27
+ end
16
28
 
17
29
  def define_resolver(target_type_class)
18
30
  me = self
@@ -8,7 +8,19 @@ require_relative 'builder_applier'
8
8
  # be combined with @eq/@where to fetch one record by criteria.
9
9
  class Lighthouse::GraphQL::DirectiveResolvers::FirstDirectiveResolver < Lighthouse::GraphQL::DirectiveResolvers::BaseDirectiveResolver
10
10
  def self.directive_name = 'first'
11
- def self.definition = 'directive @first(model: String, scopes: [String!]) on FIELD_DEFINITION'
11
+
12
+ def self.definition
13
+ <<~GRAPHQL.chomp
14
+ """
15
+ Get the first record that matches the field's argument filters (@eq, @where, ...),
16
+ or null. Unlike @find, it does not fail when more than one record matches.
17
+ """
18
+ directive @first(
19
+ #{MODEL_ARGUMENT}
20
+ #{SCOPES_ARGUMENT}
21
+ ) on FIELD_DEFINITION
22
+ GRAPHQL
23
+ end
12
24
 
13
25
  def define_resolver(target_type_class)
14
26
  method_name = "resolve_field_#{@field_def.name}"
@@ -7,7 +7,16 @@ require_relative 'base'
7
7
  # present (per Config.authenticated_user). Mirrors PHP Lighthouse's @guard.
8
8
  class Lighthouse::GraphQL::DirectiveResolvers::GuardDirectiveResolver < Lighthouse::GraphQL::DirectiveResolvers::BaseDirectiveResolver
9
9
  def self.directive_name = 'guard'
10
- def self.definition = 'directive @guard on FIELD_DEFINITION'
10
+
11
+ def self.definition
12
+ <<~GRAPHQL.chomp
13
+ """
14
+ Require an authenticated user (see Config.authenticated_user) before the field resolves,
15
+ else fail with "Unauthenticated.".
16
+ """
17
+ directive @guard on FIELD_DEFINITION
18
+ GRAPHQL
19
+ end
11
20
 
12
21
  def define_resolver(target_type_class)
13
22
  field_name = @field_def.name
@@ -16,7 +16,19 @@ require_relative 'base'
16
16
  # taking no parameters is called without them.
17
17
  class Lighthouse::GraphQL::DirectiveResolvers::MethodDirectiveResolver < Lighthouse::GraphQL::DirectiveResolvers::BaseDirectiveResolver
18
18
  def self.directive_name = 'method'
19
- def self.definition = 'directive @method(name: String) on FIELD_DEFINITION'
19
+
20
+ def self.definition
21
+ <<~GRAPHQL.chomp
22
+ """
23
+ Resolve a field by calling a method on the parent object.
24
+ The field's arguments are passed to the method.
25
+ """
26
+ directive @method(
27
+ "The name of the method to call. Defaults to the field's name in snake_case."
28
+ name: String
29
+ ) on FIELD_DEFINITION
30
+ GRAPHQL
31
+ end
20
32
 
21
33
  def define_resolver(target_type_class)
22
34
  resolver_method = "resolve_field_#{@field_def.name}"
@@ -15,9 +15,31 @@ module Lighthouse::GraphQL::DirectiveResolvers
15
15
  # adds server-side values. On an argument, these directives take
16
16
  # `relation:` and act on that association of the parent instead.
17
17
  class ModelMutationResolver < Lighthouse::GraphQL::DirectiveResolvers::BaseDirectiveResolver
18
- ARGUMENTS = 'model: String, relation: String'
19
18
  LOCATIONS = 'FIELD_DEFINITION | ARGUMENT_DEFINITION | INPUT_FIELD_DEFINITION'
20
19
 
20
+ NESTED_RELATION_ARGUMENT = argument_sdl(
21
+ 'relation: String',
22
+ "On an argument or input field: the association of the parent model that its mutation acts on. Defaults to the argument's name in snake_case."
23
+ )
24
+ IDENTIFYING_COLUMNS_ARGUMENT = argument_sdl(
25
+ 'identifyingColumns: [String!]',
26
+ 'The columns whose values in the input identify an existing model to update. Defaults to the primary key. Must not be empty.'
27
+ )
28
+
29
+ # The SDL of a mutation directive: `description`, then its arguments (the model,
30
+ # the `extra` ones, then the nested relation).
31
+ def self.sdl(description, extra: [])
32
+ arguments = [MODEL_ARGUMENT, *extra, NESTED_RELATION_ARGUMENT]
33
+ <<~GRAPHQL.chomp
34
+ """
35
+ #{description}
36
+ """
37
+ directive @#{directive_name}(
38
+ #{arguments.join("\n")}
39
+ ) on #{LOCATIONS}
40
+ GRAPHQL
41
+ end
42
+
21
43
  def define_resolver(target_type_class)
22
44
  me = self
23
45
  install(target_type_class, lambda do |obj, args, ctx|
@@ -102,7 +124,14 @@ module Lighthouse::GraphQL::DirectiveResolvers
102
124
  # @create — create a model from the field's arguments.
103
125
  class CreateDirectiveResolver < ModelMutationResolver
104
126
  def self.directive_name = 'create'
105
- def self.definition = "directive @create(#{ARGUMENTS}) on #{LOCATIONS}"
127
+
128
+ def self.definition
129
+ sdl(<<~TEXT.chomp)
130
+ Create a model from the field's arguments, through ActiveRecord, so the model's validations apply.
131
+ Nested input on its associations runs as nested mutations.
132
+ On an argument or input field, it creates the record on that association of the parent.
133
+ TEXT
134
+ end
106
135
 
107
136
  protected
108
137
 
@@ -112,7 +141,13 @@ module Lighthouse::GraphQL::DirectiveResolvers
112
141
  # @createMany — create one model per input in the field's single list argument.
113
142
  class CreateManyDirectiveResolver < ModelMutationResolver
114
143
  def self.directive_name = 'createMany'
115
- def self.definition = "directive @createMany(#{ARGUMENTS}) on #{LOCATIONS}"
144
+
145
+ def self.definition
146
+ sdl(<<~TEXT.chomp)
147
+ Create many models, one per input of the field's single list argument.
148
+ If any is invalid nothing is saved, and the errors of all are reported.
149
+ TEXT
150
+ end
116
151
 
117
152
  protected
118
153
 
@@ -124,7 +159,14 @@ module Lighthouse::GraphQL::DirectiveResolvers
124
159
  # @update — find a model by its primary key argument and update it.
125
160
  class UpdateDirectiveResolver < ModelMutationResolver
126
161
  def self.directive_name = 'update'
127
- def self.definition = "directive @update(#{ARGUMENTS}) on #{LOCATIONS}"
162
+
163
+ def self.definition
164
+ sdl(<<~TEXT.chomp)
165
+ Update a model found by the primary key in the field's arguments, through ActiveRecord,
166
+ so the model's validations apply. Nested input on its associations runs as nested mutations.
167
+ On an argument or input field, it updates the record on that association of the parent.
168
+ TEXT
169
+ end
128
170
 
129
171
  protected
130
172
 
@@ -134,7 +176,13 @@ module Lighthouse::GraphQL::DirectiveResolvers
134
176
  # @updateMany — update one model per input (each carrying its primary key).
135
177
  class UpdateManyDirectiveResolver < ModelMutationResolver
136
178
  def self.directive_name = 'updateMany'
137
- def self.definition = "directive @updateMany(#{ARGUMENTS}) on #{LOCATIONS}"
179
+
180
+ def self.definition
181
+ sdl(<<~TEXT.chomp)
182
+ Update many models, one per input of the field's single list argument, each carrying its primary key.
183
+ If any is invalid nothing is saved, and the errors of all are reported.
184
+ TEXT
185
+ end
138
186
 
139
187
  protected
140
188
 
@@ -147,7 +195,13 @@ module Lighthouse::GraphQL::DirectiveResolvers
147
195
  # else primary key), or create it.
148
196
  class UpsertDirectiveResolver < ModelMutationResolver
149
197
  def self.directive_name = 'upsert'
150
- def self.definition = "directive @upsert(#{ARGUMENTS}, identifyingColumns: [String!]) on #{LOCATIONS}"
198
+
199
+ def self.definition
200
+ sdl(<<~TEXT.chomp, extra: [IDENTIFYING_COLUMNS_ARGUMENT])
201
+ Update the model the arguments identify (by `identifyingColumns`, else the primary key), or create it.
202
+ Saved through ActiveRecord, so the model's validations apply.
203
+ TEXT
204
+ end
151
205
 
152
206
  def define_resolver(target_type_class)
153
207
  validate_identifying_columns!
@@ -165,7 +219,13 @@ module Lighthouse::GraphQL::DirectiveResolvers
165
219
  # @upsertMany — @upsert for each input in the field's single list argument.
166
220
  class UpsertManyDirectiveResolver < ModelMutationResolver
167
221
  def self.directive_name = 'upsertMany'
168
- def self.definition = "directive @upsertMany(#{ARGUMENTS}, identifyingColumns: [String!]) on #{LOCATIONS}"
222
+
223
+ def self.definition
224
+ sdl(<<~TEXT.chomp, extra: [IDENTIFYING_COLUMNS_ARGUMENT])
225
+ Upsert many models, one per input of the field's single list argument.
226
+ If any is invalid nothing is saved, and the errors of all are reported.
227
+ TEXT
228
+ end
169
229
 
170
230
  def define_resolver(target_type_class)
171
231
  validate_identifying_columns!
@@ -225,7 +285,13 @@ module Lighthouse::GraphQL::DirectiveResolvers
225
285
  # (@whereKey, @eq, ...), running their callbacks, and return them.
226
286
  class DeleteDirectiveResolver < ModifyModelsResolver
227
287
  def self.directive_name = 'delete'
228
- def self.definition = "directive @delete(#{ARGUMENTS}, scopes: [String!]) on #{LOCATIONS}"
288
+
289
+ def self.definition
290
+ sdl(<<~TEXT.chomp, extra: [SCOPES_ARGUMENT])
291
+ Delete the models the field's argument filters (@whereKey, @eq, ...) select, running their callbacks,
292
+ and return them: a list for a list field, else the first or null. Refuses to run without a filter argument.
293
+ TEXT
294
+ end
229
295
 
230
296
  protected
231
297
 
@@ -18,8 +18,23 @@ class Lighthouse::GraphQL::DirectiveResolvers::PaginateDirectiveResolver < Light
18
18
  def self.directive_name = 'paginate'
19
19
 
20
20
  def self.definition
21
- 'directive @paginate(type: PaginateType = PAGINATOR, model: String, scopes: [String!], ' \
22
- 'defaultCount: Int, maxCount: Int) on FIELD_DEFINITION'
21
+ <<~GRAPHQL.chomp
22
+ """
23
+ Query multiple model entries as a paginated list.
24
+ The query is the model's, narrowed by `scopes`, @builder and the field's argument filters
25
+ (@eq, @where, @orderBy, ...). On a nested field, it pages the parent's association of the same name.
26
+ """
27
+ directive @paginate(
28
+ "Which pagination style to use."
29
+ type: PaginateType = PAGINATOR
30
+ #{MODEL_ARGUMENT}
31
+ #{SCOPES_ARGUMENT}
32
+ "The page size when the client omits `first`. Defaults to Config.pagination_default_count."
33
+ defaultCount: Int
34
+ "The most records the client may ask for with `first`; null for no limit. Defaults to Config.pagination_max_count."
35
+ maxCount: Int
36
+ ) on FIELD_DEFINITION
37
+ GRAPHQL
23
38
  end
24
39
 
25
40
  def self.type_definitions
@@ -7,7 +7,20 @@ require_relative 'base'
7
7
  # of the GraphQL field name.
8
8
  class Lighthouse::GraphQL::DirectiveResolvers::RenameDirectiveResolver < Lighthouse::GraphQL::DirectiveResolvers::BaseDirectiveResolver
9
9
  def self.directive_name = 'rename'
10
- def self.definition = 'directive @rename(attribute: String!) on FIELD_DEFINITION | ARGUMENT_DEFINITION | INPUT_FIELD_DEFINITION'
10
+
11
+ def self.definition
12
+ <<~GRAPHQL.chomp
13
+ """
14
+ Use another attribute than the name of the field, argument or input field.
15
+ On a field, its value is read from that attribute of the parent object;
16
+ on an argument, @create and @update write it to that attribute of the model.
17
+ """
18
+ directive @rename(
19
+ "The name of the attribute, in the model or the parent object."
20
+ attribute: String!
21
+ ) on FIELD_DEFINITION | ARGUMENT_DEFINITION | INPUT_FIELD_DEFINITION
22
+ GRAPHQL
23
+ end
11
24
 
12
25
  def define_resolver(target_type_class)
13
26
  method_name = "resolve_field_#{@field_def.name}"
@@ -20,7 +20,19 @@ require_relative 'base'
20
20
  # each request, so its hooks (`subscribe`, `update`, ...) run as usual.
21
21
  class Lighthouse::GraphQL::DirectiveResolvers::SubscriptionDirectiveResolver < Lighthouse::GraphQL::DirectiveResolvers::BaseDirectiveResolver
22
22
  def self.directive_name = 'subscription'
23
- def self.definition = 'directive @subscription(class: String!) on FIELD_DEFINITION'
23
+
24
+ def self.definition
25
+ <<~GRAPHQL.chomp
26
+ """
27
+ Reference a class to handle a field of the Subscription type: a GraphQL::Schema::Subscription.
28
+ Without it, the field resolves to the class named after it, in Config.namespaces[:subscriptions].
29
+ """
30
+ directive @subscription(
31
+ "The subscription class, looked up in Config.namespaces[:subscriptions]. A fully-qualified name is used as-is."
32
+ class: String!
33
+ ) on FIELD_DEFINITION
34
+ GRAPHQL
35
+ end
24
36
 
25
37
  def define_resolver(target_type_class)
26
38
  unless @opts[:root_kind] == :subscriptions
@@ -25,7 +25,19 @@ require_relative 'base'
25
25
  # across arguments, arguments of a query, or input that never reaches a model.
26
26
  class Lighthouse::GraphQL::DirectiveResolvers::ValidatorDirectiveResolver < Lighthouse::GraphQL::DirectiveResolvers::BaseDirectiveResolver
27
27
  def self.directive_name = 'validator'
28
- def self.definition = 'directive @validator(class: String) on FIELD_DEFINITION | INPUT_OBJECT'
28
+
29
+ def self.definition
30
+ <<~GRAPHQL.chomp
31
+ """
32
+ Validate the arguments of a field, or the values of an input type, with a contract class
33
+ before the field resolves. Failures are reported keyed by argument path.
34
+ """
35
+ directive @validator(
36
+ "The contract class, looked up in Config.namespaces[:validators]. Defaults to Validators::<ParentType>::<FieldName>Validator on a field, and to Validators::<InputType>Validator on an input type."
37
+ class: String
38
+ ) on FIELD_DEFINITION | INPUT_OBJECT
39
+ GRAPHQL
40
+ end
29
41
 
30
42
  # A default name, e.g. Mutation::SignUpValidator: always looked up under
31
43
  # the validators namespaces, although it contains `::`.
@@ -7,6 +7,24 @@
7
7
  class Lighthouse::GraphQL::Directives::Arguments::Base
8
8
  include Lighthouse::Contracts::ArgBuilder
9
9
 
10
+ # The `key` argument's SDL, which most of them take.
11
+ KEY_ARGUMENT = Lighthouse::GraphQL::DirectiveResolvers::BaseDirectiveResolver.argument_sdl(
12
+ 'key: String', "The column to filter on. Defaults to the argument's name in snake_case."
13
+ )
14
+
15
+ def self.argument_sdl(...) = Lighthouse::GraphQL::DirectiveResolvers::BaseDirectiveResolver.argument_sdl(...)
16
+
17
+ # The SDL of a filter directive: `description`, then its arguments (`key` by default).
18
+ def self.sdl(name, description, arguments = KEY_ARGUMENT)
19
+ parentheses = "(\n#{arguments}\n)" if arguments
20
+ <<~GRAPHQL.chomp
21
+ """
22
+ #{description}
23
+ """
24
+ directive @#{name}#{parentheses} on ARGUMENT_DEFINITION | INPUT_FIELD_DEFINITION
25
+ GRAPHQL
26
+ end
27
+
10
28
  def initialize(arg_node, directive_node)
11
29
  @arg_node = arg_node
12
30
  @directive_node = directive_node
@@ -6,7 +6,7 @@ module Lighthouse::GraphQL::Directives::Arguments
6
6
  # @eq — equality filter (the default). `field(status: String @eq)`
7
7
  class Eq < Base
8
8
  def self.directive_name = 'eq'
9
- def self.definition = 'directive @eq(key: String) on ARGUMENT_DEFINITION | INPUT_FIELD_DEFINITION'
9
+ def self.definition = sdl('eq', 'Add an equality filter to the query: the column equals the value.')
10
10
 
11
11
  def handle_builder(relation, value)
12
12
  return relation if value.nil?
@@ -19,7 +19,7 @@ module Lighthouse::GraphQL::Directives::Arguments
19
19
  # named; a list matches any of its values. `deletePosts(ids: [ID!]! @whereKey)`
20
20
  class WhereKey < Base
21
21
  def self.directive_name = 'whereKey'
22
- def self.definition = 'directive @whereKey on ARGUMENT_DEFINITION | INPUT_FIELD_DEFINITION'
22
+ def self.definition = sdl('whereKey', "Filter by the model's primary key, whatever its column is named. A list matches any of its values.", nil)
23
23
 
24
24
  def handle_builder(relation, value)
25
25
  return relation if value.nil?
@@ -31,7 +31,7 @@ module Lighthouse::GraphQL::Directives::Arguments
31
31
  # @neq — inequality filter.
32
32
  class Neq < Base
33
33
  def self.directive_name = 'neq'
34
- def self.definition = 'directive @neq(key: String) on ARGUMENT_DEFINITION | INPUT_FIELD_DEFINITION'
34
+ def self.definition = sdl('neq', 'Add an inequality filter to the query: the column differs from the value.')
35
35
 
36
36
  def handle_builder(relation, value)
37
37
  return relation if value.nil?
@@ -43,7 +43,7 @@ module Lighthouse::GraphQL::Directives::Arguments
43
43
  # @in — value is one of a list.
44
44
  class In < Base
45
45
  def self.directive_name = 'in'
46
- def self.definition = 'directive @in(key: String) on ARGUMENT_DEFINITION | INPUT_FIELD_DEFINITION'
46
+ def self.definition = sdl('in', 'Filter by a list of values: the column is one of them.')
47
47
 
48
48
  def handle_builder(relation, value)
49
49
  return relation if value.nil?
@@ -55,7 +55,7 @@ module Lighthouse::GraphQL::Directives::Arguments
55
55
  # @notIn — value is none of a list.
56
56
  class NotIn < Base
57
57
  def self.directive_name = 'notIn'
58
- def self.definition = 'directive @notIn(key: String) on ARGUMENT_DEFINITION | INPUT_FIELD_DEFINITION'
58
+ def self.definition = sdl('notIn', 'Filter by a list of values: the column is none of them.')
59
59
 
60
60
  def handle_builder(relation, value)
61
61
  return relation if value.nil?
@@ -67,7 +67,7 @@ module Lighthouse::GraphQL::Directives::Arguments
67
67
  # @like — SQL LIKE with the value wrapped in wildcards.
68
68
  class Like < Base
69
69
  def self.directive_name = 'like'
70
- def self.definition = 'directive @like(key: String) on ARGUMENT_DEFINITION | INPUT_FIELD_DEFINITION'
70
+ def self.definition = sdl('like', 'Filter with SQL LIKE: the column contains the value, which is wrapped in wildcards.')
71
71
 
72
72
  def handle_builder(relation, value)
73
73
  return relation if value.nil?
@@ -80,7 +80,16 @@ module Lighthouse::GraphQL::Directives::Arguments
80
80
  # the @whereConditions extension. `field(age: Int @where(operator: "GT"))`
81
81
  class Where < Base
82
82
  def self.directive_name = 'where'
83
- def self.definition = 'directive @where(key: String, operator: String) on ARGUMENT_DEFINITION | INPUT_FIELD_DEFINITION'
83
+
84
+ def self.definition
85
+ sdl('where', 'Add a where condition to the query: the column compares to the value with an operator.',
86
+ "#{KEY_ARGUMENT}\n#{OPERATOR_ARGUMENT}")
87
+ end
88
+
89
+ OPERATOR_ARGUMENT = argument_sdl(
90
+ 'operator: String',
91
+ 'The comparison: EQ (default), NEQ, GT, GTE, LT, LTE, IN, NIN, CONTAINS, STARTS_WITH, ENDS_WITH, IS_NULL or IS_NOT_NULL.'
92
+ )
84
93
 
85
94
  # LIKE pattern of each wildcard operator, built from the client value.
86
95
  LIKE_PATTERNS = {
@@ -8,10 +8,38 @@
8
8
  module Lighthouse::GraphQL::Directives::Definitions
9
9
  # Directives that only mark the SDL for others to read.
10
10
  PASSIVE = {
11
- 'nest' => 'directive @nest on ARGUMENT_DEFINITION | INPUT_FIELD_DEFINITION',
12
- 'spread' => 'directive @spread on ARGUMENT_DEFINITION | INPUT_FIELD_DEFINITION',
13
- 'inject' => 'directive @inject(context: String!, name: String!) repeatable on FIELD_DEFINITION',
14
- 'cacheKey' => 'directive @cacheKey on FIELD_DEFINITION'
11
+ 'nest' => <<~GRAPHQL.chomp,
12
+ """
13
+ On an argument of an input object type, apply the filters of the input's fields
14
+ (@eq, @where, ...; at any depth, for those that carry @nest too) as if they were the field's own arguments.
15
+ """
16
+ directive @nest on ARGUMENT_DEFINITION | INPUT_FIELD_DEFINITION
17
+ GRAPHQL
18
+ 'spread' => <<~GRAPHQL.chomp,
19
+ """
20
+ On an argument of an input object type, merge the input's fields into the arguments
21
+ the field's resolver or mutation receives, as if they were the field's own.
22
+ """
23
+ directive @spread on ARGUMENT_DEFINITION | INPUT_FIELD_DEFINITION
24
+ GRAPHQL
25
+ 'inject' => <<~GRAPHQL.chomp,
26
+ """
27
+ Set an argument from the request's context, on the server, so clients cannot set it.
28
+ """
29
+ directive @inject(
30
+ "A path in the context, with dots: \\"current_user.id\\"."
31
+ context: String!
32
+ "The name of the argument to set, with dots for a nested one: \\"input.author_id\\"."
33
+ name: String!
34
+ ) repeatable on FIELD_DEFINITION
35
+ GRAPHQL
36
+ 'cacheKey' => <<~GRAPHQL.chomp
37
+ """
38
+ Mark the field whose value identifies its object in the keys of @cache and @clearCache.
39
+ Without one, the first field of type ID is used.
40
+ """
41
+ directive @cacheKey on FIELD_DEFINITION
42
+ GRAPHQL
15
43
  }.freeze
16
44
 
17
45
  module_function
@@ -38,8 +66,17 @@ module Lighthouse::GraphQL::Directives::Definitions
38
66
  # @hasManyThrough and @hasOneThrough: @hasMany's and @hasOne's definitions, renamed.
39
67
  def through_aliases
40
68
  resolvers = Lighthouse::GraphQL::DirectiveResolvers
41
- { 'hasManyThrough' => resolvers::HasManyDirectiveResolver, 'hasOneThrough' => resolvers::HasOneDirectiveResolver }
42
- .to_h { |name, klass| [name, klass.definition.sub("@#{klass.directive_name}(", "@#{name}(")] }
69
+ {
70
+ 'hasManyThrough' => [resolvers::HasManyDirectiveResolver, <<~TEXT.chomp],
71
+ Resolve a one-to-many association of the parent that goes through another (`has_many :through`)
72
+ as a list, batch-loaded so that listing it for many parents is not an N+1.
73
+ With `type`, the list is paginated.
74
+ TEXT
75
+ 'hasOneThrough' => [resolvers::HasOneDirectiveResolver, <<~TEXT.chomp]
76
+ Resolve a one-to-one association of the parent that goes through another (`has_one :through`),
77
+ batch-loaded so that loading it for many parents is not an N+1.
78
+ TEXT
79
+ }.to_h { |name, (klass, description)| [name, klass.definition(name, description)] }
43
80
  end
44
81
 
45
82
  # { type name => its SDL }, for the types the directives above refer to.
@@ -67,6 +67,12 @@ class Lighthouse::GraphQL::DocumentAST
67
67
  self
68
68
  end
69
69
 
70
+ # Drops the type of that name, if any.
71
+ def remove_type(name)
72
+ @types.delete(name)
73
+ self
74
+ end
75
+
70
76
  # Adds the type parsed from `sdl`, unless the document has a type of that name.
71
77
  def define_type(name, sdl = nil)
72
78
  return @types[name] if @types.key?(name)
@@ -24,7 +24,12 @@ module Lighthouse::GraphQL::EnumDirective
24
24
  ) on ENUM_VALUE
25
25
  GRAPHQL
26
26
 
27
- TYPE_DEFINITIONS = { 'Mixed' => 'scalar Mixed' }.freeze
27
+ TYPE_DEFINITIONS = {
28
+ 'Mixed' => <<~GRAPHQL.chomp
29
+ "Loose type that allows any value. Be careful when passing in large JSON objects, as it may result in poor performance."
30
+ scalar Mixed
31
+ GRAPHQL
32
+ }.freeze
28
33
 
29
34
  module_function
30
35
 
@@ -22,7 +22,19 @@ module Lighthouse::GraphQL::FeatureDirective
22
22
  # Arguments with a default are nullable, not `Type! = default` as in PHP: graphql-ruby
23
23
  # 2.5 does not fill in a directive argument's default, so a non-null one would be
24
24
  # refused wherever it is left out. Left out or null, it takes its default.
25
- DEFINITION = 'directive @feature(name: String!, when: FeatureState = ACTIVE) on FIELD_DEFINITION | OBJECT'
25
+ DEFINITION = <<~GRAPHQL.chomp
26
+ """
27
+ Include a field or an object type in the schema only while a feature flag is in a given state,
28
+ for the actor of the request (Flipper by default, see Config.feature_checker).
29
+ Otherwise, it is hidden from introspection and a query selecting it is invalid.
30
+ """
31
+ directive @feature(
32
+ "The name of the feature flag."
33
+ name: String!
34
+ "The state the feature must be in for the element to be visible."
35
+ when: FeatureState = ACTIVE
36
+ ) on FIELD_DEFINITION | OBJECT
37
+ GRAPHQL
26
38
 
27
39
  TYPE_DEFINITIONS = {
28
40
  'FeatureState' => <<~GRAPHQL.chomp
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Lighthouse
4
4
  module GraphQL
5
- VERSION = '0.7.1'
5
+ VERSION = '0.7.2'
6
6
  end
7
7
  end
@@ -6,7 +6,8 @@ module Lighthouse::GraphQL
6
6
  # of nil, and building the schema then fails to coerce it. Its own
7
7
  # build_default_value already maps NullValue to nil; do the same here.
8
8
  module NullDirectiveArguments
9
- def args_to_kwargs(arg_owner, node)
9
+ # graphql 2.6.14 added a third parameter (the type resolver): pass on whatever it takes.
10
+ def args_to_kwargs(arg_owner, node, *rest)
10
11
  node.is_a?(::GraphQL::Language::Nodes::NullValue) ? nil : super
11
12
  end
12
13
  end
@@ -29,7 +30,28 @@ module Lighthouse::GraphQL
29
30
  end
30
31
 
31
32
  def build_interface_type(definition, type_resolver, base_type)
32
- Lighthouse::GraphQL::RootTypes.base_for(definition.name) || super
33
+ root = Lighthouse::GraphQL::RootTypes.base_for(definition.name)
34
+ return super unless root
35
+
36
+ type_resolver.register(root) if type_resolver.respond_to?(:register)
37
+ root
38
+ end
39
+ end
40
+
41
+ # graphql 2.6.14 makes the class of every type before any is built, on
42
+ # base_types, and builds into it, whatever base build_object_type is given:
43
+ # seed the root types' with the app's (`Types::QueryType`, `Types::NodeType`).
44
+ module RootTypeClasses
45
+ def initialize(lookup_hash, directives, queued_directives, type_classes, &block)
46
+ super
47
+ # `keys`: the values change while it goes over them.
48
+ type_classes.keys.each do |name|
49
+ root = Lighthouse::GraphQL::RootTypes.base_for(name)
50
+ current = type_classes[name]
51
+ next if root.nil? || (current.is_a?(Class) ? current < root : current.equal?(root))
52
+
53
+ type_classes[name] = root.is_a?(Class) ? Class.new(root) { graphql_name(name) } : root
54
+ end
33
55
  end
34
56
  end
35
57
 
@@ -37,6 +59,8 @@ module Lighthouse::GraphQL
37
59
  # root types are then built on base_types[:object], as before.
38
60
  if builder && %i[build_object_type build_interface_type].all? { |name| builder.method_defined?(name) || builder.private_method_defined?(name) }
39
61
  builder.prepend(RootTypeBuilder)
62
+ resolver = builder.const_get(:TypeResolver) if builder.const_defined?(:TypeResolver)
63
+ resolver.prepend(RootTypeClasses) if resolver&.method_defined?(:interface_module)
40
64
  end
41
65
 
42
66
  # The app's schema class inherits Lighthouse's (`class AppSchema <
@@ -78,9 +78,8 @@ module Lighthouse::IdeHelper
78
78
  end
79
79
 
80
80
  def placeholder_only(sdl)
81
- declared = sdl.to_s.match?(/^\s*scalar\s+_\b/) ? Set['_'] : Set.new
82
81
  render('The schema does not build: only the `_` placeholder is declared until it does.',
83
- undeclared_placeholder?(sdl, declared) ? [PLACEHOLDER_SCALAR] : [])
82
+ placeholder_declared?(sdl) ? [] : [PLACEHOLDER_SCALAR])
84
83
  end
85
84
 
86
85
  # Config.ide_helper's refresh: the files in the Rails app's root, when the
@@ -110,7 +109,7 @@ module Lighthouse::IdeHelper
110
109
  def programmatic_types_sdl(sdl, registry: Lighthouse::DirectiveRegistry.default, directives: directives_sdl(sdl, registry: registry))
111
110
  declared = type_names(sdl) | type_names(directives)
112
111
  generated_sdl = generated_types(sdl, declared, registry).sort_by(&:name).map(&:to_query_string)
113
- generated_sdl.unshift(PLACEHOLDER_SCALAR) if undeclared_placeholder?(sdl, declared)
112
+ generated_sdl.unshift(PLACEHOLDER_SCALAR) if declared.exclude?('_')
114
113
 
115
114
  render(
116
115
  'Types the manipulation phase generates (Connection/Paginator types, whereConditions inputs, ...).',
@@ -128,9 +127,11 @@ module Lighthouse::IdeHelper
128
127
 
129
128
  # The manipulation phase replaces the `_` placeholder (`where: _ @whereConditions`,
130
129
  # `orderBy: _ @orderBy`), but the IDE reads the SDL as written, so it needs
131
- # the placeholder declared.
132
- def undeclared_placeholder?(sdl, declared)
133
- sdl.to_s.match?(/:\s*\[?\s*_\b/) && declared.exclude?('_')
130
+ # the placeholder declared. It is always declared, whether or not the SDL
131
+ # uses it yet, so the first `_` an author writes is not an error; only an SDL
132
+ # that declares it itself is left alone.
133
+ def placeholder_declared?(sdl)
134
+ sdl.to_s.match?(/^\s*scalar\s+_\b/)
134
135
  end
135
136
 
136
137
  def type_names(sdl)
@@ -21,7 +21,10 @@ module Lighthouse::GraphQL::ScalarCoercion
21
21
  The class that reads and writes this scalar's values: coerce_input(value, context) and
22
22
  coerce_result(value, context), as a GraphQL::Schema::Scalar.
23
23
  """
24
- directive @scalar(class: String!) on SCALAR
24
+ directive @scalar(
25
+ "The scalar class, looked up in Config.namespaces[:types]. A fully-qualified name is used as-is."
26
+ class: String!
27
+ ) on SCALAR
25
28
  GRAPHQL
26
29
 
27
30
  module_function