Skip to content

Commit 7dacfe5

Browse files
author
Dimitri POSTOLOV
authored
fix error report for avoid-scalar-result-type-on-mutation rule (#737)
1 parent f712780 commit 7dacfe5

File tree

4 files changed

+36
-27
lines changed

4 files changed

+36
-27
lines changed

.changeset/nine-tigers-listen.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@graphql-eslint/eslint-plugin': patch
3+
---
4+
5+
fix error report for `avoid-scalar-result-type-on-mutation` rule

packages/plugin/src/rules/avoid-scalar-result-type-on-mutation.ts

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Kind, FieldDefinitionNode, isScalarType } from 'graphql';
2-
import { requireGraphQLSchemaFromContext, getTypeName } from '../utils';
2+
import { getLocation, requireGraphQLSchemaFromContext } from '../utils';
33
import { GraphQLESLintRule } from '../types';
44
import { GraphQLESTreeNode } from '../estree-parser';
55

@@ -38,17 +38,21 @@ const rule: GraphQLESLintRule = {
3838
if (!mutationType) {
3939
return {};
4040
}
41-
const selector = `:matches(${Kind.OBJECT_TYPE_DEFINITION}, ${Kind.OBJECT_TYPE_EXTENSION})[name.value=${mutationType.name}] > ${Kind.FIELD_DEFINITION}`;
41+
const selector = [
42+
`:matches(${Kind.OBJECT_TYPE_DEFINITION}, ${Kind.OBJECT_TYPE_EXTENSION})[name.value=${mutationType.name}]`,
43+
'>',
44+
Kind.FIELD_DEFINITION,
45+
Kind.NAMED_TYPE,
46+
].join(' ');
4247

4348
return {
4449
[selector](node: GraphQLESTreeNode<FieldDefinitionNode>) {
45-
const rawNode = node.rawNode();
46-
const typeName = getTypeName(rawNode);
50+
const typeName = node.name.value;
4751
const graphQLType = schema.getType(typeName);
4852
if (isScalarType(graphQLType)) {
4953
context.report({
50-
node,
51-
message: `Unexpected scalar result type "${typeName}".`,
54+
loc: getLocation(node.loc, typeName),
55+
message: `Unexpected scalar result type "${typeName}"`,
5256
});
5357
}
5458
},

packages/plugin/tests/__snapshots__/avoid-scalar-result-type-on-mutation.spec.ts.snap

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ exports[` 1`] = `
44
1 |
55
2 | type Mutation {
66
> 3 | createUser: Boolean
7-
| ^^^^^^^^^^^^ Unexpected scalar result type "Boolean".
7+
| ^^^^^^^ Unexpected scalar result type "Boolean"
88
4 | }
99
5 |
1010
`;
@@ -14,17 +14,17 @@ exports[` 2`] = `
1414
2 | type Mutation
1515
3 |
1616
4 | extend type Mutation {
17-
> 5 | createUser: Boolean
18-
| ^^^^^^^^^^^^ Unexpected scalar result type "Boolean".
17+
> 5 | createUser: Boolean!
18+
| ^^^^^^^ Unexpected scalar result type "Boolean"
1919
6 | }
2020
7 |
2121
`;
2222

2323
exports[` 3`] = `
2424
1 |
2525
2 | type RootMutation {
26-
> 3 | createUser: Boolean!
27-
| ^^^^^^^^^^^^^^^^^^^ Unexpected scalar result type "Boolean".
26+
> 3 | createUser: [Boolean]
27+
| ^^^^^^^ Unexpected scalar result type "Boolean"
2828
4 | }
2929
5 |
3030
6 | schema {
@@ -37,8 +37,8 @@ exports[` 4`] = `
3737
1 |
3838
2 | type RootMutation
3939
3 | extend type RootMutation {
40-
> 4 | createUser: Boolean!
41-
| ^^^^^^^^^^^^^^^^^^^ Unexpected scalar result type "Boolean".
40+
> 4 | createUser: [Boolean]!
41+
| ^^^^^^^ Unexpected scalar result type "Boolean"
4242
5 | }
4343
6 |
4444
7 | schema {
@@ -52,8 +52,8 @@ exports[` 5`] = `
5252
2 | type Mutation {
5353
3 | createUser: User!
5454
> 4 | updateUser: Int
55-
| ^^^^^^^^^^^^ Unexpected scalar result type "Int".
56-
5 | deleteUser: Boolean
55+
| ^^^ Unexpected scalar result type "Int"
56+
5 | deleteUser: [Boolean!]!
5757
6 | }
5858
7 |
5959
`;
@@ -63,8 +63,8 @@ exports[` 6`] = `
6363
2 | type Mutation {
6464
3 | createUser: User!
6565
4 | updateUser: Int
66-
> 5 | deleteUser: Boolean
67-
| ^^^^^^^^^^^^ Unexpected scalar result type "Boolean".
66+
> 5 | deleteUser: [Boolean!]!
67+
| ^^^^^^^ Unexpected scalar result type "Boolean"
6868
6 | }
6969
7 |
7070
`;

packages/plugin/tests/avoid-scalar-result-type-on-mutation.spec.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -45,54 +45,54 @@ ruleTester.runGraphQLTests('avoid-scalar-result-type-on-mutation', rule, {
4545
createUser: Boolean
4646
}
4747
`),
48-
errors: [{ message: 'Unexpected scalar result type "Boolean".' }],
48+
errors: [{ message: 'Unexpected scalar result type "Boolean"' }],
4949
},
5050
{
5151
...useSchema(/* GraphQL */ `
5252
type Mutation
5353
5454
extend type Mutation {
55-
createUser: Boolean
55+
createUser: Boolean!
5656
}
5757
`),
58-
errors: [{ message: 'Unexpected scalar result type "Boolean".' }],
58+
errors: [{ message: 'Unexpected scalar result type "Boolean"' }],
5959
},
6060
{
6161
...useSchema(/* GraphQL */ `
6262
type RootMutation {
63-
createUser: Boolean!
63+
createUser: [Boolean]
6464
}
6565
6666
schema {
6767
mutation: RootMutation
6868
}
6969
`),
70-
errors: [{ message: 'Unexpected scalar result type "Boolean".' }],
70+
errors: [{ message: 'Unexpected scalar result type "Boolean"' }],
7171
},
7272
{
7373
...useSchema(/* GraphQL */ `
7474
type RootMutation
7575
extend type RootMutation {
76-
createUser: Boolean!
76+
createUser: [Boolean]!
7777
}
7878
7979
schema {
8080
mutation: RootMutation
8181
}
8282
`),
83-
errors: [{ message: 'Unexpected scalar result type "Boolean".' }],
83+
errors: [{ message: 'Unexpected scalar result type "Boolean"' }],
8484
},
8585
{
8686
...useSchema(/* GraphQL */ `
8787
type Mutation {
8888
createUser: User!
8989
updateUser: Int
90-
deleteUser: Boolean
90+
deleteUser: [Boolean!]!
9191
}
9292
`),
9393
errors: [
94-
{ message: 'Unexpected scalar result type "Int".' },
95-
{ message: 'Unexpected scalar result type "Boolean".' },
94+
{ message: 'Unexpected scalar result type "Int"' },
95+
{ message: 'Unexpected scalar result type "Boolean"' },
9696
],
9797
},
9898
],

0 commit comments

Comments
 (0)