Skip to content

Commit 0fe0f68

Browse files
authored
input-name: Ignore non-Mutation fields. (#156)
* input-name: Ignore non-Mutation fields. * add changeset
1 parent 5804c50 commit 0fe0f68

File tree

3 files changed

+21
-5
lines changed

3 files changed

+21
-5
lines changed

.changeset/wise-paws-speak.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@graphql-eslint/eslint-plugin': patch
3+
---
4+
5+
Fix a bug in the input-name rule to make sure it only checks fields on the Mutation type

packages/plugin/src/rules/input-name.ts

+13-4
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,15 @@ const rule: GraphQLESLintRule<InputNameRuleConfig> = {
3030
],
3131
},
3232
create(context) {
33+
const isMutationType = node => {
34+
return (
35+
(node.type === 'ObjectTypeDefinition' || node.type === 'ObjectTypeExtension') && node.name.value === 'Mutation'
36+
);
37+
};
38+
3339
const listeners = {
3440
'FieldDefinition > InputValueDefinition': node => {
35-
if (node.name.value !== 'input') {
41+
if (node.name.value !== 'input' && isMutationType(node.parent.parent)) {
3642
context.report({
3743
node: node.name,
3844
message: `Input "${node.name.value}" should be called "input"`,
@@ -50,10 +56,13 @@ const rule: GraphQLESLintRule<InputNameRuleConfig> = {
5056
return currentNode;
5157
};
5258

53-
const mutationName = `${findInputType(node).parent.name.value}Input`;
59+
const inputValueNode = findInputType(node);
60+
if (isMutationType(inputValueNode.parent.parent)) {
61+
const mutationName = `${inputValueNode.parent.name.value}Input`;
5462

55-
if (node.name.value !== mutationName) {
56-
context.report({ node, message: `InputType "${node.name.value}" name should be "${mutationName}"` });
63+
if (node.name.value !== mutationName) {
64+
context.report({ node, message: `InputType "${node.name.value}" name should be "${mutationName}"` });
65+
}
5766
}
5867
};
5968
}

packages/plugin/tests/input-name.spec.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ ruleTester.runGraphQLTests('input-name', rule, {
2323
options: [{ checkInputType: true }],
2424
},
2525
'type Mutation { CreateMessage(input: String): String }',
26+
'extend type Mutation { CreateMessage(input: String): String }',
27+
'type Query { message(id: ID): Message }',
2628
],
2729
invalid: [
2830
{
@@ -39,7 +41,7 @@ ruleTester.runGraphQLTests('input-name', rule, {
3941
errors: [{ message: 'InputType "String" name should be "SetMessageInput"' }],
4042
},
4143
{
42-
code: 'type Mutaton { SetMessage(hello: SetMessageInput): String }',
44+
code: 'type Mutation { SetMessage(hello: SetMessageInput): String }',
4345
options: [{ checkInputType: true }],
4446
errors: [{ message: 'Input "hello" should be called "input"' }],
4547
},

0 commit comments

Comments
 (0)