Skip to content

Commit 52a98df

Browse files
authored
Fixes for validate-against-schema (#69)
* improve reported error from validate-against-schema ignore NoUnusedFragmentsRule when it's not set * added changeset * fixed lint issues * fixes * fixes for unused
1 parent 3ecbe2e commit 52a98df

File tree

4 files changed

+44
-12
lines changed

4 files changed

+44
-12
lines changed

.changeset/eleven-turkeys-smash.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+
Ignore NoUnusedFragmentsRule validation for fragments operations

.changeset/spotty-bottles-whisper.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+
Improve error reported from validate-against-schema (added the rule name to the error)

packages/plugin/src/rules/validate-against-schema.ts

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,16 @@ import { GraphQLESTreeNode } from '../estree-parser';
33
import { GraphQLESLintRule, GraphQLESlintRuleContext } from '../types';
44
import { requireGraphQLSchemaFromContext } from '../utils';
55

6+
function extractRuleName(stack: string | undefined): string | null {
7+
const match = (stack || '').match(/validation[/\\\\]rules[/\\\\](.*?)\.js:/);
8+
9+
if (!match) {
10+
return null;
11+
}
12+
13+
return match[1] || null;
14+
}
15+
616
function validateDoc(
717
sourceNode: GraphQLESTreeNode<ASTNode>,
818
context: GraphQLESlintRuleContext,
@@ -15,11 +25,11 @@ function validateDoc(
1525
const validationErrors = validate(schema, documentNode, rules);
1626

1727
for (const error of validationErrors) {
18-
const node = (error.nodes[0] as any) as GraphQLESTreeNode<ASTNode>;
28+
const validateRuleName = extractRuleName(error.stack);
1929

2030
context.report({
21-
loc: node.loc,
22-
message: error.message,
31+
loc: error.locations[0],
32+
message: validateRuleName ? `[${validateRuleName}] ${error.message}` : error.message,
2333
});
2434
}
2535
} catch (e) {
@@ -99,7 +109,7 @@ const rule: GraphQLESLintRule<ValidateAgainstSchemaRuleConfig> = {
99109
kind: Kind.DOCUMENT,
100110
definitions: [node.rawNode()],
101111
},
102-
rulesArr
112+
rulesArr.filter(r => r.name !== 'KnownFragmentNamesRule')
103113
);
104114
},
105115
FragmentDefinition(node) {
@@ -112,7 +122,7 @@ const rule: GraphQLESLintRule<ValidateAgainstSchemaRuleConfig> = {
112122
kind: Kind.DOCUMENT,
113123
definitions: [node.rawNode()],
114124
},
115-
rulesArr
125+
rulesArr.filter(r => r.name !== 'NoUnusedFragmentsRule')
116126
);
117127
},
118128
};

packages/plugin/tests/validate-against-schema.spec.ts

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,37 +30,49 @@ ruleTester.runGraphQLTests('validate-against-schema', rule, {
3030
options: [{ overrideRules: ['NoUnusedVariablesRule'] }],
3131
code: `query named ($id: ID!) { user(id: $id) { id @client } }`,
3232
},
33+
{
34+
...WITH_SCHEMA,
35+
code: `fragment UserFields on User { id }`,
36+
},
37+
{
38+
...WITH_SCHEMA,
39+
code: `fragment UserFields on User { id }`,
40+
},
41+
{
42+
...WITH_SCHEMA,
43+
code: `query { user(id: 1) { ...UserFields } }`,
44+
},
3345
],
3446
invalid: [
3547
{
3648
...WITH_SCHEMA,
3749
code: `query { user(id: 1) { notExists } }`,
38-
errors: ['Cannot query field "notExists" on type "User".'],
50+
errors: ['[FieldsOnCorrectTypeRule] Cannot query field "notExists" on type "User".'],
3951
},
4052
{
4153
...WITH_SCHEMA,
4254
options: [{ overrideRules: ['NoUnusedVariablesRule'] }],
4355
code: `query named ($id: ID!) { user(id: 2) { id @client } }`,
44-
errors: ['Variable "$id" is never used in operation "named".'],
56+
errors: ['[NoUnusedVariablesRule] Variable "$id" is never used in operation "named".'],
4557
},
4658
{
4759
...WITH_SCHEMA,
48-
errors: ['Unknown directive "@client".'],
60+
errors: ['[KnownDirectivesRule] Unknown directive "@client".'],
4961
code: `query named ($id: ID!) { user(id: $id) { id @client } }`,
5062
},
5163
{
5264
...WITH_SCHEMA,
53-
errors: ['Unknown directive "@client".'],
65+
errors: ['[KnownDirectivesRule] Unknown directive "@client".'],
5466
options: [{ overrideRules: ['KnownDirectivesRule'] }],
5567
code: `query named ($id: ID!) { user(id: $id) { id @client } }`,
5668
},
5769
{
5870
...WITH_SCHEMA,
5971
code: `query test($id: ID!) { user(invalid: $id) { test } }`,
6072
errors: [
61-
'Unknown argument "invalid" on field "Query.user".',
62-
'Cannot query field "test" on type "User".',
63-
'Field "user" argument "id" of type "ID!" is required, but it was not provided.',
73+
'[ProvidedRequiredArgumentsRule] Field "user" argument "id" of type "ID!" is required, but it was not provided.',
74+
'[KnownArgumentNamesRule] Unknown argument "invalid" on field "Query.user".',
75+
'[FieldsOnCorrectTypeRule] Cannot query field "test" on type "User".',
6476
],
6577
},
6678
],

0 commit comments

Comments
 (0)