-
-
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-type-pattern-with-oneof
(#1331)
- Loading branch information
1 parent
bab45cc
commit 0f7afa5
Showing
9 changed files
with
205 additions
and
3 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-type-pattern-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,39 @@ | ||
# `require-type-pattern-with-oneof` | ||
|
||
- Category: `Schema` | ||
- Rule name: `@graphql-eslint/require-type-pattern-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) | ||
|
||
Enforce types with `@oneOf` directive have `error` and `ok` fields. | ||
|
||
## Usage Examples | ||
|
||
### Correct | ||
|
||
```graphql | ||
# eslint @graphql-eslint/require-type-pattern-with-oneof: 'error' | ||
|
||
type Mutation { | ||
doSomething: DoSomethingMutationResult! | ||
} | ||
|
||
interface Error { | ||
message: String! | ||
} | ||
|
||
type DoSomethingMutationResult @oneOf { | ||
ok: DoSomethingSuccess | ||
error: Error | ||
} | ||
|
||
type DoSomethingSuccess { | ||
# ... | ||
} | ||
``` | ||
|
||
## Resources | ||
|
||
- [Rule source](../../packages/plugin/src/rules/require-type-pattern-with-oneof.ts) | ||
- [Test source](../../packages/plugin/tests/require-type-pattern-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
66 changes: 66 additions & 0 deletions
66
packages/plugin/src/rules/require-type-pattern-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,66 @@ | ||
import { GraphQLESLintRule } from '../types'; | ||
import { GraphQLESTreeNode } from '../estree-converter'; | ||
import { ObjectTypeDefinitionNode } from 'graphql/index'; | ||
|
||
const RULE_ID = 'require-type-pattern-with-oneof'; | ||
|
||
export const rule: GraphQLESLintRule = { | ||
meta: { | ||
type: 'suggestion', | ||
docs: { | ||
category: 'Schema', | ||
description: 'Enforce types with `@oneOf` directive have `error` and `ok` fields.', | ||
url: `https://github.com/B2o5T/graphql-eslint/blob/master/docs/rules/${RULE_ID}.md`, | ||
examples: [ | ||
{ | ||
title: 'Correct', | ||
code: /* GraphQL */ ` | ||
type Mutation { | ||
doSomething: DoSomethingMutationResult! | ||
} | ||
interface Error { | ||
message: String! | ||
} | ||
type DoSomethingMutationResult @oneOf { | ||
ok: DoSomethingSuccess | ||
error: Error | ||
} | ||
type DoSomethingSuccess { | ||
# ... | ||
} | ||
`, | ||
}, | ||
], | ||
}, | ||
messages: { | ||
[RULE_ID]: 'Type `{{typeName}}` should have `{{fieldName}}` field.', | ||
}, | ||
schema: [], | ||
}, | ||
create(context) { | ||
return { | ||
'Directive[name.value=oneOf][parent.kind=ObjectTypeDefinition]'({ | ||
parent, | ||
}: { | ||
parent: GraphQLESTreeNode<ObjectTypeDefinitionNode>; | ||
}) { | ||
const requiredFields = ['error', 'ok']; | ||
for (const fieldName of requiredFields) { | ||
if (!parent.fields.some(field => field.name.value === fieldName)) { | ||
context.report({ | ||
node: parent.name, | ||
messageId: RULE_ID, | ||
data: { | ||
typeName: parent.name.value, | ||
fieldName, | ||
}, | ||
}); | ||
} | ||
} | ||
}, | ||
}; | ||
}, | ||
}; |
31 changes: 31 additions & 0 deletions
31
packages/plugin/tests/__snapshots__/require-type-pattern-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,31 @@ | ||
// Vitest Snapshot v1 | ||
|
||
exports[`should validate \`error\` field 1`] = ` | ||
#### ⌨️ Code | ||
|
||
1 | type T @oneOf { | ||
2 | ok: Ok | ||
3 | err: Error | ||
4 | } | ||
|
||
#### ❌ Error | ||
|
||
> 1 | type T @oneOf { | ||
| ^ Type \`T\` should have \`error\` field. | ||
2 | ok: Ok | ||
`; | ||
|
||
exports[`should validate \`ok\` field 1`] = ` | ||
#### ⌨️ Code | ||
|
||
1 | type T @oneOf { | ||
2 | notok: Ok | ||
3 | error: Error | ||
4 | } | ||
|
||
#### ❌ Error | ||
|
||
> 1 | type T @oneOf { | ||
| ^ Type \`T\` should have \`ok\` field. | ||
2 | notok: Ok | ||
`; |
55 changes: 55 additions & 0 deletions
55
packages/plugin/tests/require-type-pattern-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,55 @@ | ||
import { GraphQLRuleTester } from '../src'; | ||
import { rule } from '../src/rules/require-type-pattern-with-oneof'; | ||
|
||
const ruleTester = new GraphQLRuleTester(); | ||
|
||
ruleTester.runGraphQLTests('require-type-pattern-with-oneof', rule, { | ||
valid: [ | ||
/* GraphQL */ ` | ||
type T @oneOf { | ||
ok: Ok | ||
error: Error | ||
} | ||
`, | ||
{ | ||
name: 'should ignore types without `@oneOf` directive', | ||
code: /* GraphQL */ ` | ||
type T { | ||
notok: Ok | ||
err: Error | ||
} | ||
`, | ||
}, | ||
{ | ||
name: 'should validate only `type` with `@oneOf` directive', | ||
code: /* GraphQL */ ` | ||
input I { | ||
notok: Ok | ||
err: Error | ||
} | ||
`, | ||
}, | ||
], | ||
invalid: [ | ||
{ | ||
name: 'should validate `ok` field', | ||
code: /* GraphQL */ ` | ||
type T @oneOf { | ||
notok: Ok | ||
error: Error | ||
} | ||
`, | ||
errors: [{ message: 'Type `T` should have `ok` field.' }], | ||
}, | ||
{ | ||
name: 'should validate `error` field', | ||
code: /* GraphQL */ ` | ||
type T @oneOf { | ||
ok: Ok | ||
err: Error | ||
} | ||
`, | ||
errors: [{ message: 'Type `T` should have `error` field.' }], | ||
}, | ||
], | ||
}); |