-
-
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.
- Loading branch information
1 parent
e26a5d4
commit b093f88
Showing
8 changed files
with
311 additions
and
2 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: selection-set-depth |
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,56 @@ | ||
# `selection-set-depth` | ||
|
||
- Category: `Best Practices` | ||
- Rule name: `@graphql-eslint/selection-set-depth` | ||
- Requires GraphQL Schema: `false` [ℹ️](../../README.md#extended-linting-rules-with-graphql-schema) | ||
- Requires GraphQL Operations: `true` [ℹ️](../../README.md#extended-linting-rules-with-siblings-operations) | ||
|
||
Limit the complexity of the GraphQL operations solely by their depth. Based on https://github.com/stems/graphql-depth-limit . | ||
|
||
## Usage Examples | ||
|
||
### Incorrect | ||
|
||
```graphql | ||
# eslint @graphql-eslint/selection-set-depth: ["error", [{"maxDepth":1}]] | ||
|
||
query deep2 { | ||
viewer { # Level 0 | ||
albums { # Level 1 | ||
title # Level 2 | ||
} | ||
} | ||
} | ||
``` | ||
|
||
### Correct | ||
|
||
```graphql | ||
# eslint @graphql-eslint/selection-set-depth: ["error", [{"maxDepth":4}]] | ||
|
||
query deep2 { | ||
viewer { # Level 0 | ||
albums { # Level 1 | ||
title # Level 2 | ||
} | ||
} | ||
} | ||
``` | ||
|
||
### Correct (ignored field) | ||
|
||
```graphql | ||
# eslint @graphql-eslint/selection-set-depth: ["error", [{"maxDepth":1,"ignore":["albums"]}]] | ||
|
||
query deep2 { | ||
viewer { # Level 0 | ||
albums { # Level 1 | ||
title # Level 2 | ||
} | ||
} | ||
} | ||
``` | ||
|
||
## Config Schema | ||
|
||
The schema defines the following properties: |
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
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,133 @@ | ||
import { GraphQLESLintRule } from '../types'; | ||
import depthLimit from 'graphql-depth-limit'; | ||
import { DocumentNode, FragmentDefinitionNode, GraphQLError, Kind, OperationDefinitionNode } from 'graphql'; | ||
import { GraphQLESTreeNode } from '../estree-parser'; | ||
import { requireSiblingsOperations } from '../utils'; | ||
import { SiblingOperations } from '../sibling-operations'; | ||
|
||
type SelectionSetDepthRuleConfig = [{ maxDepth: number; ignore?: string[] }]; | ||
|
||
const rule: GraphQLESLintRule<SelectionSetDepthRuleConfig> = { | ||
meta: { | ||
docs: { | ||
category: 'Best Practices', | ||
description: `Limit the complexity of the GraphQL operations solely by their depth. Based on https://github.com/stems/graphql-depth-limit .`, | ||
url: `https://github.com/dotansimha/graphql-eslint/blob/master/docs/rules/selection-set-depth.md`, | ||
requiresSchema: false, | ||
requiresSiblings: true, | ||
examples: [ | ||
{ | ||
title: 'Incorrect', | ||
usage: [{ maxDepth: 1 }], | ||
code: ` | ||
query deep2 { | ||
viewer { # Level 0 | ||
albums { # Level 1 | ||
title # Level 2 | ||
} | ||
} | ||
} | ||
`, | ||
}, | ||
{ | ||
title: 'Correct', | ||
usage: [{ maxDepth: 4 }], | ||
code: ` | ||
query deep2 { | ||
viewer { # Level 0 | ||
albums { # Level 1 | ||
title # Level 2 | ||
} | ||
} | ||
} | ||
`, | ||
}, | ||
{ | ||
title: 'Correct (ignored field)', | ||
usage: [{ maxDepth: 1, ignore: ['albums'] }], | ||
code: ` | ||
query deep2 { | ||
viewer { # Level 0 | ||
albums { # Level 1 | ||
title # Level 2 | ||
} | ||
} | ||
} | ||
`, | ||
}, | ||
], | ||
}, | ||
type: 'suggestion', | ||
schema: { | ||
type: 'array', | ||
additionalItems: false, | ||
minItems: 1, | ||
maxItems: 1, | ||
items: { | ||
type: 'object', | ||
require: ['maxDepth'], | ||
properties: { | ||
maxDepth: { | ||
type: 'number', | ||
}, | ||
ignore: { | ||
type: 'array', | ||
items: { | ||
type: 'string', | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
create(context) { | ||
let siblings: SiblingOperations | null = null; | ||
|
||
try { | ||
siblings = requireSiblingsOperations('selection-set-depth', context); | ||
} catch (e) { | ||
// eslint-disable-next-line no-console | ||
console.warn( | ||
`Rule "selection-set-depth" works best with sibligns operations loaded. For more info: http://bit.ly/graphql-eslint-operations` | ||
); | ||
} | ||
|
||
const maxDepth = context.options[0].maxDepth; | ||
const ignore = context.options[0].ignore || []; | ||
const checkFn = depthLimit(maxDepth, { ignore }); | ||
|
||
return ['OperationDefinition', 'FragmentDefinition'].reduce((prev, nodeType) => { | ||
return { | ||
...prev, | ||
[nodeType]: (node: GraphQLESTreeNode<OperationDefinitionNode> | GraphQLESTreeNode<FragmentDefinitionNode>) => { | ||
try { | ||
const rawNode = node.rawNode(); | ||
const fragmentsInUse = siblings ? siblings.getFragmentsInUse(rawNode, true) : []; | ||
const document: DocumentNode = { | ||
kind: Kind.DOCUMENT, | ||
definitions: [rawNode, ...fragmentsInUse], | ||
}; | ||
|
||
checkFn({ | ||
getDocument: () => document, | ||
reportError: (error: GraphQLError) => { | ||
context.report({ | ||
loc: error.locations[0], | ||
message: error.message, | ||
}); | ||
}, | ||
}); | ||
} catch (e) { | ||
// eslint-disable-next-line no-console | ||
console.warn( | ||
`Rule "selection-set-depth" check failed due to a missing siblings operations. For more info: http://bit.ly/graphql-eslint-operations`, | ||
e | ||
); | ||
} | ||
}, | ||
}; | ||
}, {}); | ||
}, | ||
}; | ||
|
||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
import { GraphQLRuleTester } from '../src/testkit'; | ||
import rule from '../src/rules/selection-set-depth'; | ||
import { ParserOptions } from '../src/types'; | ||
|
||
const WITH_SIBLINGS = { | ||
parserOptions: <ParserOptions>{ | ||
operations: [ | ||
`fragment AlbumFields on Album { | ||
id | ||
}`, | ||
], | ||
}, | ||
}; | ||
|
||
const ruleTester = new GraphQLRuleTester(); | ||
|
||
ruleTester.runGraphQLTests('selection-set-depth', rule, { | ||
valid: [ | ||
{ | ||
options: [{ maxDepth: 2 }], | ||
code: /* GraphQL */ ` | ||
query deep2 { | ||
viewer { | ||
# Level 0 | ||
albums { | ||
# Level 1 | ||
title # Level 2 | ||
} | ||
} | ||
} | ||
`, | ||
}, | ||
{ | ||
...WITH_SIBLINGS, | ||
options: [{ maxDepth: 2 }], | ||
code: /* GraphQL */ ` | ||
query deep2 { | ||
viewer { | ||
albums { | ||
...AlbumFields | ||
} | ||
} | ||
} | ||
`, | ||
}, | ||
{ | ||
...WITH_SIBLINGS, | ||
options: [{ maxDepth: 1, ignore: ['albums'] }], | ||
code: /* GraphQL */ ` | ||
query deep2 { | ||
viewer { | ||
albums { | ||
...AlbumFields | ||
} | ||
} | ||
} | ||
`, | ||
}, | ||
], | ||
invalid: [ | ||
{ | ||
options: [{ maxDepth: 1 }], | ||
errors: [{ message: `'deep2' exceeds maximum operation depth of 1` }], | ||
code: /* GraphQL */ ` | ||
query deep2 { | ||
viewer { | ||
albums { | ||
title | ||
} | ||
} | ||
} | ||
`, | ||
}, | ||
{ | ||
...WITH_SIBLINGS, | ||
options: [{ maxDepth: 1 }], | ||
errors: [{ message: `'deep2' exceeds maximum operation depth of 1` }], | ||
code: /* GraphQL */ ` | ||
query deep2 { | ||
viewer { | ||
albums { | ||
...AlbumFields | ||
} | ||
} | ||
} | ||
`, | ||
}, | ||
], | ||
}); |
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