|
1 |
| -import { ASTNode, ASTVisitor, GraphQLSchema, isInterfaceType, Kind, NameNode, visit } from 'graphql'; |
| 1 | +import { ASTNode, ASTVisitor, GraphQLSchema, isInterfaceType, Kind, NameNode, visit, DirectiveLocation } from 'graphql'; |
2 | 2 | import lowerCase from 'lodash.lowercase';
|
3 | 3 | import { GraphQLESLintRule } from '../types';
|
4 | 4 | import { getTypeName, requireGraphQLSchemaFromContext } from '../utils';
|
@@ -26,6 +26,17 @@ type ReachableTypes = Set<string>;
|
26 | 26 |
|
27 | 27 | let reachableTypesCache: ReachableTypes;
|
28 | 28 |
|
| 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 | + |
29 | 40 | function getReachableTypes(schema: GraphQLSchema): ReachableTypes {
|
30 | 41 | // We don't want cache reachableTypes on test environment
|
31 | 42 | // Otherwise reachableTypes will be same for all tests
|
@@ -74,6 +85,13 @@ function getReachableTypes(schema: GraphQLSchema): ReachableTypes {
|
74 | 85 | visit(type.astNode, visitor);
|
75 | 86 | }
|
76 | 87 | }
|
| 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 | + |
77 | 95 | reachableTypesCache = reachableTypes;
|
78 | 96 | return reachableTypesCache;
|
79 | 97 | }
|
|
0 commit comments