Skip to content

Commit 7411a5e

Browse files
authored
fix(utils/validation): fix definitions map conflict (#4842)
1 parent c68ab55 commit 7411a5e

File tree

3 files changed

+43
-1
lines changed

3 files changed

+43
-1
lines changed

.changeset/rotten-pumpkins-grow.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@graphql-tools/utils': patch
3+
---
4+
5+
Fix validation swallowing fragments on naming conflicts

packages/utils/src/validate-documents.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ export function validateGraphQlDocuments(
2121
for (const document of documents) {
2222
for (const docDefinition of document.definitions) {
2323
if ('name' in docDefinition && docDefinition.name) {
24-
definitionMap.set(docDefinition.name.value, docDefinition);
24+
definitionMap.set(`${docDefinition.kind}_${docDefinition.name.value}`, docDefinition);
2525
} else {
2626
definitionMap.set(Date.now().toString(), docDefinition);
2727
}

packages/utils/tests/validate-documents.spec.ts

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,4 +54,41 @@ describe('validateGraphQlDocuments', () => {
5454
.toBe(`Fragment "pizzeriaFragment" cannot be spread here as objects of type "Query" can never be of type "Pizzeria".
5555
at packages/client/src/pages/search/searchPage.query.graphql:6:15`);
5656
});
57+
58+
it('Should not swallow fragments on operation/fragment name conflict', async () => {
59+
const schema = buildSchema(/* GraphQL */ `
60+
type Query {
61+
currentUser(id: ID!): User!
62+
}
63+
64+
type User {
65+
id: ID!
66+
username: String
67+
email: String!
68+
}
69+
`);
70+
71+
const result = validateGraphQlDocuments(schema, [
72+
parse(
73+
new Source(
74+
/* GraphQL */ `
75+
fragment CurrentUser on User {
76+
id
77+
email
78+
username
79+
}
80+
81+
query CurrentUser {
82+
currentUser(id: "1") {
83+
...CurrentUser
84+
}
85+
}
86+
`,
87+
'packages/client/src/pages/search/operations.graphql'
88+
)
89+
),
90+
]);
91+
92+
expect(result).toHaveLength(0);
93+
});
5794
});

0 commit comments

Comments
 (0)