Skip to content

Properly handle duplicate fragment names used indirectly #4268

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion lib/graphql/static_validation/definition_dependencies.rb
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,14 @@ def resolve_dependencies
# same name as if they were the same name. If _any_ of the fragments
# with that name has a dependency, we record it.
independent_fragment_nodes = @defdep_fragment_definitions.values.flatten - @defdep_immediate_dependencies.keys

visited_fragment_names = Set.new
while fragment_node = independent_fragment_nodes.pop
if visited_fragment_names.add?(fragment_node.name)
# this is a new fragment name
else
# this is a duplicate fragment name
next
end
loops += 1
if loops > max_loops
raise("Resolution loops exceeded the number of definitions; infinite loop detected. (Max: #{max_loops}, Current: #{loops})")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,29 @@
assert_includes(errors, fragment_def_error)
end
end

describe "when used at second level" do
let(:query_string) {"
query {
cheese(id: 1) {
... frag1
}
}

fragment frag1 on Cheese { ...frag2 }
fragment frag2 on Cheese { id }
fragment frag2 on Cheese { id }
"}

it "finds the error" do
assert_equal(1, errors.length)
fragment_def_error = {
"message"=>"Fragment name \"frag2\" must be unique",
"locations"=>[{"line"=>9, "column"=>7}, {"line"=>10, "column"=>7}],
"path"=>[],
"extensions"=>{"code"=>"fragmentNotUnique", "fragmentName"=>"frag2"}
}
assert_includes(errors, fragment_def_error)
end
end
end