Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/eleven-turkeys-smash.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@graphql-eslint/eslint-plugin': patch
---

Ignore NoUnusedFragmentsRule validation for fragments operations
5 changes: 5 additions & 0 deletions .changeset/spotty-bottles-whisper.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@graphql-eslint/eslint-plugin': patch
---

Improve error reported from validate-against-schema (added the rule name to the error)
20 changes: 15 additions & 5 deletions packages/plugin/src/rules/validate-against-schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,16 @@ import { GraphQLESTreeNode } from '../estree-parser';
import { GraphQLESLintRule, GraphQLESlintRuleContext } from '../types';
import { requireGraphQLSchemaFromContext } from '../utils';

function extractRuleName(stack: string | undefined): string | null {
const match = (stack || '').match(/validation[/\\\\]rules[/\\\\](.*?)\.js:/);

if (!match) {
return null;
}

return match[1] || null;
}

function validateDoc(
sourceNode: GraphQLESTreeNode<ASTNode>,
context: GraphQLESlintRuleContext,
Expand All @@ -15,11 +25,11 @@ function validateDoc(
const validationErrors = validate(schema, documentNode, rules);

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

context.report({
loc: node.loc,
message: error.message,
loc: error.locations[0],
message: validateRuleName ? `[${validateRuleName}] ${error.message}` : error.message,
});
}
} catch (e) {
Expand Down Expand Up @@ -99,7 +109,7 @@ const rule: GraphQLESLintRule<ValidateAgainstSchemaRuleConfig> = {
kind: Kind.DOCUMENT,
definitions: [node.rawNode()],
},
rulesArr
rulesArr.filter(r => r.name !== 'KnownFragmentNamesRule')
);
},
FragmentDefinition(node) {
Expand All @@ -112,7 +122,7 @@ const rule: GraphQLESLintRule<ValidateAgainstSchemaRuleConfig> = {
kind: Kind.DOCUMENT,
definitions: [node.rawNode()],
},
rulesArr
rulesArr.filter(r => r.name !== 'NoUnusedFragmentsRule')
);
},
};
Expand Down
26 changes: 19 additions & 7 deletions packages/plugin/tests/validate-against-schema.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,37 +30,49 @@ ruleTester.runGraphQLTests('validate-against-schema', rule, {
options: [{ overrideRules: ['NoUnusedVariablesRule'] }],
code: `query named ($id: ID!) { user(id: $id) { id @client } }`,
},
{
...WITH_SCHEMA,
code: `fragment UserFields on User { id }`,
},
{
...WITH_SCHEMA,
code: `fragment UserFields on User { id }`,
},
{
...WITH_SCHEMA,
code: `query { user(id: 1) { ...UserFields } }`,
},
],
invalid: [
{
...WITH_SCHEMA,
code: `query { user(id: 1) { notExists } }`,
errors: ['Cannot query field "notExists" on type "User".'],
errors: ['[FieldsOnCorrectTypeRule] Cannot query field "notExists" on type "User".'],
},
{
...WITH_SCHEMA,
options: [{ overrideRules: ['NoUnusedVariablesRule'] }],
code: `query named ($id: ID!) { user(id: 2) { id @client } }`,
errors: ['Variable "$id" is never used in operation "named".'],
errors: ['[NoUnusedVariablesRule] Variable "$id" is never used in operation "named".'],
},
{
...WITH_SCHEMA,
errors: ['Unknown directive "@client".'],
errors: ['[KnownDirectivesRule] Unknown directive "@client".'],
code: `query named ($id: ID!) { user(id: $id) { id @client } }`,
},
{
...WITH_SCHEMA,
errors: ['Unknown directive "@client".'],
errors: ['[KnownDirectivesRule] Unknown directive "@client".'],
options: [{ overrideRules: ['KnownDirectivesRule'] }],
code: `query named ($id: ID!) { user(id: $id) { id @client } }`,
},
{
...WITH_SCHEMA,
code: `query test($id: ID!) { user(invalid: $id) { test } }`,
errors: [
'Unknown argument "invalid" on field "Query.user".',
'Cannot query field "test" on type "User".',
'Field "user" argument "id" of type "ID!" is required, but it was not provided.',
'[ProvidedRequiredArgumentsRule] Field "user" argument "id" of type "ID!" is required, but it was not provided.',
'[KnownArgumentNamesRule] Unknown argument "invalid" on field "Query.user".',
'[FieldsOnCorrectTypeRule] Cannot query field "test" on type "User".',
],
},
],
Expand Down