From 08a86a35cbe91d80357f262707b9e0f23ea76948 Mon Sep 17 00:00:00 2001 From: Matt Edlefsen Date: Tue, 19 May 2026 09:50:16 -0400 Subject: [PATCH] Fix `required: :nullable` crash when combined with `as:` The auto-registered RequiredValidator was keyed by the original positional argument name, but `coerce_into_values` keys the values hash by `@keyword` (which respects `as:`). When the two differed, validation always failed; on older builds without safe-nav in `arg_keyword_to_graphql_name` it crashed with NoMethodError. --- lib/graphql/schema/argument.rb | 2 +- spec/graphql/schema/argument_spec.rb | 29 ++++++++++++++++++++++++++++ 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/lib/graphql/schema/argument.rb b/lib/graphql/schema/argument.rb index 7f38fea5c5..214d6c6ef9 100644 --- a/lib/graphql/schema/argument.rb +++ b/lib/graphql/schema/argument.rb @@ -94,7 +94,7 @@ def initialize(arg_name = nil, type_expr = nil, desc = nil, required: true, type end if required == :nullable - self.owner.validates(required: { argument: arg_name }) + self.owner.validates(required: { argument: @keyword }) end if definition_block diff --git a/spec/graphql/schema/argument_spec.rb b/spec/graphql/schema/argument_spec.rb index d8d5024306..080c8565f8 100644 --- a/spec/graphql/schema/argument_spec.rb +++ b/spec/graphql/schema/argument_spec.rb @@ -620,6 +620,35 @@ def echo(str:) res = RequiredNullableSchema.execute('{ echo }') assert_equal ["echo must include the following argument: str."], res["errors"].map { |e| e["message"] } end + + describe "combined with `as:`" do + class RequiredNullableAsSchema < GraphQL::Schema + class Query < GraphQL::Schema::Object + field :echo, String, resolve_static: true do + argument :str, String, required: :nullable, as: :renamed_str + end + + def self.echo(context, renamed_str:) + renamed_str.inspect + end + + def echo(renamed_str:) + renamed_str.inspect + end + end + + query(Query) + end + + it "validates against the aliased keyword" do + res = RequiredNullableAsSchema.execute('{ echo(str: "ok") }') + assert_equal "\"ok\"", res["data"]["echo"] + res = RequiredNullableAsSchema.execute('{ echo(str: null) }') + assert_equal "nil", res["data"]["echo"] + res = RequiredNullableAsSchema.execute('{ echo }') + assert_equal ["echo must include the following argument: str."], res["errors"].map { |e| e["message"] } + end + end end describe "replace_null_with_default: true" do