1
- import {
2
- Kind ,
3
- NamedTypeNode ,
4
- ObjectTypeExtensionNode ,
5
- ObjectTypeDefinitionNode ,
6
- InputValueDefinitionNode ,
7
- } from 'graphql' ;
1
+ import { Kind , NamedTypeNode , ObjectTypeExtensionNode , ObjectTypeDefinitionNode , NameNode } from 'graphql' ;
8
2
import { GraphQLESLintRule } from '../types' ;
9
3
import { GraphQLESTreeNode } from '../estree-parser' ;
10
4
import { GraphQLESLintRuleListener } from '../testkit' ;
@@ -26,6 +20,7 @@ const isMutationType = (node: ObjectTypeNode): boolean => isObjectType(node) &&
26
20
const rule : GraphQLESLintRule < [ InputNameRuleConfig ] > = {
27
21
meta : {
28
22
type : 'suggestion' ,
23
+ hasSuggestions : true ,
29
24
docs : {
30
25
description :
31
26
'Require mutation argument to be always called "input" and input type to be called Mutation name + "Input".\nUsing the same name for all input parameters will make your schemas easier to consume and more predictable. Using the same name as mutation for InputType will make it easier to find mutations that InputType belongs to.' ,
@@ -103,12 +98,18 @@ const rule: GraphQLESLintRule<[InputNameRuleConfig]> = {
103
98
( options . checkMutations && isMutationType ( node ) ) || ( options . checkQueries && isQueryType ( node ) ) ;
104
99
105
100
const listeners : GraphQLESLintRuleListener = {
106
- 'FieldDefinition > InputValueDefinition[name.value!=input]' ( node : GraphQLESTreeNode < InputValueDefinitionNode > ) {
107
- if ( shouldCheckType ( ( node as any ) . parent . parent ) ) {
108
- const name = node . name . value ;
101
+ 'FieldDefinition > InputValueDefinition[name.value!=input] > Name ' ( node : GraphQLESTreeNode < NameNode > ) {
102
+ if ( shouldCheckType ( ( node as any ) . parent . parent . parent ) ) {
103
+ const inputName = node . value ;
109
104
context . report ( {
110
- node : node . name ,
111
- message : `Input "${ name } " should be called "input"` ,
105
+ node,
106
+ message : `Input \`${ inputName } \` should be called \`input\`.` ,
107
+ suggest : [
108
+ {
109
+ desc : 'Rename to `input`' ,
110
+ fix : fixer => fixer . replaceText ( node as any , 'input' ) ,
111
+ } ,
112
+ ] ,
112
113
} ) ;
113
114
}
114
115
} ,
@@ -134,7 +135,13 @@ const rule: GraphQLESLintRule<[InputNameRuleConfig]> = {
134
135
) {
135
136
context . report ( {
136
137
node : node . name ,
137
- message : `InputType "${ name } " name should be "${ mutationName } "` ,
138
+ message : `Input type \`${ name } \` name should be \`${ mutationName } \`.` ,
139
+ suggest : [
140
+ {
141
+ desc : `Rename to \`${ mutationName } \`` ,
142
+ fix : fixer => fixer . replaceText ( node as any , mutationName ) ,
143
+ } ,
144
+ ] ,
138
145
} ) ;
139
146
}
140
147
}
0 commit comments