Skip to content

Commit 44a6c73

Browse files
Dimitri POSTOLOVdotansimha
andauthored
fix false negative case for no-undefined-variables rule (#659)
Co-authored-by: Dotan Simha <[email protected]>
1 parent afc25b9 commit 44a6c73

File tree

6 files changed

+49
-4
lines changed

6 files changed

+49
-4
lines changed

.changeset/hungry-kings-hammer.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': minor
3+
---
4+
5+
fix false negative case for `no-undefined-variables` rule

packages/plugin/src/rules/graphql-js-validation.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -216,9 +216,14 @@ export const GRAPHQL_JS_VALIDATIONS = Object.assign(
216216
validationToRule('no-fragment-cycles', 'NoFragmentCycles', {
217217
description: `A GraphQL fragment is only valid when it does not have cycles in fragments usage.`,
218218
}),
219-
validationToRule('no-undefined-variables', 'NoUndefinedVariables', {
220-
description: `A GraphQL operation is only valid if all variables encountered, both directly and via fragment spreads, are defined by that operation.`,
221-
}),
219+
validationToRule(
220+
'no-undefined-variables',
221+
'NoUndefinedVariables',
222+
{
223+
description: `A GraphQL operation is only valid if all variables encountered, both directly and via fragment spreads, are defined by that operation.`,
224+
},
225+
importFiles
226+
),
222227
validationToRule(
223228
'no-unused-fragments',
224229
'NoUnusedFragments',
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#import UserFields from './user-fields-with-variables.gql'
2+
3+
query User {
4+
user {
5+
id
6+
...UserFields
7+
}
8+
}

packages/plugin/tests/mocks/no-unused-variables.gql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#import UserFields from './no-unused-variables-imported.gql'
1+
#import UserFields from './user-fields-with-variables.gql'
22

33
query User($limit: Int!, $offset: Int!) {
44
user {
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
fragment UserFields on User {
2+
firstName
3+
posts(limit: $limit, offset: $offset) {
4+
id
5+
}
6+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { join } from 'path';
2+
import { GraphQLRuleTester, rules } from '../src';
3+
4+
const ruleTester = new GraphQLRuleTester();
5+
6+
ruleTester.runGraphQLTests('no-undefined-variables', rules['no-undefined-variables'], {
7+
valid: [],
8+
invalid: [
9+
{
10+
filename: join(__dirname, 'mocks/no-undefined-variables.gql'),
11+
code: ruleTester.fromMockFile('no-undefined-variables.gql'),
12+
parserOptions: {
13+
schema: join(__dirname, 'mocks/user-schema.graphql'),
14+
},
15+
errors: [
16+
{ message: 'Variable "$limit" is not defined by operation "User".' },
17+
{ message: 'Variable "$offset" is not defined by operation "User".' },
18+
],
19+
},
20+
],
21+
});

0 commit comments

Comments
 (0)