-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Resolver#initialize argument cache misses arguments added by field extensions #5572
Description
Describe the bug
When a FieldExtension adds arguments to a mutation field via apply, those arguments are not included in the @arguments_by_keyword cache built in Resolver#initialize. This means context.types.argument(mutation_class, name) returns nil for extension-added arguments, even though they are valid schema arguments on the field.
The root cause is this caching in Resolver#initialize:
@arguments_by_keyword = {}
context.types.arguments(self.class).each do |arg|
@arguments_by_keyword[arg.keyword] = arg
endcontext.types.arguments(self.class) only returns arguments defined directly on the resolver class — not arguments added to the field by extensions.
Versions
graphql version: 2.5.15
rails (or other framework): Rails (version not specific to the bug)
other applicable versions: N/A
GraphQL schema
class ExtraInput < GraphQL::Schema::InputObject
argument :value, String, required: false
end
class MyExtension < GraphQL::Schema::FieldExtension
def apply
field.argument(:extra, ExtraInput, required: false)
end
end
class MyMutation < GraphQL::Schema::Mutation
argument :input, String, required: false
extension(MyExtension)
field :result, String
def resolve(input: nil, extra: nil)
# extra is received correctly at resolve time,
# but is not in @arguments_by_keyword
{ result: "ok" }
end
end
class MutationRoot < GraphQL::Schema::Object
field :my_mutation, mutation: MyMutation
end
class MySchema < GraphQL::Schema
mutation MutationRoot
endGraphQL query
mutation {
myMutation(input: "test", extra: { value: "hello" }) {
result
}
}Steps to reproduce
- Define a
FieldExtensionthat adds an argument viafield.argument(...)inapply - Attach the extension to a mutation with
extension(MyExtension) - In any code that runs during resolution, call
context.types.argument(MyMutation, "extra")— it returnsnil - Alternatively, inspect
@arguments_by_keywordinside the resolver —:extrais missing
Expected behavior
context.types.arguments(self.class) (and by extension context.types.argument(self.class, name)) should include arguments added by field extensions, since those arguments are part of the field's actual schema signature and are accepted at query time.
Actual behavior
Extension-added arguments are missing from @arguments_by_keyword and from context.types.argument lookups against the resolver class. The arguments work correctly at resolve time (they're passed as keyword args), but any introspection or validation code that uses the types API to look up arguments on the mutation class can't find them.
Additional context
A workaround is to look up arguments via the schema field instead of the resolver class:
mutation_root = context.schema.mutation
mutation_field = context.types.field(mutation_root, "myMutation")
context.types.argument(mutation_field, "extra") # => returns the argumentThis correctly finds extension-added arguments, but requires traversing the schema manually rather than using the resolver class directly.