Skip to content

Commit 7a04e1e

Browse files
committed
Added support for direcrives in no-unreachable-types rule
1 parent 9a40163 commit 7a04e1e

File tree

4 files changed

+46
-16
lines changed

4 files changed

+46
-16
lines changed

.changeset/tender-islands-call.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+
Added support for direcrives in `no-unreachable-types` rule

packages/plugin/src/graphql-ast.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import {
1515
isInputObjectType,
1616
isListType,
1717
isNonNullType,
18+
GraphQLDirective,
1819
} from 'graphql';
1920

2021
export function createReachableTypesService(schema: GraphQLSchema): () => Set<string>;
@@ -36,12 +37,20 @@ export function createReachableTypesService(schema?: GraphQLSchema): () => Set<s
3637
export function collectReachableTypes(schema: GraphQLSchema): Set<string> {
3738
const reachableTypes = new Set<string>();
3839

40+
collectFromDirectives(schema.getDirectives());
3941
collectFrom(schema.getQueryType());
4042
collectFrom(schema.getMutationType());
4143
collectFrom(schema.getSubscriptionType());
4244

4345
return reachableTypes;
4446

47+
function collectFromDirectives(directives: readonly GraphQLDirective[]) {
48+
for (const directive of directives || []) {
49+
reachableTypes.add(directive.name);
50+
directive.args.forEach(collectFromArgument);
51+
}
52+
}
53+
4554
function collectFrom(type?: GraphQLNamedType): void {
4655
if (type && shouldCollect(type.name)) {
4756
if (isObjectType(type)) {

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ const rule: GraphQLESLintRule = {
6363
}
6464

6565
return {
66+
DirectiveDefinition: ensureReachability,
6667
ObjectTypeDefinition: ensureReachability,
6768
ObjectTypeExtension: ensureReachability,
6869
InterfaceTypeDefinition: ensureReachability,

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

Lines changed: 31 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,21 @@ function useSchema(
2020

2121
ruleTester.runGraphQLTests('no-unreachable-types', rule, {
2222
valid: [
23+
useSchema(/* GraphQL */ `
24+
directive @omit(unlessContains: [String], unlessContainsTypes: OmitTypes) on FIELD_DEFINITION
25+
26+
"Types of 'unlessContainsTypes' omit"
27+
enum OmitTypes {
28+
"Scalar fields"
29+
scalar
30+
"Complex type fields"
31+
nonScalar
32+
}
33+
34+
type Query {
35+
foo: String
36+
}
37+
`),
2338
useSchema(/* GraphQL */ `
2439
type Query {
2540
me: User
@@ -95,22 +110,22 @@ ruleTester.runGraphQLTests('no-unreachable-types', rule, {
95110
}
96111
`),
97112
useSchema(/* GraphQL */ `
98-
interface User {
99-
id: String
100-
}
101-
102-
type SuperUser implements User {
103-
id: String
104-
}
105-
106-
extend type SuperUser {
107-
detail: String
108-
}
109-
110-
type Query {
111-
user: User!
112-
}
113-
`),
113+
interface User {
114+
id: String
115+
}
116+
117+
type SuperUser implements User {
118+
id: String
119+
}
120+
121+
extend type SuperUser {
122+
detail: String
123+
}
124+
125+
type Query {
126+
user: User!
127+
}
128+
`),
114129
],
115130
invalid: [
116131
{

0 commit comments

Comments
 (0)