From d288b8b75794b25f2f2fa6383d55087a24dacd20 Mon Sep 17 00:00:00 2001 From: Melissa <93585768+fantastiskm@users.noreply.github.com> Date: Tue, 24 Dec 2024 12:55:08 +0100 Subject: [PATCH] make globalGqlIdentifierName case sensitive (#5945) * Global Identifiers are forced lower case Global identifiers are converted to lower case before being used in this file. This patch corrects the comparison function to so they match in the file if the source identifier is not all lower case * Prettier & Tests * Update graphql-tag-pluck.mdx * Change to be case sensitive - don't lower case any globalGqlIdentifierNames --- packages/graphql-tag-pluck/src/visitor.ts | 2 +- .../tests/graphql-tag-pluck.test.ts | 34 ++++++++++++++++++- website/src/pages/docs/graphql-tag-pluck.mdx | 2 +- 3 files changed, 35 insertions(+), 3 deletions(-) diff --git a/packages/graphql-tag-pluck/src/visitor.ts b/packages/graphql-tag-pluck/src/visitor.ts index 111c0743c1b..35667f300e2 100644 --- a/packages/graphql-tag-pluck/src/visitor.ts +++ b/packages/graphql-tag-pluck/src/visitor.ts @@ -203,7 +203,7 @@ export default (code: string, out: any, options: GraphQLTagPluckOptions = {}) => identifier: mod.identifier && mod.identifier.toLowerCase(), }; }); - globalGqlIdentifierName = asArray(globalGqlIdentifierName).map(s => s!.toLowerCase()); + globalGqlIdentifierName = asArray(globalGqlIdentifierName ?? ''); const hooksOptions = { skipIndent, gqlMagicComment, modules, globalGqlIdentifierName }; diff --git a/packages/graphql-tag-pluck/tests/graphql-tag-pluck.test.ts b/packages/graphql-tag-pluck/tests/graphql-tag-pluck.test.ts index 1b711e3fcc9..568c4bfe7f7 100644 --- a/packages/graphql-tag-pluck/tests/graphql-tag-pluck.test.ts +++ b/packages/graphql-tag-pluck/tests/graphql-tag-pluck.test.ts @@ -1943,7 +1943,6 @@ describe('graphql-tag-pluck', () => { globalGqlIdentifierName: 'anothergql', }, ); - expect(sources.map(source => source.body).join('\n\n')).toEqual( freeText(` fragment Foo on FooType { @@ -1959,6 +1958,39 @@ describe('graphql-tag-pluck', () => { ); }); + it('should be able to specify the global GraphQL identifier name case sensitively', async () => { + const sources = await pluck( + 'tmp-XXXXXX.js', + freeText(` + const fragment = anotherGql(\` + fragment Foo on FooType { + id + } + \`) + + const doc = AnotherGql\` + query foo { + foo { + ...Foo + } + } + + \${fragment} + \` + `), + { + globalGqlIdentifierName: 'anotherGql', + }, + ); + + expect(sources.map(source => source.body).join('\n\n')).toEqual( + freeText(` + fragment Foo on FooType { + id + }`), + ); + }); + it('should be able to specify the GraphQL magic comment to look for', async () => { const sources = await pluck( 'tmp-XXXXXX.js', diff --git a/website/src/pages/docs/graphql-tag-pluck.mdx b/website/src/pages/docs/graphql-tag-pluck.mdx index c389672ba2b..2c1e2423f5c 100644 --- a/website/src/pages/docs/graphql-tag-pluck.mdx +++ b/website/src/pages/docs/graphql-tag-pluck.mdx @@ -103,7 +103,7 @@ be translated into `/* GraphQL */` in code. ### `globalGqlIdentifierName` -Allows using a global identifier instead of a module import. +Allows using a global identifier instead of a module import. Identifiers are case sensitive. ```js // `graphql` is a global function