Skip to content

Commit 3a5e370

Browse files
authored
Merge pull request rmosolgo#4268 from rmosolgo/fix-duplicate-second-level-fragment-name
Properly handle duplicate fragment names used indirectly
2 parents 266aa76 + 3cc3511 commit 3a5e370

File tree

2 files changed

+32
-1
lines changed

2 files changed

+32
-1
lines changed

lib/graphql/static_validation/definition_dependencies.rb

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,8 +127,14 @@ def resolve_dependencies
127127
# same name as if they were the same name. If _any_ of the fragments
128128
# with that name has a dependency, we record it.
129129
independent_fragment_nodes = @defdep_fragment_definitions.values.flatten - @defdep_immediate_dependencies.keys
130-
130+
visited_fragment_names = Set.new
131131
while fragment_node = independent_fragment_nodes.pop
132+
if visited_fragment_names.add?(fragment_node.name)
133+
# this is a new fragment name
134+
else
135+
# this is a duplicate fragment name
136+
next
137+
end
132138
loops += 1
133139
if loops > max_loops
134140
raise("Resolution loops exceeded the number of definitions; infinite loop detected. (Max: #{max_loops}, Current: #{loops})")

spec/graphql/static_validation/rules/fragment_names_are_unique_spec.rb

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,4 +50,29 @@
5050
assert_includes(errors, fragment_def_error)
5151
end
5252
end
53+
54+
describe "when used at second level" do
55+
let(:query_string) {"
56+
query {
57+
cheese(id: 1) {
58+
... frag1
59+
}
60+
}
61+
62+
fragment frag1 on Cheese { ...frag2 }
63+
fragment frag2 on Cheese { id }
64+
fragment frag2 on Cheese { id }
65+
"}
66+
67+
it "finds the error" do
68+
assert_equal(1, errors.length)
69+
fragment_def_error = {
70+
"message"=>"Fragment name \"frag2\" must be unique",
71+
"locations"=>[{"line"=>9, "column"=>7}, {"line"=>10, "column"=>7}],
72+
"path"=>[],
73+
"extensions"=>{"code"=>"fragmentNotUnique", "fragmentName"=>"frag2"}
74+
}
75+
assert_includes(errors, fragment_def_error)
76+
end
77+
end
5378
end

0 commit comments

Comments
 (0)