Skip to content

Commit f370359

Browse files
authored
add an example with custom graphql rules (#2787)
* aa * aa * aa * aa * aa * Update examples/custom-rules/package.json * aa * polish * fix test * tiny fix * aa * aa * aa * Update .changeset/strong-otters-mate.md
1 parent 99b0703 commit f370359

File tree

14 files changed

+164
-32
lines changed

14 files changed

+164
-32
lines changed

.changeset/strong-otters-mate.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@graphql-eslint/eslint-plugin': patch
3+
---
4+
5+
add an example with [custom graphql rules](https://github.com/dimaMachina/graphql-eslint/tree/master/examples/custom-rules)
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import graphqlPlugin from '@graphql-eslint/eslint-plugin';
2+
import { rule } from './my-rule.js';
3+
4+
export default [
5+
{
6+
files: ['**/*.graphql'],
7+
languageOptions: {
8+
parser: graphqlPlugin.parser,
9+
},
10+
plugins: {
11+
'@internal': {
12+
rules: {
13+
'my-rule': rule,
14+
},
15+
},
16+
},
17+
rules: {
18+
'@internal/my-rule': 'error',
19+
},
20+
},
21+
];

examples/custom-rules/my-rule.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
export const rule = {
2+
create(context) {
3+
return {
4+
OperationDefinition(node) {
5+
if (!node.name?.value) {
6+
context.report({
7+
node,
8+
message: 'Oops, name is required!',
9+
});
10+
}
11+
},
12+
};
13+
},
14+
};

examples/custom-rules/package.json

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"name": "@graphql-eslint/example-custom-rules",
3+
"version": "0.0.0",
4+
"type": "module",
5+
"repository": "https://github.com/dimaMachina/graphql-eslint",
6+
"author": "Dimitri POSTOLOV <[email protected]>",
7+
"private": true,
8+
"scripts": {
9+
"lint": "eslint --cache ."
10+
},
11+
"dependencies": {
12+
"graphql": "16.9.0"
13+
},
14+
"devDependencies": {
15+
"@graphql-eslint/eslint-plugin": "workspace:*",
16+
"eslint": "9.15.0"
17+
}
18+
}

examples/custom-rules/test.graphql

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
query {
2+
foo
3+
}

packages/plugin/__tests__/__snapshots__/examples.spec.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2133,3 +2133,23 @@ exports[`Examples > should work with \`graphql-config\` 2`] = `
21332133
},
21342134
]
21352135
`;
2136+
2137+
exports[`Examples > should work with custom rules 1`] = `
2138+
[
2139+
{
2140+
filePath: examples/custom-rules/test.graphql,
2141+
messages: [
2142+
{
2143+
column: 1,
2144+
endColumn: 16,
2145+
endLine: 3,
2146+
line: 1,
2147+
message: Oops, name is required!,
2148+
nodeType: OperationDefinition,
2149+
ruleId: @internal/my-rule,
2150+
severity: 2,
2151+
},
2152+
],
2153+
},
2154+
]
2155+
`;

packages/plugin/__tests__/examples.spec.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,16 @@ describe('Examples', () => {
121121
const cwd = path.join(CWD, 'examples', 'multiple-projects-graphql-config');
122122
testESLintOutput(cwd, 4);
123123
});
124+
125+
it('should work with custom rules', () => {
126+
const cwd = path.join(CWD, 'examples', 'custom-rules');
127+
const flatResults = getFlatESLintOutput(cwd);
128+
// Windows has some offset for `range`, I think due \r\n handling
129+
if (os.platform() !== 'win32') {
130+
expect(normalizeResults(flatResults)).toMatchSnapshot();
131+
}
132+
expect(countErrors(flatResults)).toBe(1);
133+
});
124134
});
125135

126136
function testESLintOutput(cwd: string, errorCount: number): void {

pnpm-lock.yaml

Lines changed: 13 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
---
2+
sidebarTitle: Custom GraphQL Rules
3+
---
4+
5+
# Usage with custom GraphQL rules
6+
7+
<ESLintConfigs gitFolder="custom-rules" additionalFiles={{ 'Custom GraphQL Rule': 'my-rule.js' }} />
8+
9+
> [!TIP]
10+
>
11+
> Check out the [custom rules](/docs/custom-rules) guide to learn how to create your own rules.

website/content/docs/usage/graphql.mdx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,7 @@ icon: GraphQLIcon
55

66
# Usage with `.graphql` files
77

8-
<ESLintConfigs gitFolder="graphql-config" graphqlConfigFile="graphql.config.js" />
8+
<ESLintConfigs
9+
gitFolder="graphql-config"
10+
additionalFiles={{ 'GraphQL Config': 'graphql.config.js' }}
11+
/>

0 commit comments

Comments
 (0)