From 7411a5e71a8138d9ccfe907b1fb01e62fcbb0cdb Mon Sep 17 00:00:00 2001 From: Charly POLY <1252066+charlypoly@users.noreply.github.com> Date: Mon, 14 Nov 2022 12:06:44 +0100 Subject: [PATCH] fix(utils/validation): fix definitions map conflict (#4842) --- .changeset/rotten-pumpkins-grow.md | 5 +++ packages/utils/src/validate-documents.ts | 2 +- .../utils/tests/validate-documents.spec.ts | 37 +++++++++++++++++++ 3 files changed, 43 insertions(+), 1 deletion(-) create mode 100644 .changeset/rotten-pumpkins-grow.md diff --git a/.changeset/rotten-pumpkins-grow.md b/.changeset/rotten-pumpkins-grow.md new file mode 100644 index 00000000000..8fb2b1178ca --- /dev/null +++ b/.changeset/rotten-pumpkins-grow.md @@ -0,0 +1,5 @@ +--- +'@graphql-tools/utils': patch +--- + +Fix validation swallowing fragments on naming conflicts diff --git a/packages/utils/src/validate-documents.ts b/packages/utils/src/validate-documents.ts index 4b831dcfcff..bc375f02d70 100644 --- a/packages/utils/src/validate-documents.ts +++ b/packages/utils/src/validate-documents.ts @@ -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); } diff --git a/packages/utils/tests/validate-documents.spec.ts b/packages/utils/tests/validate-documents.spec.ts index 02e7b0f1105..fca5064e2cf 100644 --- a/packages/utils/tests/validate-documents.spec.ts +++ b/packages/utils/tests/validate-documents.spec.ts @@ -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); + }); });