1
1
import { GraphQLESLintRule } from '../types' ;
2
2
3
- type InputNameRuleConfig = [
4
- {
5
- checkInputType ?: boolean ;
6
- }
7
- ] ;
3
+ type InputNameRuleConfig = {
4
+ checkInputType ?: boolean ;
5
+ caseSensitiveInputType ?: boolean ;
6
+ checkQueries ?: boolean ;
7
+ checkMutations ?: boolean ;
8
+ } ;
8
9
9
- const rule : GraphQLESLintRule < InputNameRuleConfig > = {
10
+ const rule : GraphQLESLintRule < InputNameRuleConfig [ ] > = {
10
11
meta : {
11
12
type : 'suggestion' ,
12
13
docs : {
@@ -52,31 +53,65 @@ const rule: GraphQLESLintRule<InputNameRuleConfig> = {
52
53
properties : {
53
54
checkInputType : {
54
55
type : 'boolean' ,
55
- default : 'true' ,
56
+ default : false ,
57
+ description : 'Check that the input type name follows the convention <mutationName>Input' ,
58
+ } ,
59
+ caseSensitiveInputType : {
60
+ type : 'boolean' ,
61
+ default : true ,
62
+ description : 'Allow for case discrepancies in the input type name' ,
63
+ } ,
64
+ checkQueries : {
65
+ type : 'boolean' ,
66
+ default : false ,
67
+ description : 'Apply the rule to Queries' ,
68
+ } ,
69
+ checkMutations : {
70
+ type : 'boolean' ,
71
+ default : true ,
72
+ description : 'Apply the rule to Mutations' ,
56
73
} ,
57
74
} ,
58
75
additionalProperties : false ,
59
76
} ,
60
77
] ,
61
78
} ,
62
79
create ( context ) {
80
+ const options : InputNameRuleConfig = {
81
+ caseSensitiveInputType : true ,
82
+ checkInputType : false ,
83
+ checkMutations : true ,
84
+ checkQueries : false ,
85
+ ...context ?. options ?. [ 0 ] ,
86
+ } ;
87
+
63
88
const isMutationType = node => {
64
89
return (
65
90
( node . type === 'ObjectTypeDefinition' || node . type === 'ObjectTypeExtension' ) && node . name . value === 'Mutation'
66
91
) ;
67
92
} ;
68
93
94
+ const isQueryType = node => {
95
+ return (
96
+ ( node . type === 'ObjectTypeDefinition' || node . type === 'ObjectTypeExtension' ) && node . name . value === 'Query'
97
+ ) ;
98
+ } ;
99
+
100
+ const shouldCheckType = node =>
101
+ ( options . checkMutations && isMutationType ( node ) ) || ( options . checkQueries && isQueryType ( node ) ) ;
102
+
69
103
const listeners = {
70
104
'FieldDefinition > InputValueDefinition' : node => {
71
- if ( node . name . value !== 'input' && isMutationType ( node . parent . parent ) ) {
105
+ if ( node . name . value !== 'input' && shouldCheckType ( node . parent . parent ) ) {
72
106
context . report ( {
73
107
node : node . name ,
74
108
message : `Input "${ node . name . value } " should be called "input"` ,
75
109
} ) ;
76
110
}
77
111
} ,
78
112
} ;
79
- if ( context . options && context . options [ 0 ] && context . options [ 0 ] . checkInputType ) {
113
+
114
+ if ( options ?. checkInputType ) {
80
115
listeners [ 'FieldDefinition > InputValueDefinition NamedType' ] = node => {
81
116
const findInputType = item => {
82
117
let currentNode = item ;
@@ -87,11 +122,17 @@ const rule: GraphQLESLintRule<InputNameRuleConfig> = {
87
122
} ;
88
123
89
124
const inputValueNode = findInputType ( node ) ;
90
- if ( isMutationType ( inputValueNode . parent . parent ) ) {
125
+ if ( shouldCheckType ( inputValueNode . parent . parent ) ) {
91
126
const mutationName = `${ inputValueNode . parent . name . value } Input` ;
92
127
93
- if ( node . name . value !== mutationName ) {
94
- context . report ( { node, message : `InputType "${ node . name . value } " name should be "${ mutationName } "` } ) ;
128
+ if ( options . caseSensitiveInputType ) {
129
+ if ( node . name . value !== mutationName ) {
130
+ context . report ( { node, message : `InputType "${ node . name . value } " name should be "${ mutationName } "` } ) ;
131
+ }
132
+ } else {
133
+ if ( node . name . value . toLowerCase ( ) !== mutationName . toLowerCase ( ) ) {
134
+ context . report ( { node, message : `InputType "${ node . name . value } " name should be "${ mutationName } "` } ) ;
135
+ }
95
136
}
96
137
}
97
138
} ;
0 commit comments