From 5f22ee6912b73de184b0ce1f256be047c3ee7aac Mon Sep 17 00:00:00 2001 From: Robert Mosolgo Date: Mon, 11 Nov 2024 08:00:18 -0500 Subject: [PATCH] Fix Lookahead#arguments when it is an Error --- lib/graphql/execution/lookahead.rb | 9 +++++- spec/graphql/execution/lookahead_spec.rb | 36 ++++++++++++++++++++++++ 2 files changed, 44 insertions(+), 1 deletion(-) diff --git a/lib/graphql/execution/lookahead.rb b/lib/graphql/execution/lookahead.rb index 90f509c40a..61aa94aae5 100644 --- a/lib/graphql/execution/lookahead.rb +++ b/lib/graphql/execution/lookahead.rb @@ -56,7 +56,14 @@ def arguments else @arguments = if @field @query.after_lazy(@query.arguments_for(@ast_nodes.first, @field)) do |args| - args.is_a?(Execution::Interpreter::Arguments) ? args.keyword_arguments : args + case args + when Execution::Interpreter::Arguments + args.keyword_arguments + when GraphQL::ExecutionError + EmptyObjects::EMPTY_HASH + else + args + end end else nil diff --git a/spec/graphql/execution/lookahead_spec.rb b/spec/graphql/execution/lookahead_spec.rb index 0b9cc32e78..eb87ca33f7 100644 --- a/spec/graphql/execution/lookahead_spec.rb +++ b/spec/graphql/execution/lookahead_spec.rb @@ -355,6 +355,42 @@ class AlwaysVisibleSchema < Schema assert res.key?("errors") assert_equal 0, context[:lookahead_latin_name] end + + describe "When there is an argument error" do + class NestedArgumentErrorSchema < GraphQL::Schema + class Data < GraphQL::Schema::Object + field :echo, String do + argument :input, String + end + + def echo(input:) + input + end + end + + class Query < GraphQL::Schema::Object + field :data, Data, extras: [:lookahead] + + def data(lookahead:) + context[:args_class] = lookahead.selection(:echo).arguments.class + {} + end + end + + query(Query) + end + + it "uses empty arguments" do + query_str = "query getEcho($input: String = null) { data { echo(input: $input) } }" + res = NestedArgumentErrorSchema.execute(query_str, variables: {}) + assert_equal ["`null` is not a valid input for `String!`, please provide a value for this argument."], res["errors"].map { |err| err["message"] } + assert_equal Hash, res.context[:args_class] + + good_res = NestedArgumentErrorSchema.execute("{ data { echo(input: \"Hello\") } }") + assert_equal "Hello", good_res["data"]["data"]["echo"] + assert_equal Hash, good_res.context[:args_class] + end + end end describe '#selections' do