Skip to content

Commit

Permalink
Merge pull request #5129 from ravangen/lookahead-no-selected-operation
Browse files Browse the repository at this point in the history
Handle no selected operation for `Query#lookahead`
  • Loading branch information
rmosolgo authored Oct 21, 2024
2 parents c663621 + 8a60325 commit 56265cd
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 8 deletions.
20 changes: 12 additions & 8 deletions lib/graphql/query.rb
Original file line number Diff line number Diff line change
Expand Up @@ -209,15 +209,19 @@ def subscription_update?
def lookahead
@lookahead ||= begin
ast_node = selected_operation
root_type = case ast_node.operation_type
when nil, "query"
types.query_root # rubocop:disable Development/ContextIsPassedCop
when "mutation"
types.mutation_root # rubocop:disable Development/ContextIsPassedCop
when "subscription"
types.subscription_root # rubocop:disable Development/ContextIsPassedCop
if ast_node.nil?
GraphQL::Execution::Lookahead::NULL_LOOKAHEAD
else
root_type = case ast_node.operation_type
when nil, "query"
types.query_root # rubocop:disable Development/ContextIsPassedCop
when "mutation"
types.mutation_root # rubocop:disable Development/ContextIsPassedCop
when "subscription"
types.subscription_root # rubocop:disable Development/ContextIsPassedCop
end
GraphQL::Execution::Lookahead.new(query: self, root_type: root_type, ast_nodes: [ast_node])
end
GraphQL::Execution::Lookahead.new(query: self, root_type: root_type, ast_nodes: [ast_node])
end
end

Expand Down
5 changes: 5 additions & 0 deletions spec/graphql/execution/lookahead_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,11 @@ class AlwaysVisibleSchema < Schema
assert_equal true, query.lookahead.selects?("__typename")
end

it "uses null lookahead when no operation is selected" do
query = GraphQL::Query.new(schema, document: document, variables: { name: "Cardinal" }, operation_name: "Invalid")
assert_selection_is_null query.lookahead
end

describe "with a NullWarden" do
let(:schema) { LookaheadTest::AlwaysVisibleSchema }

Expand Down

0 comments on commit 56265cd

Please sign in to comment.