Skip to content

Commit

Permalink
Merge pull request #5155 from rmosolgo/fix-lookahead-arguments-error
Browse files Browse the repository at this point in the history
Fix Lookahead#arguments when it is an Error
  • Loading branch information
rmosolgo authored Nov 11, 2024
2 parents 6adf39b + 5f22ee6 commit 6b71689
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 1 deletion.
9 changes: 8 additions & 1 deletion lib/graphql/execution/lookahead.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
36 changes: 36 additions & 0 deletions spec/graphql/execution/lookahead_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 6b71689

Please sign in to comment.