Skip to content

Commit

Permalink
fix false positive case in no-unreachable-types rule when directive…
Browse files Browse the repository at this point in the history
… is used on schema (#755)
  • Loading branch information
Dimitri POSTOLOV committed Nov 11, 2021
1 parent 4c29de7 commit c837c99
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 11 deletions.
5 changes: 5 additions & 0 deletions .changeset/shiny-buttons-relax.md
@@ -0,0 +1,5 @@
---
'@graphql-eslint/eslint-plugin': minor
---

fix false positive case in `no-unreachable-types` rule when directive on root schema is used
17 changes: 7 additions & 10 deletions packages/plugin/src/graphql-ast.ts
@@ -1,12 +1,4 @@
import {
ASTNode,
ASTVisitor,
TypeInfo,
GraphQLSchema,
visit,
isInterfaceType,
visitWithTypeInfo,
} from 'graphql';
import { ASTNode, ASTVisitor, TypeInfo, GraphQLSchema, visit, isInterfaceType, visitWithTypeInfo } from 'graphql';
import { SiblingOperations } from './sibling-operations';
import { getTypeName } from './utils';

Expand Down Expand Up @@ -50,7 +42,12 @@ export function getReachableTypes(schema: GraphQLSchema): ReachableTypes {
NamedType: collect,
};

for (const type of [schema.getQueryType(), schema.getMutationType(), schema.getSubscriptionType()]) {
for (const type of [
schema, // visiting SchemaDefinition node
schema.getQueryType(),
schema.getMutationType(),
schema.getSubscriptionType(),
]) {
if (type) {
visit(type.astNode, visitor);
}
Expand Down
16 changes: 15 additions & 1 deletion packages/plugin/tests/no-unreachable-types.spec.ts
Expand Up @@ -8,7 +8,9 @@ const useSchema = (schema: string): { code: string; parserOptions: ParserOptions
};
};

new GraphQLRuleTester().runGraphQLTests('no-unreachable-types', rule, {
const ruleTester = new GraphQLRuleTester();

ruleTester.runGraphQLTests('no-unreachable-types', rule, {
valid: [
useSchema(/* GraphQL */ `
scalar A
Expand Down Expand Up @@ -118,6 +120,18 @@ new GraphQLRuleTester().runGraphQLTests('no-unreachable-types', rule, {
me: User
}
`),
{
name: 'directive on schema',
...useSchema(/* GraphQL */ `
type Query
schema @good {
query: Query
}
directive @good on SCHEMA
`),
},
],
invalid: [
{
Expand Down

0 comments on commit c837c99

Please sign in to comment.