Skip to content

Commit

Permalink
fix error report for avoid-scalar-result-type-on-mutation rule (#737)
Browse files Browse the repository at this point in the history
  • Loading branch information
Dimitri POSTOLOV committed Oct 31, 2021
1 parent f712780 commit 7dacfe5
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 27 deletions.
5 changes: 5 additions & 0 deletions .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
16 changes: 10 additions & 6 deletions 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';

Expand Down Expand Up @@ -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<FieldDefinitionNode>) {
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}"`,
});
}
},
Expand Down
Expand Up @@ -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 |
`;
Expand All @@ -14,17 +14,17 @@ 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 |
`;

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 {
Expand All @@ -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 {
Expand All @@ -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 |
`;
Expand All @@ -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 |
`;
20 changes: 10 additions & 10 deletions packages/plugin/tests/avoid-scalar-result-type-on-mutation.spec.ts
Expand Up @@ -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"' },
],
},
],
Expand Down

0 comments on commit 7dacfe5

Please sign in to comment.