-
-
Notifications
You must be signed in to change notification settings - Fork 100
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
NEW RULE: no-unreachable-types rule (#243)
- Loading branch information
1 parent
10debb5
commit 625f083
Showing
9 changed files
with
468 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'@graphql-eslint/eslint-plugin': minor | ||
--- | ||
|
||
New rule: no-unreachable-types |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
# `no-unreachable-types` | ||
|
||
- Category: `Best Practices` | ||
- Rule name: `@graphql-eslint/no-unreachable-types` | ||
- Requires GraphQL Schema: `true` [ℹ️](../../README.md#extended-linting-rules-with-graphql-schema) | ||
- Requires GraphQL Operations: `false` [ℹ️](../../README.md#extended-linting-rules-with-siblings-operations) | ||
|
||
This rule allow you to enforce that all types have to reachable by root level fields (Query.*, Mutation.*, Subscription.*). | ||
|
||
## Usage Examples | ||
|
||
### Incorrect (field) | ||
|
||
```graphql | ||
# eslint @graphql-eslint/no-unreachable-types: ["error"] | ||
|
||
type Query { | ||
me: String | ||
} | ||
|
||
type User { # This is not used, so you'll get an error | ||
id: ID! | ||
name: String! | ||
} | ||
``` | ||
|
||
|
||
### Correct | ||
|
||
```graphql | ||
# eslint @graphql-eslint/no-unreachable-types: ["error"] | ||
|
||
type Query { | ||
me: User | ||
} | ||
|
||
type User { # This is now used, so you won't get an error | ||
id: ID! | ||
name: String! | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
import { | ||
GraphQLSchema, | ||
GraphQLFieldMap, | ||
GraphQLInputFieldMap, | ||
GraphQLField, | ||
GraphQLInputField, | ||
GraphQLInputType, | ||
GraphQLOutputType, | ||
GraphQLNamedType, | ||
GraphQLInterfaceType, | ||
GraphQLArgument, | ||
isObjectType, | ||
isInterfaceType, | ||
isUnionType, | ||
isInputObjectType, | ||
isListType, | ||
isNonNullType, | ||
} from 'graphql'; | ||
|
||
export function createReachableTypesService(schema: GraphQLSchema): () => Set<string>; | ||
export function createReachableTypesService(schema?: GraphQLSchema): () => Set<string> | null { | ||
if (schema) { | ||
let cache: Set<string> = null; | ||
return () => { | ||
if (!cache) { | ||
cache = collectReachableTypes(schema); | ||
} | ||
|
||
return cache; | ||
}; | ||
} | ||
|
||
return () => null; | ||
} | ||
|
||
export function collectReachableTypes(schema: GraphQLSchema): Set<string> { | ||
const reachableTypes = new Set<string>(); | ||
|
||
collectFrom(schema.getQueryType()); | ||
collectFrom(schema.getMutationType()); | ||
collectFrom(schema.getSubscriptionType()); | ||
|
||
return reachableTypes; | ||
|
||
function collectFrom(type?: GraphQLNamedType): void { | ||
if (type && shouldCollect(type.name)) { | ||
if (isObjectType(type) || isInterfaceType(type)) { | ||
collectFromFieldMap(type.getFields()); | ||
collectFromInterfaces(type.getInterfaces()); | ||
} else if (isUnionType(type)) { | ||
type.getTypes().forEach(collectFrom); | ||
} else if (isInputObjectType(type)) { | ||
collectFromInputFieldMap(type.getFields()); | ||
} | ||
} | ||
} | ||
|
||
function collectFromFieldMap(fieldMap: GraphQLFieldMap<any, any>): void { | ||
for (const fieldName in fieldMap) { | ||
collectFromField(fieldMap[fieldName]); | ||
} | ||
} | ||
|
||
function collectFromField(field: GraphQLField<any, any>): void { | ||
collectFromOutputType(field.type); | ||
field.args.forEach(collectFromArgument); | ||
} | ||
|
||
function collectFromArgument(arg: GraphQLArgument): void { | ||
collectFromInputType(arg.type); | ||
} | ||
|
||
function collectFromInputFieldMap(fieldMap: GraphQLInputFieldMap): void { | ||
for (const fieldName in fieldMap) { | ||
collectFromInputField(fieldMap[fieldName]); | ||
} | ||
} | ||
|
||
function collectFromInputField(field: GraphQLInputField): void { | ||
collectFromInputType(field.type); | ||
} | ||
|
||
function collectFromInterfaces(interfaces: GraphQLInterfaceType[]): void { | ||
if (interfaces) { | ||
interfaces.forEach(interfaceType => { | ||
collectFromFieldMap(interfaceType.getFields()); | ||
collectFromInterfaces(interfaceType.getInterfaces()); | ||
}); | ||
} | ||
} | ||
|
||
function collectFromOutputType(output: GraphQLOutputType): void { | ||
collectFrom(schema.getType(resolveName(output))); | ||
} | ||
|
||
function collectFromInputType(input: GraphQLInputType): void { | ||
collectFrom(schema.getType(resolveName(input))); | ||
} | ||
|
||
function resolveName(type: GraphQLOutputType | GraphQLInputType) { | ||
if (isListType(type) || isNonNullType(type)) { | ||
return resolveName(type.ofType); | ||
} | ||
|
||
return type.name; | ||
} | ||
|
||
function shouldCollect(name: string): boolean { | ||
if (!reachableTypes.has(name)) { | ||
reachableTypes.add(name); | ||
return true; | ||
} | ||
|
||
return false; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
import { GraphQLESLintRule } from '../types'; | ||
import { requireReachableTypesFromContext } from '../utils'; | ||
|
||
const UNREACHABLE_TYPE = 'UNREACHABLE_TYPE'; | ||
|
||
const rule: GraphQLESLintRule = { | ||
meta: { | ||
messages: { | ||
[UNREACHABLE_TYPE]: `Type "{{ typeName }}" is unreachable`, | ||
}, | ||
docs: { | ||
description: `Requires all types to be reachable at some level by root level fields`, | ||
category: 'Best Practices', | ||
url: `https://github.com/dotansimha/graphql-eslint/blob/master/docs/rules/no-unreachable-types.md`, | ||
requiresSchema: true, | ||
examples: [ | ||
{ | ||
title: 'Incorrect', | ||
code: /* GraphQL */ ` | ||
type User { | ||
id: ID! | ||
name: String | ||
} | ||
type Query { | ||
me: String | ||
} | ||
`, | ||
}, | ||
{ | ||
title: 'Correct', | ||
code: /* GraphQL */ ` | ||
type User { | ||
id: ID! | ||
name: String | ||
} | ||
type Query { | ||
me: User | ||
} | ||
`, | ||
}, | ||
], | ||
}, | ||
fixable: 'code', | ||
type: 'suggestion', | ||
}, | ||
create(context) { | ||
function ensureReachability(node) { | ||
const typeName = node.name.value; | ||
const reachableTypes = requireReachableTypesFromContext('no-unreachable-types', context); | ||
|
||
if (!reachableTypes.has(typeName)) { | ||
context.report({ | ||
node, | ||
messageId: UNREACHABLE_TYPE, | ||
data: { | ||
typeName, | ||
}, | ||
fix: fixer => fixer.removeRange(node.range) | ||
}); | ||
} | ||
} | ||
|
||
return { | ||
ObjectTypeDefinition: ensureReachability, | ||
ObjectTypeExtension: ensureReachability, | ||
InterfaceTypeDefinition: ensureReachability, | ||
InterfaceTypeExtension: ensureReachability, | ||
ScalarTypeDefinition: ensureReachability, | ||
ScalarTypeExtension: ensureReachability, | ||
InputObjectTypeDefinition: ensureReachability, | ||
InputObjectTypeExtension: ensureReachability, | ||
UnionTypeDefinition: ensureReachability, | ||
UnionTypeExtension: ensureReachability, | ||
EnumTypeDefinition: ensureReachability, | ||
EnumTypeExtension: ensureReachability, | ||
}; | ||
}, | ||
}; | ||
|
||
export default rule; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.