11import { GraphQLESLintRule } from '../types' ;
22
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+ } ;
89
9- const rule : GraphQLESLintRule < InputNameRuleConfig > = {
10+ const rule : GraphQLESLintRule < InputNameRuleConfig [ ] > = {
1011 meta : {
1112 type : 'suggestion' ,
1213 docs : {
@@ -52,31 +53,65 @@ const rule: GraphQLESLintRule<InputNameRuleConfig> = {
5253 properties : {
5354 checkInputType : {
5455 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' ,
5673 } ,
5774 } ,
5875 additionalProperties : false ,
5976 } ,
6077 ] ,
6178 } ,
6279 create ( context ) {
80+ const options : InputNameRuleConfig = {
81+ caseSensitiveInputType : true ,
82+ checkInputType : false ,
83+ checkMutations : true ,
84+ checkQueries : false ,
85+ ...context ?. options ?. [ 0 ] ,
86+ } ;
87+
6388 const isMutationType = node => {
6489 return (
6590 ( node . type === 'ObjectTypeDefinition' || node . type === 'ObjectTypeExtension' ) && node . name . value === 'Mutation'
6691 ) ;
6792 } ;
6893
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+
69103 const listeners = {
70104 'FieldDefinition > InputValueDefinition' : node => {
71- if ( node . name . value !== 'input' && isMutationType ( node . parent . parent ) ) {
105+ if ( node . name . value !== 'input' && shouldCheckType ( node . parent . parent ) ) {
72106 context . report ( {
73107 node : node . name ,
74108 message : `Input "${ node . name . value } " should be called "input"` ,
75109 } ) ;
76110 }
77111 } ,
78112 } ;
79- if ( context . options && context . options [ 0 ] && context . options [ 0 ] . checkInputType ) {
113+
114+ if ( options ?. checkInputType ) {
80115 listeners [ 'FieldDefinition > InputValueDefinition NamedType' ] = node => {
81116 const findInputType = item => {
82117 let currentNode = item ;
@@ -87,11 +122,17 @@ const rule: GraphQLESLintRule<InputNameRuleConfig> = {
87122 } ;
88123
89124 const inputValueNode = findInputType ( node ) ;
90- if ( isMutationType ( inputValueNode . parent . parent ) ) {
125+ if ( shouldCheckType ( inputValueNode . parent . parent ) ) {
91126 const mutationName = `${ inputValueNode . parent . name . value } Input` ;
92127
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+ }
95136 }
96137 }
97138 } ;
0 commit comments