Skip to content

Commit c837c99

Browse files
author
Dimitri POSTOLOV
authored
fix false positive case in no-unreachable-types rule when directive is used on schema (#755)
1 parent 4c29de7 commit c837c99

File tree

3 files changed

+27
-11
lines changed

3 files changed

+27
-11
lines changed

.changeset/shiny-buttons-relax.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 positive case in `no-unreachable-types` rule when directive on root schema is used

packages/plugin/src/graphql-ast.ts

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,4 @@
1-
import {
2-
ASTNode,
3-
ASTVisitor,
4-
TypeInfo,
5-
GraphQLSchema,
6-
visit,
7-
isInterfaceType,
8-
visitWithTypeInfo,
9-
} from 'graphql';
1+
import { ASTNode, ASTVisitor, TypeInfo, GraphQLSchema, visit, isInterfaceType, visitWithTypeInfo } from 'graphql';
102
import { SiblingOperations } from './sibling-operations';
113
import { getTypeName } from './utils';
124

@@ -50,7 +42,12 @@ export function getReachableTypes(schema: GraphQLSchema): ReachableTypes {
5042
NamedType: collect,
5143
};
5244

53-
for (const type of [schema.getQueryType(), schema.getMutationType(), schema.getSubscriptionType()]) {
45+
for (const type of [
46+
schema, // visiting SchemaDefinition node
47+
schema.getQueryType(),
48+
schema.getMutationType(),
49+
schema.getSubscriptionType(),
50+
]) {
5451
if (type) {
5552
visit(type.astNode, visitor);
5653
}

packages/plugin/tests/no-unreachable-types.spec.ts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@ const useSchema = (schema: string): { code: string; parserOptions: ParserOptions
88
};
99
};
1010

11-
new GraphQLRuleTester().runGraphQLTests('no-unreachable-types', rule, {
11+
const ruleTester = new GraphQLRuleTester();
12+
13+
ruleTester.runGraphQLTests('no-unreachable-types', rule, {
1214
valid: [
1315
useSchema(/* GraphQL */ `
1416
scalar A
@@ -118,6 +120,18 @@ new GraphQLRuleTester().runGraphQLTests('no-unreachable-types', rule, {
118120
me: User
119121
}
120122
`),
123+
{
124+
name: 'directive on schema',
125+
...useSchema(/* GraphQL */ `
126+
type Query
127+
128+
schema @good {
129+
query: Query
130+
}
131+
132+
directive @good on SCHEMA
133+
`),
134+
},
121135
],
122136
invalid: [
123137
{

0 commit comments

Comments
 (0)