From 88623bfa8d109b7404e9984d0b266d2a9073552a Mon Sep 17 00:00:00 2001 From: dimitri Date: Tue, 23 Nov 2021 21:44:29 +0100 Subject: [PATCH] fix: ignore arguments in `require-field-of-type-query-in-mutation-result` rule --- .changeset/rotten-eggs-rest.md | 5 +++++ ...uire-field-of-type-query-in-mutation-result.ts | 12 +++++------- ...-of-type-query-in-mutation-result.spec.ts.snap | 14 +++++++------- ...field-of-type-query-in-mutation-result.spec.ts | 15 ++++++++------- 4 files changed, 25 insertions(+), 21 deletions(-) create mode 100644 .changeset/rotten-eggs-rest.md diff --git a/.changeset/rotten-eggs-rest.md b/.changeset/rotten-eggs-rest.md new file mode 100644 index 00000000000..a18200b2da6 --- /dev/null +++ b/.changeset/rotten-eggs-rest.md @@ -0,0 +1,5 @@ +--- +'@graphql-eslint/eslint-plugin': patch +--- + +fix: ignore arguments in `require-field-of-type-query-in-mutation-result` rule diff --git a/packages/plugin/src/rules/require-field-of-type-query-in-mutation-result.ts b/packages/plugin/src/rules/require-field-of-type-query-in-mutation-result.ts index d6dcdb69efa..f2e6baacbf3 100644 --- a/packages/plugin/src/rules/require-field-of-type-query-in-mutation-result.ts +++ b/packages/plugin/src/rules/require-field-of-type-query-in-mutation-result.ts @@ -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'; @@ -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) { - const typeName = node.name.value; + [selector](node: GraphQLESTreeNode) { + const typeName = node.value; const graphQLType = schema.getType(typeName); if (isObjectType(graphQLType)) { @@ -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}"`, }); } } diff --git a/packages/plugin/tests/__snapshots__/require-field-of-type-query-in-mutation-result.spec.ts.snap b/packages/plugin/tests/__snapshots__/require-field-of-type-query-in-mutation-result.spec.ts.snap index 2b39e79fe9b..1c384e4f578 100644 --- a/packages/plugin/tests/__snapshots__/require-field-of-type-query-in-mutation-result.spec.ts.snap +++ b/packages/plugin/tests/__snapshots__/require-field-of-type-query-in-mutation-result.spec.ts.snap @@ -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 | `; @@ -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 | `; @@ -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 { @@ -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 { diff --git a/packages/plugin/tests/require-field-of-type-query-in-mutation-result.spec.ts b/packages/plugin/tests/require-field-of-type-query-in-mutation-result.spec.ts index 9f7025bb22f..194d7016762 100644 --- a/packages/plugin/tests/require-field-of-type-query-in-mutation-result.spec.ts +++ b/packages/plugin/tests/require-field-of-type-query-in-mutation-result.spec.ts @@ -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 */ ` @@ -78,13 +79,13 @@ 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 { @@ -92,14 +93,14 @@ ruleTester.runGraphQLTests('require-field-of-type-query-in-mutation-result', rul 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 { @@ -107,7 +108,7 @@ ruleTester.runGraphQLTests('require-field-of-type-query-in-mutation-result', rul 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"' }], }, ], });