-
-
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.
add new rule
require-nullable-fields-with-oneof
(#1330)
- Loading branch information
1 parent
2c68532
commit bab45cc
Showing
11 changed files
with
236 additions
and
13 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 | ||
--- | ||
|
||
add new rule `require-nullable-fields-with-oneof` |
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,38 @@ | ||
# `require-nullable-fields-with-oneof` | ||
|
||
- Category: `Schema` | ||
- Rule name: `@graphql-eslint/require-nullable-fields-with-oneof` | ||
- Requires GraphQL Schema: `false` [ℹ️](../../README.md#extended-linting-rules-with-graphql-schema) | ||
- Requires GraphQL Operations: `false` | ||
[ℹ️](../../README.md#extended-linting-rules-with-siblings-operations) | ||
|
||
Require are `input` or `type` fields be non nullable with `@oneOf` directive. | ||
|
||
## Usage Examples | ||
|
||
### Incorrect | ||
|
||
```graphql | ||
# eslint @graphql-eslint/require-nullable-fields-with-oneof: 'error' | ||
|
||
input Input @oneOf { | ||
foo: String! | ||
b: Int | ||
} | ||
``` | ||
|
||
### Correct | ||
|
||
```graphql | ||
# eslint @graphql-eslint/require-nullable-fields-with-oneof: 'error' | ||
|
||
input Input @oneOf { | ||
foo: String | ||
bar: Int | ||
} | ||
``` | ||
|
||
## Resources | ||
|
||
- [Rule source](../../packages/plugin/src/rules/require-nullable-fields-with-oneof.ts) | ||
- [Test source](../../packages/plugin/tests/require-nullable-fields-with-oneof.spec.ts) |
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
64 changes: 64 additions & 0 deletions
64
packages/plugin/src/rules/require-nullable-fields-with-oneof.ts
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,64 @@ | ||
import { GraphQLESLintRule } from '../types'; | ||
import { InputObjectTypeDefinitionNode, Kind, ObjectTypeDefinitionNode } from 'graphql'; | ||
import { GraphQLESTreeNode } from '../estree-converter'; | ||
|
||
const RULE_ID = 'require-nullable-fields-with-oneof'; | ||
|
||
export const rule: GraphQLESLintRule = { | ||
meta: { | ||
type: 'suggestion', | ||
docs: { | ||
category: 'Schema', | ||
description: 'Require are `input` or `type` fields be non nullable with `@oneOf` directive.', | ||
url: `https://github.com/B2o5T/graphql-eslint/blob/master/docs/rules/${RULE_ID}.md`, | ||
examples: [ | ||
{ | ||
title: 'Incorrect', | ||
code: /* GraphQL */ ` | ||
input Input @oneOf { | ||
foo: String! | ||
b: Int | ||
} | ||
`, | ||
}, | ||
{ | ||
title: 'Correct', | ||
code: /* GraphQL */ ` | ||
input Input @oneOf { | ||
foo: String | ||
bar: Int | ||
} | ||
`, | ||
}, | ||
], | ||
}, | ||
messages: { | ||
[RULE_ID]: 'Field `{{fieldName}}` must be nullable.', | ||
}, | ||
schema: [], | ||
}, | ||
create(context) { | ||
return { | ||
'Directive[name.value=oneOf]'(node: { | ||
parent: GraphQLESTreeNode<InputObjectTypeDefinitionNode | ObjectTypeDefinitionNode>; | ||
}) { | ||
const isTypeOrInput = [ | ||
Kind.OBJECT_TYPE_DEFINITION, | ||
Kind.INPUT_OBJECT_TYPE_DEFINITION, | ||
].includes(node.parent.kind); | ||
if (!isTypeOrInput) { | ||
return; | ||
} | ||
for (const field of node.parent.fields) { | ||
if (field.gqlType.kind === Kind.NON_NULL_TYPE) { | ||
context.report({ | ||
node: field.name, | ||
messageId: RULE_ID, | ||
data: { fieldName: field.name.value }, | ||
}); | ||
} | ||
} | ||
}, | ||
}; | ||
}, | ||
}; |
40 changes: 40 additions & 0 deletions
40
packages/plugin/tests/__snapshots__/require-nullable-fields-with-oneof.spec.md
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,40 @@ | ||
// Vitest Snapshot v1 | ||
|
||
exports[`should validate \`input\` 1`] = ` | ||
#### ⌨️ Code | ||
|
||
1 | input Input @oneOf { | ||
2 | foo: String! | ||
3 | bar: [Int]! | ||
4 | } | ||
|
||
#### ❌ Error 1/2 | ||
|
||
1 | input Input @oneOf { | ||
> 2 | foo: String! | ||
| ^^^ Field \`foo\` must be nullable. | ||
3 | bar: [Int]! | ||
|
||
#### ❌ Error 2/2 | ||
|
||
2 | foo: String! | ||
> 3 | bar: [Int]! | ||
| ^^^ Field \`bar\` must be nullable. | ||
4 | } | ||
`; | ||
|
||
exports[`should validate \`type\` 1`] = ` | ||
#### ⌨️ Code | ||
|
||
1 | type Type @oneOf { | ||
2 | foo: String! | ||
3 | bar: Int | ||
4 | } | ||
|
||
#### ❌ Error | ||
|
||
1 | type Type @oneOf { | ||
> 2 | foo: String! | ||
| ^^^ Field \`foo\` must be nullable. | ||
3 | bar: Int | ||
`; |
46 changes: 46 additions & 0 deletions
46
packages/plugin/tests/require-nullable-fields-with-oneof.spec.ts
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,46 @@ | ||
import { GraphQLRuleTester } from '../src'; | ||
import { rule } from '../src/rules/require-nullable-fields-with-oneof'; | ||
|
||
const ruleTester = new GraphQLRuleTester(); | ||
|
||
ruleTester.runGraphQLTests('require-nullable-fields-with-oneof', rule, { | ||
valid: [ | ||
/* GraphQL */ ` | ||
input Input @oneOf { | ||
foo: [String] | ||
bar: Int | ||
} | ||
`, | ||
/* GraphQL */ ` | ||
type User @oneOf { | ||
foo: String | ||
bar: [Int!] | ||
} | ||
`, | ||
], | ||
invalid: [ | ||
{ | ||
name: 'should validate `input`', | ||
code: /* GraphQL */ ` | ||
input Input @oneOf { | ||
foo: String! | ||
bar: [Int]! | ||
} | ||
`, | ||
errors: [ | ||
{ message: 'Field `foo` must be nullable.' }, | ||
{ message: 'Field `bar` must be nullable.' }, | ||
], | ||
}, | ||
{ | ||
name: 'should validate `type`', | ||
code: /* GraphQL */ ` | ||
type Type @oneOf { | ||
foo: String! | ||
bar: Int | ||
} | ||
`, | ||
errors: [{ message: 'Field `foo` must be nullable.' }], | ||
}, | ||
], | ||
}); |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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