lighthouse-graphql 0.8.1 → 0.8.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 9256c2a7c721e495cf5c3754da6b1f684a1d15822014435150c08612af4964e6
4
- data.tar.gz: 8acfd179e8ede63d8d9f7addfb85e5472435078e9f389ca684d543203353ef88
3
+ metadata.gz: b2ab527dcb3edd5db81b2cbd6c9bbec9c3e57a563ef226ec45ed4f44aab44f40
4
+ data.tar.gz: e3965b3baa2fc998bce3ecbfafeaf7de724d5715957f35384e63cdbb484df5dd
5
5
  SHA512:
6
- metadata.gz: 066e474500d83f2c5d8809275819a96941fa4e303bb3d2984a8652a7e54f2bf08e0259ef40f7ddbf8105eb5dc40485588c1e760c368c7cd9205823e44fc1c50a
7
- data.tar.gz: 013fffcb17f486024bcb94f3ba347973326bbbaf1cfdfac3b2d7ba9be90a808e64c21d37d0eac5c74d4c552f8b5ed74b40c0689715a6041ae5d232de3560bee4
6
+ metadata.gz: 436960e077273eef766bcd648ba709d16eaf263e12bb76c5447c730353822c8e3d334bf214bc252bcc01af3a23234b81987ef29d8e103c5f1f85d6c28abf1ea6
7
+ data.tar.gz: 8008508abc330c3fffb6725655bd445af480812b17d953db62fbb6f0aefa61700d63b46fc88171cfd74ad11536ee5e93bcfd598370453f0fd18ce012b14baff2
data/CHANGELOG.md CHANGED
@@ -6,6 +6,19 @@ project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
6
6
 
7
7
  ## [Unreleased]
8
8
 
9
+ ## [0.8.2] - 2026-10-30
10
+
11
+ ### Fixed
12
+
13
+ - **`@globalId(decode: ID)` checks the type of the id** (#6). It kept the id and dropped the
14
+ type, so another type's global id found the record with the same number:
15
+ `user(id: <a Post's id>)` answered user 1, and `@delete` / `@forceDelete` could act on a
16
+ record the client did not name. An argument that filters the field's model by its key
17
+ (`@whereKey`, `@eq`/`@in` on the primary key, or `id`), on the field or in an input it
18
+ takes, now fails with `"…" is not a valid global id for User.` given another type's id;
19
+ a subclass (STI) or a type whose `@model` is the field's model passes. The new
20
+ `@globalId(type: "User")` names the type anywhere else. PHP Lighthouse does not check.
21
+
9
22
  ## [0.8.1] - 2026-10-30
10
23
 
11
24
  ### Changed
@@ -853,7 +866,8 @@ had been running inside a private monorepo as a path gem before being extracted.
853
866
  - Config-driven extension seams: `Lighthouse.configure`, `resolver_namespaces`,
854
867
  `on_error`, and `DirectiveRegistry.register` for app-defined directives.
855
868
 
856
- [Unreleased]: https://github.com/Vaz-Innovation/lighthouse-graphql/compare/v0.8.1...HEAD
869
+ [Unreleased]: https://github.com/Vaz-Innovation/lighthouse-graphql/compare/v0.8.2...HEAD
870
+ [0.8.2]: https://github.com/Vaz-Innovation/lighthouse-graphql/compare/v0.8.1...v0.8.2
857
871
  [0.8.1]: https://github.com/Vaz-Innovation/lighthouse-graphql/compare/v0.8.0...v0.8.1
858
872
  [0.8.0]: https://github.com/Vaz-Innovation/lighthouse-graphql/compare/v0.7.3...v0.8.0
859
873
  [0.7.3]: https://github.com/Vaz-Innovation/lighthouse-graphql/compare/v0.7.2...v0.7.3
data/docs/directives.md CHANGED
@@ -286,7 +286,10 @@ type User {
286
286
  ### `@globalId` and `@node`
287
287
 
288
288
  `@globalId` encodes a field's value as a Relay global id (base64 of `Type:id`), and
289
- decodes an argument or input field (`decode: ARRAY | TYPE | ID`). `@node` on a type
289
+ decodes an argument or input field (`decode: ARRAY | TYPE | ID`). A decoded id must be
290
+ of the type it is for: `type:` names it, and with `decode: ID` an argument filtering
291
+ the field's model by its key is checked against that model, so another type's id
292
+ fails the field ([relay.md](relay.md#the-type-of-a-decoded-id)). `@node` on a type
290
293
  makes it implement `Node` and adds `Query.node(id:)`, which finds the type's model
291
294
  record, or calls `@node(resolver:)`. See [relay.md](relay.md).
292
295
 
data/docs/relay.md CHANGED
@@ -47,6 +47,33 @@ type Mutation {
47
47
  Lists of ids decode item by item. A value that is not a global id makes the field
48
48
  fail with `"…" is not a valid global id.`
49
49
 
50
+ ### The type of a decoded id
51
+
52
+ Relay ids are unique across types, so a decoded id is checked to be of the type it is
53
+ for: another type's id makes the field fail with `"…" is not a valid global id for
54
+ User.`, where it would find the record that happens to have the same number (a
55
+ `Post`'s id finding user 1, or `@delete` removing it). PHP Lighthouse does not check.
56
+
57
+ - **Inferred** with `decode: ID`, when the argument filters the field's model by its
58
+ key: `@whereKey`, an `@eq` or `@in` on the primary key, or an argument named after
59
+ it (`id`). That is an argument of the field, or a field of an input one of its
60
+ arguments takes (`user(by: UserBy!)`), unless that argument is an association of
61
+ the model. The field's model is its `model:`, else its return type's.
62
+ - **Explicit** with `type:`, a GraphQL type or model name, for any other argument and
63
+ any `decode:`:
64
+
65
+ ```graphql
66
+ type Query {
67
+ user(id: ID! @globalId(decode: ID) @eq): User @find # a User's id, inferred
68
+ posts(authorId: ID! @globalId(decode: ID, type: "User") @eq(key: "author_id")): [Post!]! @all
69
+ }
70
+ ```
71
+
72
+ An id of a subclass (STI) passes, and so does the id of a type whose
73
+ [`@model`](directives.md#model) is the field's model. `authorId` above, without
74
+ `type:`, is not checked: it holds another model's id, as the inputs of nested
75
+ mutations do.
76
+
50
77
  ## Nodes
51
78
 
52
79
  `@node` registers a type for refetching:
@@ -160,8 +160,13 @@ class Lighthouse::GraphQL::DirectiveResolvers::ArgumentTransformsResolver < Ligh
160
160
  send(TRANSFORMS.fetch(directive.name), directive, argument, value, ctx)
161
161
  end
162
162
 
163
- def decode_global_id(directive, _argument, value, ctx)
164
- decode(value, Lighthouse::Support::DirectiveArgs.literal(directive, 'decode') || 'ARRAY', ctx)
163
+ def decode_global_id(directive, argument, value, ctx)
164
+ mode = Lighthouse::Support::DirectiveArgs.literal(directive, 'decode') || 'ARRAY'
165
+ decode(value, mode, ctx, global_id_types.model(directive, argument, mode, ctx))
166
+ end
167
+
168
+ def global_id_types
169
+ @global_id_types ||= Lighthouse::GraphQL::DirectiveResolvers::GlobalIdTypes.new(self, @field_def)
165
170
  end
166
171
 
167
172
  def trim_value(_directive, _argument, value, _ctx) = trim(value)
@@ -175,10 +180,15 @@ class Lighthouse::GraphQL::DirectiveResolvers::ArgumentTransformsResolver < Ligh
175
180
  Lighthouse::GraphQL::UploadStore.call(value, argument: argument.graphql_name, **options)
176
181
  end
177
182
 
178
- def decode(value, mode, ctx = nil)
179
- return value.map { |item| decode(item, mode, ctx) } if value.is_a?(Array)
183
+ # `model`: the class the id must be of, if any (GlobalIdTypes).
184
+ def decode(value, mode, ctx = nil, model = nil)
185
+ return value.map { |item| decode(item, mode, ctx, model) } if value.is_a?(Array)
180
186
 
181
187
  type, id = Lighthouse::GraphQL::GlobalId.decode!(value, ctx)
188
+ if model && !Lighthouse::GraphQL::GlobalId.of_model?(value, type, model, ctx)
189
+ raise ::GraphQL::ExecutionError, "#{value.inspect} is not a valid global id for #{model.name}."
190
+ end
191
+
182
192
  { 'TYPE' => type, 'ID' => id }.fetch(mode.to_s, [type, id])
183
193
  end
184
194
 
@@ -6,6 +6,10 @@ require_relative 'base'
6
6
  # encodes the value with the type's name (`id: ID! @globalId` answers
7
7
  # "VXNlcjox" for User 1); on an argument or input field it decodes what the
8
8
  # client sent (ArgumentTransformsResolver). Config.global_id does the encoding.
9
+ # Unlike PHP Lighthouse's, a decoded id is checked to be of the type it is
10
+ # for: `type:`, or with decode: ID the field's model when the argument filters
11
+ # its key (GlobalIdTypes), so another type's id fails rather than finds the
12
+ # record of the same number.
9
13
  class Lighthouse::GraphQL::DirectiveResolvers::GlobalIdDirectiveResolver < Lighthouse::GraphQL::DirectiveResolvers::BaseDirectiveResolver
10
14
  NAME = 'globalId'
11
15
 
@@ -25,6 +29,8 @@ class Lighthouse::GraphQL::DirectiveResolvers::GlobalIdDirectiveResolver < Light
25
29
  This setting controls which of those is passed along.
26
30
  """
27
31
  decode: GlobalIdDecode = ARRAY
32
+ "The type (GraphQL type or model) a decoded id must be of. Inferred for a key of the field's model."
33
+ type: String
28
34
  ) on FIELD_DEFINITION | INPUT_FIELD_DEFINITION | ARGUMENT_DEFINITION
29
35
  GRAPHQL
30
36
  end
@@ -0,0 +1,84 @@
1
+ # frozen_string_literal: true
2
+
3
+ # The model a field's @globalId arguments decode ids of, so another type's
4
+ # id fails the field instead of finding the record with the same number
5
+ # (`user(id: <a Post's id>)`). PHP Lighthouse checks nothing.
6
+ #
7
+ # - `@globalId(type: "User")`: that type (a GraphQL type or model name), any decode mode.
8
+ # - With decode: ID and no type:, the field's model (its `model:`, else its
9
+ # return type's), for an argument that filters that model by its key: one
10
+ # with @whereKey, an @eq / @in on the primary key, or one named after it
11
+ # (`id`). That is a field's argument, or a field of an input object one of
12
+ # its arguments takes, unless that argument is an association of the model
13
+ # (whose own ids it holds). Elsewhere (`authorId`, the inputs of nested
14
+ # mutations) the id is of another model, which `type:` names.
15
+ #
16
+ # Models are looked up per call, so code reloading works.
17
+ class Lighthouse::GraphQL::DirectiveResolvers::GlobalIdTypes
18
+ KEY_FILTERS = %w[eq in].freeze
19
+
20
+ def initialize(resolver, field_def)
21
+ @resolver = resolver
22
+ @keys = key_arguments(field_def)
23
+ end
24
+
25
+ # The class the ids `argument` decodes must be of; nil: any.
26
+ def model(directive, argument, mode, ctx)
27
+ explicit = Lighthouse::Support::DirectiveArgs.literal(directive, 'type')
28
+ return explicit_model(explicit, ctx) if explicit
29
+ return unless mode.to_s == 'ID' && @keys.key?(argument)
30
+
31
+ column, parent = @keys[argument]
32
+ model = field_model
33
+ model if model && keyed?(model, column, parent)
34
+ end
35
+
36
+ private
37
+
38
+ def explicit_model(name, ctx)
39
+ Lighthouse::GraphQL::GlobalId.type_model(name, ctx) ||
40
+ raise(Lighthouse::DefinitionError, "@globalId(type: #{name.inspect}): no such type or model.")
41
+ end
42
+
43
+ def keyed?(model, column, parent)
44
+ return false if parent && model.reflect_on_association(Lighthouse::Support::Naming.attribute(parent))
45
+
46
+ column == :primary_key || column == model.primary_key.to_s
47
+ end
48
+
49
+ # The field's model, when it has one: a field returning a scalar has none.
50
+ def field_model
51
+ model = @resolver.send(:model_class)
52
+ model if defined?(::ActiveRecord::Base) && model < ::ActiveRecord::Base
53
+ rescue Lighthouse::DefinitionError
54
+ nil
55
+ end
56
+
57
+ # { argument => [the column it filters, the argument whose input it is a field of] },
58
+ # by identity, for the @globalId(decode: ID) arguments and their inputs' fields.
59
+ def key_arguments(field_def)
60
+ keys = {}.compare_by_identity
61
+ field_def.arguments.each_value do |argument|
62
+ keys[argument] = [column(argument), nil] if decodes_id?(argument)
63
+ type = argument.type.unwrap
64
+ next unless type.kind.input_object?
65
+
66
+ type.arguments.each_value { |field| keys[field] = [column(field), argument.graphql_name] if decodes_id?(field) }
67
+ end
68
+ keys
69
+ end
70
+
71
+ def decodes_id?(argument)
72
+ directives(argument).any? { |d| d.name == 'globalId' && Lighthouse::Support::DirectiveArgs.literal(d, 'decode') == 'ID' }
73
+ end
74
+
75
+ def column(argument)
76
+ nodes = directives(argument)
77
+ return :primary_key if nodes.any? { |d| d.name == 'whereKey' }
78
+
79
+ filter = nodes.find { |d| KEY_FILTERS.include?(d.name) }
80
+ (Lighthouse::Support::DirectiveArgs.literal(filter, 'key') || Lighthouse::Support::Naming.attribute(argument.graphql_name)).to_s
81
+ end
82
+
83
+ def directives(argument) = argument.ast_node&.directives || []
84
+ end
@@ -60,6 +60,34 @@ module Lighthouse::GraphQL::GlobalId
60
60
  gid ? [gid.model_name.demodulize, gid.model_id] : decode_object(global_id, context)
61
61
  end
62
62
 
63
+ # Is `global_id`, decoded as `type`, an id of `model` or of a subclass (STI)?
64
+ # A Rails GlobalID names its model class; another names a GraphQL type, whose
65
+ # model is its @model(class:), else the class of its name.
66
+ def of_model?(global_id, type, model, context)
67
+ klass = rails_model(global_id) || type_model(type, context)
68
+ klass.is_a?(Class) && klass <= model
69
+ end
70
+
71
+ def rails_model(global_id)
72
+ return unless defined?(::GlobalID) && global_id.is_a?(String)
73
+
74
+ ::GlobalID.parse(global_id)&.model_class
75
+ rescue NameError
76
+ nil
77
+ end
78
+
79
+ # The model of the GraphQL type (or model) named `name`.
80
+ def type_model(name, context)
81
+ Lighthouse::Support::ModelResolver.constantize(declared_model(name, context) || name)
82
+ end
83
+
84
+ # The @model(class:) of the schema's type of that name.
85
+ def declared_model(name, context)
86
+ schema_type = context.schema.get_type(name.to_s) if context.respond_to?(:schema) && context.schema
87
+ directives = schema_type.respond_to?(:ast_node) ? schema_type.ast_node&.directives : nil
88
+ Lighthouse::Support::DirectiveArgs.literal(directives&.find { |d| d.name == 'model' }, 'class')
89
+ end
90
+
63
91
  # [type, id] of the object the schema's object_from_id finds.
64
92
  def decode_object(global_id, context)
65
93
  object = context.schema.object_from_id(global_id, context)
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Lighthouse
4
4
  module GraphQL
5
- VERSION = '0.8.1'
5
+ VERSION = '0.8.2'
6
6
  end
7
7
  end
@@ -132,6 +132,7 @@ require_relative 'namespaces'
132
132
  directive_resolvers/async_resolver
133
133
  directive_resolvers/global_id_resolver
134
134
  directive_resolvers/argument_binding
135
+ directive_resolvers/global_id_types
135
136
  directive_resolvers/argument_transforms_resolver
136
137
  directives/arguments/base
137
138
  directives/arguments/filters
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: lighthouse-graphql
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.8.1
4
+ version: 0.8.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Vaz Innovation
@@ -133,6 +133,7 @@ files:
133
133
  - lib/lighthouse/directive_resolvers/find_resolver.rb
134
134
  - lib/lighthouse/directive_resolvers/first_resolver.rb
135
135
  - lib/lighthouse/directive_resolvers/global_id_resolver.rb
136
+ - lib/lighthouse/directive_resolvers/global_id_types.rb
136
137
  - lib/lighthouse/directive_resolvers/guard_resolver.rb
137
138
  - lib/lighthouse/directive_resolvers/lazy_load_resolver.rb
138
139
  - lib/lighthouse/directive_resolvers/limit_resolver.rb