diff --git a/.changeset/nine-tigers-listen.md b/.changeset/nine-tigers-listen.md new file mode 100644 index 00000000000..ef774f0f761 --- /dev/null +++ b/.changeset/nine-tigers-listen.md @@ -0,0 +1,5 @@ +--- +'@graphql-eslint/eslint-plugin': patch +--- + +fix error report for `avoid-scalar-result-type-on-mutation` rule diff --git a/packages/plugin/src/rules/avoid-scalar-result-type-on-mutation.ts b/packages/plugin/src/rules/avoid-scalar-result-type-on-mutation.ts index 7f1be9b68ca..81941e14c92 100644 --- a/packages/plugin/src/rules/avoid-scalar-result-type-on-mutation.ts +++ b/packages/plugin/src/rules/avoid-scalar-result-type-on-mutation.ts @@ -1,5 +1,5 @@ import { Kind, FieldDefinitionNode, isScalarType } from 'graphql'; -import { requireGraphQLSchemaFromContext, getTypeName } from '../utils'; +import { getLocation, requireGraphQLSchemaFromContext } from '../utils'; import { GraphQLESLintRule } from '../types'; import { GraphQLESTreeNode } from '../estree-parser'; @@ -38,17 +38,21 @@ const rule: GraphQLESLintRule = { if (!mutationType) { return {}; } - const selector = `:matches(${Kind.OBJECT_TYPE_DEFINITION}, ${Kind.OBJECT_TYPE_EXTENSION})[name.value=${mutationType.name}] > ${Kind.FIELD_DEFINITION}`; + const selector = [ + `:matches(${Kind.OBJECT_TYPE_DEFINITION}, ${Kind.OBJECT_TYPE_EXTENSION})[name.value=${mutationType.name}]`, + '>', + Kind.FIELD_DEFINITION, + Kind.NAMED_TYPE, + ].join(' '); return { [selector](node: GraphQLESTreeNode) { - const rawNode = node.rawNode(); - const typeName = getTypeName(rawNode); + const typeName = node.name.value; const graphQLType = schema.getType(typeName); if (isScalarType(graphQLType)) { context.report({ - node, - message: `Unexpected scalar result type "${typeName}".`, + loc: getLocation(node.loc, typeName), + message: `Unexpected scalar result type "${typeName}"`, }); } }, diff --git a/packages/plugin/tests/__snapshots__/avoid-scalar-result-type-on-mutation.spec.ts.snap b/packages/plugin/tests/__snapshots__/avoid-scalar-result-type-on-mutation.spec.ts.snap index e7765ee1490..36369602706 100644 --- a/packages/plugin/tests/__snapshots__/avoid-scalar-result-type-on-mutation.spec.ts.snap +++ b/packages/plugin/tests/__snapshots__/avoid-scalar-result-type-on-mutation.spec.ts.snap @@ -4,7 +4,7 @@ exports[` 1`] = ` 1 | 2 | type Mutation { > 3 | createUser: Boolean - | ^^^^^^^^^^^^ Unexpected scalar result type "Boolean". + | ^^^^^^^ Unexpected scalar result type "Boolean" 4 | } 5 | `; @@ -14,8 +14,8 @@ exports[` 2`] = ` 2 | type Mutation 3 | 4 | extend type Mutation { -> 5 | createUser: Boolean - | ^^^^^^^^^^^^ Unexpected scalar result type "Boolean". +> 5 | createUser: Boolean! + | ^^^^^^^ Unexpected scalar result type "Boolean" 6 | } 7 | `; @@ -23,8 +23,8 @@ exports[` 2`] = ` exports[` 3`] = ` 1 | 2 | type RootMutation { -> 3 | createUser: Boolean! - | ^^^^^^^^^^^^^^^^^^^ Unexpected scalar result type "Boolean". +> 3 | createUser: [Boolean] + | ^^^^^^^ Unexpected scalar result type "Boolean" 4 | } 5 | 6 | schema { @@ -37,8 +37,8 @@ exports[` 4`] = ` 1 | 2 | type RootMutation 3 | extend type RootMutation { -> 4 | createUser: Boolean! - | ^^^^^^^^^^^^^^^^^^^ Unexpected scalar result type "Boolean". +> 4 | createUser: [Boolean]! + | ^^^^^^^ Unexpected scalar result type "Boolean" 5 | } 6 | 7 | schema { @@ -52,8 +52,8 @@ exports[` 5`] = ` 2 | type Mutation { 3 | createUser: User! > 4 | updateUser: Int - | ^^^^^^^^^^^^ Unexpected scalar result type "Int". - 5 | deleteUser: Boolean + | ^^^ Unexpected scalar result type "Int" + 5 | deleteUser: [Boolean!]! 6 | } 7 | `; @@ -63,8 +63,8 @@ exports[` 6`] = ` 2 | type Mutation { 3 | createUser: User! 4 | updateUser: Int -> 5 | deleteUser: Boolean - | ^^^^^^^^^^^^ Unexpected scalar result type "Boolean". +> 5 | deleteUser: [Boolean!]! + | ^^^^^^^ Unexpected scalar result type "Boolean" 6 | } 7 | `; diff --git a/packages/plugin/tests/avoid-scalar-result-type-on-mutation.spec.ts b/packages/plugin/tests/avoid-scalar-result-type-on-mutation.spec.ts index bd8a06b4d1a..479c92358ff 100644 --- a/packages/plugin/tests/avoid-scalar-result-type-on-mutation.spec.ts +++ b/packages/plugin/tests/avoid-scalar-result-type-on-mutation.spec.ts @@ -45,54 +45,54 @@ ruleTester.runGraphQLTests('avoid-scalar-result-type-on-mutation', rule, { createUser: Boolean } `), - errors: [{ message: 'Unexpected scalar result type "Boolean".' }], + errors: [{ message: 'Unexpected scalar result type "Boolean"' }], }, { ...useSchema(/* GraphQL */ ` type Mutation extend type Mutation { - createUser: Boolean + createUser: Boolean! } `), - errors: [{ message: 'Unexpected scalar result type "Boolean".' }], + errors: [{ message: 'Unexpected scalar result type "Boolean"' }], }, { ...useSchema(/* GraphQL */ ` type RootMutation { - createUser: Boolean! + createUser: [Boolean] } schema { mutation: RootMutation } `), - errors: [{ message: 'Unexpected scalar result type "Boolean".' }], + errors: [{ message: 'Unexpected scalar result type "Boolean"' }], }, { ...useSchema(/* GraphQL */ ` type RootMutation extend type RootMutation { - createUser: Boolean! + createUser: [Boolean]! } schema { mutation: RootMutation } `), - errors: [{ message: 'Unexpected scalar result type "Boolean".' }], + errors: [{ message: 'Unexpected scalar result type "Boolean"' }], }, { ...useSchema(/* GraphQL */ ` type Mutation { createUser: User! updateUser: Int - deleteUser: Boolean + deleteUser: [Boolean!]! } `), errors: [ - { message: 'Unexpected scalar result type "Int".' }, - { message: 'Unexpected scalar result type "Boolean".' }, + { message: 'Unexpected scalar result type "Int"' }, + { message: 'Unexpected scalar result type "Boolean"' }, ], }, ],