Skip to content

Commit

Permalink
fix: ignore arguments in `require-field-of-type-query-in-mutation-res…
Browse files Browse the repository at this point in the history
…ult` rule (#799)
  • Loading branch information
Dimitri POSTOLOV committed Nov 28, 2021
1 parent 36d5334 commit 32ec2cb
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 21 deletions.
5 changes: 5 additions & 0 deletions .changeset/rotten-eggs-rest.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@graphql-eslint/eslint-plugin': patch
---

fix: ignore arguments in `require-field-of-type-query-in-mutation-result` rule
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Kind, FieldDefinitionNode, isObjectType } from 'graphql';
import { Kind, isObjectType, NameNode } from 'graphql';
import { requireGraphQLSchemaFromContext, getTypeName, getLocation } from '../utils';
import { GraphQLESLintRule } from '../types';
import { GraphQLESTreeNode } from '../estree-parser';
Expand Down Expand Up @@ -56,14 +56,12 @@ const rule: GraphQLESLintRule = {
}
const selector = [
`:matches(${Kind.OBJECT_TYPE_DEFINITION}, ${Kind.OBJECT_TYPE_EXTENSION})[name.value=${mutationType.name}]`,
'>',
Kind.FIELD_DEFINITION,
Kind.NAMED_TYPE,
`> ${Kind.FIELD_DEFINITION} > .gqlType ${Kind.NAME}`,
].join(' ');

return {
[selector](node: GraphQLESTreeNode<FieldDefinitionNode>) {
const typeName = node.name.value;
[selector](node: GraphQLESTreeNode<NameNode>) {
const typeName = node.value;
const graphQLType = schema.getType(typeName);

if (isObjectType(graphQLType)) {
Expand All @@ -72,7 +70,7 @@ const rule: GraphQLESLintRule = {
if (!hasQueryType) {
context.report({
loc: getLocation(node.loc, typeName),
message: `Mutation result type "${graphQLType.name}" must contain field of type "${queryType.name}".`,
message: `Mutation result type "${graphQLType.name}" must contain field of type "${queryType.name}"`,
});
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ exports[` 1`] = `
1 |
2 | type Query
3 | type Mutation {
> 4 | createUser: User!
| ^^^^ Mutation result type "User" must contain field of type "Query".
> 4 | createUser(a: User, b: User!, c: [User], d: [User]!, e: [User!]!): User
| ^^^^ Mutation result type "User" must contain field of type "Query"
5 | }
6 |
`;
Expand All @@ -17,7 +17,7 @@ exports[` 2`] = `
4 |
5 | extend type Mutation {
> 6 | createUser: User!
| ^^^^ Mutation result type "User" must contain field of type "Query".
| ^^^^ Mutation result type "User" must contain field of type "Query"
7 | }
8 |
`;
Expand All @@ -26,8 +26,8 @@ exports[` 3`] = `
1 |
2 | type RootQuery
3 | type RootMutation {
> 4 | createUser: User!
| ^^^^ Mutation result type "User" must contain field of type "RootQuery".
> 4 | createUser: [User]
| ^^^^ Mutation result type "User" must contain field of type "RootQuery"
5 | }
6 |
7 | schema {
Expand All @@ -42,8 +42,8 @@ exports[` 4`] = `
2 | type RootQuery
3 | type RootMutation
4 | extend type RootMutation {
> 5 | createUser: User!
| ^^^^ Mutation result type "User" must contain field of type "RootQuery".
> 5 | createUser: [User!]!
| ^^^^ Mutation result type "User" must contain field of type "RootQuery"
6 | }
7 |
8 | schema {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,14 @@ ruleTester.runGraphQLTests('require-field-of-type-query-in-mutation-result', rul
],
invalid: [
{
name: 'should ignore arguments',
...useSchema(/* GraphQL */ `
type Query
type Mutation {
createUser: User!
createUser(a: User, b: User!, c: [User], d: [User]!, e: [User!]!): User
}
`),
errors: [{ message: 'Mutation result type "User" must contain field of type "Query".' }],
errors: [{ message: 'Mutation result type "User" must contain field of type "Query"' }],
},
{
...useSchema(/* GraphQL */ `
Expand All @@ -78,36 +79,36 @@ ruleTester.runGraphQLTests('require-field-of-type-query-in-mutation-result', rul
createUser: User!
}
`),
errors: [{ message: 'Mutation result type "User" must contain field of type "Query".' }],
errors: [{ message: 'Mutation result type "User" must contain field of type "Query"' }],
},
{
...useSchema(/* GraphQL */ `
type RootQuery
type RootMutation {
createUser: User!
createUser: [User]
}
schema {
mutation: RootMutation
query: RootQuery
}
`),
errors: [{ message: 'Mutation result type "User" must contain field of type "RootQuery".' }],
errors: [{ message: 'Mutation result type "User" must contain field of type "RootQuery"' }],
},
{
...useSchema(/* GraphQL */ `
type RootQuery
type RootMutation
extend type RootMutation {
createUser: User!
createUser: [User!]!
}
schema {
mutation: RootMutation
query: RootQuery
}
`),
errors: [{ message: 'Mutation result type "User" must contain field of type "RootQuery".' }],
errors: [{ message: 'Mutation result type "User" must contain field of type "RootQuery"' }],
},
],
});

0 comments on commit 32ec2cb

Please sign in to comment.