Skip to content

Commit

Permalink
make globalGqlIdentifierName case sensitive (#5945)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
fantastiskm authored Dec 24, 2024
1 parent 27a32bc commit d288b8b
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 3 deletions.
2 changes: 1 addition & 1 deletion packages/graphql-tag-pluck/src/visitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 };

Expand Down
34 changes: 33 additions & 1 deletion packages/graphql-tag-pluck/tests/graphql-tag-pluck.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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',
Expand Down
2 changes: 1 addition & 1 deletion website/src/pages/docs/graphql-tag-pluck.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit d288b8b

Please sign in to comment.