Skip to content

Commit 6ac42cf

Browse files
feat: graphql-tag-pluck options (#1156)
* feat: implement config override * fix: tests working * fix: add basic and failing test * feat: work in progress custom tag test * fix: one test works without graphql-config, both fail with it * fix: test clsoer to working, having jest lifecycle issues * fix: tests working * fix: improving tests * feat: changeset * refactor for improving already good work 😍 Co-authored-by: Dimitri POSTOLOV <[email protected]>
1 parent 0d3fe5b commit 6ac42cf

File tree

5 files changed

+137
-6
lines changed

5 files changed

+137
-6
lines changed

.changeset/dirty-actors-decide.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
'@graphql-eslint/eslint-plugin': minor
3+
---
4+
5+
Using configuration options for `graphql-tag-pluck` through `graphql-config`
6+
Allow setup custom `globalGqlIdentifierName`, `modules.identifier` and `gqlMagicComment`

packages/plugin/src/graphql-config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import { ParserOptions } from './types';
1212
const debug = debugFactory('graphql-eslint:graphql-config');
1313
let graphQLConfig: GraphQLConfig;
1414

15-
export function loadGraphQLConfig(options: ParserOptions): GraphQLConfig {
15+
export function loadGraphQLConfig(options: ParserOptions = {}): GraphQLConfig {
1616
// We don't want cache config on test environment
1717
// Otherwise schema and documents will be same for all tests
1818
if (process.env.NODE_ENV !== 'test' && graphQLConfig) {

packages/plugin/src/processor.ts

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,32 @@
1-
import type { Linter } from 'eslint';
2-
import { parseCode } from '@graphql-tools/graphql-tag-pluck';
1+
import { Linter } from 'eslint';
2+
import { parseCode, GraphQLTagPluckOptions } from '@graphql-tools/graphql-tag-pluck';
3+
import { asArray } from '@graphql-tools/utils';
4+
import { loadGraphQLConfig } from './graphql-config';
35

4-
type Block = Linter.ProcessorFile & {
6+
export type Block = Linter.ProcessorFile & {
57
lineOffset: number;
68
offset: number;
79
};
810

9-
const RELEVANT_KEYWORDS = ['gql', 'graphql', '/* GraphQL */'] as const;
11+
const graphQLTagPluckOptions: GraphQLTagPluckOptions =
12+
loadGraphQLConfig().getDefault()?.extensions?.graphqlTagPluck;
13+
14+
const {
15+
modules = [],
16+
globalGqlIdentifierName = ['gql', 'graphql'],
17+
gqlMagicComment = 'GraphQL',
18+
} = graphQLTagPluckOptions || {};
19+
20+
const RELEVANT_KEYWORDS = [
21+
...new Set(
22+
[
23+
...modules.map(({ identifier }) => identifier),
24+
...asArray(globalGqlIdentifierName),
25+
gqlMagicComment,
26+
].filter(Boolean)
27+
),
28+
];
29+
1030
const blocksMap = new Map<string, Block[]>();
1131

1232
export const processor: Linter.Processor<Block | string> = {
@@ -19,8 +39,8 @@ export const processor: Linter.Processor<Block | string> = {
1939
code,
2040
filePath,
2141
options: {
22-
globalGqlIdentifierName: ['gql', 'graphql'],
2342
skipIndent: true,
43+
...graphQLTagPluckOptions,
2444
},
2545
});
2646

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import { Block, processor } from '../src/processor';
2+
3+
jest.mock('../src/graphql-config', () => ({
4+
loadGraphQLConfig: jest.fn(() => ({
5+
getDefault: () => ({
6+
extensions: {
7+
graphqlTagPluck: {
8+
modules: [{ name: 'custom-gql-tag', identifier: 'custom' }],
9+
gqlMagicComment: 'CustoM',
10+
},
11+
},
12+
}),
13+
})),
14+
}));
15+
16+
describe('processor.preprocess() with graphql-config', () => {
17+
const QUERY = 'query users { id }';
18+
it('should find "custom" tag', () => {
19+
const code = `
20+
import { custom } from 'custom-gql-tag'
21+
const fooQuery = custom\`${QUERY}\`
22+
`;
23+
const blocks = processor.preprocess(code, '') as Block[];
24+
25+
expect(blocks[0].text).toBe(QUERY);
26+
expect(blocks).toMatchInlineSnapshot(`
27+
Array [
28+
Object {
29+
filename: document.graphql,
30+
lineOffset: 2,
31+
offset: 77,
32+
text: query users { id },
33+
},
34+
35+
import { custom } from 'custom-gql-tag'
36+
const fooQuery = custom\`query users { id }\`
37+
,
38+
]
39+
`);
40+
});
41+
42+
it('should find /* CustoM */ magic comment', () => {
43+
const code = `/* CustoM */ \`${QUERY}\``;
44+
const blocks = processor.preprocess(code, '') as Block[];
45+
46+
expect(blocks[0].text).toBe(QUERY);
47+
expect(blocks).toMatchInlineSnapshot(`
48+
Array [
49+
Object {
50+
filename: document.graphql,
51+
lineOffset: 0,
52+
offset: 16,
53+
text: query users { id },
54+
},
55+
/* CustoM */ \`query users { id }\`,
56+
]
57+
`);
58+
});
59+
});
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import { Block, processor } from '../src/processor';
2+
3+
describe('processor.preprocess() without graphql-config', () => {
4+
const QUERY = 'query users { id }';
5+
it('should find "gql" tag', () => {
6+
const code = `
7+
import { gql } from 'graphql'
8+
const fooQuery = gql\`${QUERY}\`
9+
`;
10+
const blocks = processor.preprocess(code, '') as Block[];
11+
12+
expect(blocks[0].text).toBe(QUERY);
13+
expect(blocks).toMatchInlineSnapshot(`
14+
Array [
15+
Object {
16+
filename: document.graphql,
17+
lineOffset: 2,
18+
offset: 64,
19+
text: query users { id },
20+
},
21+
22+
import { gql } from 'graphql'
23+
const fooQuery = gql\`query users { id }\`
24+
,
25+
]
26+
`);
27+
});
28+
29+
it('should find /* GraphQL */ magic comment', () => {
30+
const code = `/* GraphQL */ \`${QUERY}\``;
31+
const blocks = processor.preprocess(code, '') as Block[];
32+
33+
expect(blocks[0].text).toBe(QUERY);
34+
expect(blocks).toMatchInlineSnapshot(`
35+
Array [
36+
Object {
37+
filename: document.graphql,
38+
lineOffset: 0,
39+
offset: 17,
40+
text: query users { id },
41+
},
42+
/* GraphQL */ \`query users { id }\`,
43+
]
44+
`);
45+
});
46+
});

0 commit comments

Comments
 (0)