Skip to content

Commit

Permalink
fix(utils/validation): fix definitions map conflict (#4842)
Browse files Browse the repository at this point in the history
  • Loading branch information
charlypoly committed Nov 14, 2022
1 parent c68ab55 commit 7411a5e
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/rotten-pumpkins-grow.md
@@ -0,0 +1,5 @@
---
'@graphql-tools/utils': patch
---

Fix validation swallowing fragments on naming conflicts
2 changes: 1 addition & 1 deletion packages/utils/src/validate-documents.ts
Expand Up @@ -21,7 +21,7 @@ export function validateGraphQlDocuments(
for (const document of documents) {
for (const docDefinition of document.definitions) {
if ('name' in docDefinition && docDefinition.name) {
definitionMap.set(docDefinition.name.value, docDefinition);
definitionMap.set(`${docDefinition.kind}_${docDefinition.name.value}`, docDefinition);
} else {
definitionMap.set(Date.now().toString(), docDefinition);
}
Expand Down
37 changes: 37 additions & 0 deletions packages/utils/tests/validate-documents.spec.ts
Expand Up @@ -54,4 +54,41 @@ describe('validateGraphQlDocuments', () => {
.toBe(`Fragment "pizzeriaFragment" cannot be spread here as objects of type "Query" can never be of type "Pizzeria".
at packages/client/src/pages/search/searchPage.query.graphql:6:15`);
});

it('Should not swallow fragments on operation/fragment name conflict', async () => {
const schema = buildSchema(/* GraphQL */ `
type Query {
currentUser(id: ID!): User!
}
type User {
id: ID!
username: String
email: String!
}
`);

const result = validateGraphQlDocuments(schema, [
parse(
new Source(
/* GraphQL */ `
fragment CurrentUser on User {
id
email
username
}
query CurrentUser {
currentUser(id: "1") {
...CurrentUser
}
}
`,
'packages/client/src/pages/search/operations.graphql'
)
),
]);

expect(result).toHaveLength(0);
});
});

0 comments on commit 7411a5e

Please sign in to comment.