Skip to content

Commit 01421ae

Browse files
authored
fix(no-unreachable-types): ignore directives with Request Definitions… (#1108)
* fix(no-unreachable-types): ignore directives with Request Definitions locations * fix ts issue on graphql 15
1 parent 86339fb commit 01421ae

File tree

3 files changed

+38
-1
lines changed

3 files changed

+38
-1
lines changed

.changeset/serious-chairs-compete.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+
fix(no-unreachable-types): ignore directives with Request Definitions locations

packages/plugin/src/rules/no-unreachable-types.ts

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { ASTNode, ASTVisitor, GraphQLSchema, isInterfaceType, Kind, NameNode, visit } from 'graphql';
1+
import { ASTNode, ASTVisitor, GraphQLSchema, isInterfaceType, Kind, NameNode, visit, DirectiveLocation } from 'graphql';
22
import lowerCase from 'lodash.lowercase';
33
import { GraphQLESLintRule } from '../types';
44
import { getTypeName, requireGraphQLSchemaFromContext } from '../utils';
@@ -26,6 +26,17 @@ type ReachableTypes = Set<string>;
2626

2727
let reachableTypesCache: ReachableTypes;
2828

29+
const RequestDirectiveLocations = new Set<string>([
30+
DirectiveLocation.QUERY,
31+
DirectiveLocation.MUTATION,
32+
DirectiveLocation.SUBSCRIPTION,
33+
DirectiveLocation.FIELD,
34+
DirectiveLocation.FRAGMENT_DEFINITION,
35+
DirectiveLocation.FRAGMENT_SPREAD,
36+
DirectiveLocation.INLINE_FRAGMENT,
37+
DirectiveLocation.VARIABLE_DEFINITION,
38+
]);
39+
2940
function getReachableTypes(schema: GraphQLSchema): ReachableTypes {
3041
// We don't want cache reachableTypes on test environment
3142
// Otherwise reachableTypes will be same for all tests
@@ -74,6 +85,13 @@ function getReachableTypes(schema: GraphQLSchema): ReachableTypes {
7485
visit(type.astNode, visitor);
7586
}
7687
}
88+
89+
for (const node of schema.getDirectives()) {
90+
if (node.locations.some(location => RequestDirectiveLocations.has(location))) {
91+
reachableTypes.add(node.name);
92+
}
93+
}
94+
7795
reachableTypesCache = reachableTypes;
7896
return reachableTypesCache;
7997
}

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,20 @@ ruleTester.runGraphQLTests('no-unreachable-types', rule, {
132132
directive @good on SCHEMA
133133
`),
134134
},
135+
{
136+
name: 'should ignore directive with request locations',
137+
...useSchema(/* GraphQL */ `
138+
directive @q on QUERY
139+
directive @w on MUTATION
140+
directive @e on SUBSCRIPTION
141+
directive @r on FIELD
142+
directive @t on FRAGMENT_DEFINITION
143+
directive @y on FRAGMENT_SPREAD
144+
directive @u on INLINE_FRAGMENT
145+
directive @i on VARIABLE_DEFINITION
146+
type Query
147+
`),
148+
},
135149
],
136150
invalid: [
137151
{

0 commit comments

Comments
 (0)