Skip to content

Commit

Permalink
fix: invalid originalError propagation in custom scalars (#3837)
Browse files Browse the repository at this point in the history
  • Loading branch information
stenreijers committed Jan 31, 2023
1 parent 8be83d8 commit 76e47fc
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 1 deletion.
60 changes: 60 additions & 0 deletions src/execution/__tests__/variables-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import { expectJSON } from '../../__testUtils__/expectJSON.js';

import { inspect } from '../../jsutils/inspect.js';

import { GraphQLError } from '../../error/GraphQLError.js';

import { Kind } from '../../language/kinds.js';
import { parse } from '../../language/parser.js';

Expand All @@ -26,6 +28,25 @@ import { GraphQLSchema } from '../../type/schema.js';
import { executeSync } from '../execute.js';
import { getVariableValues } from '../values.js';

const TestFaultyScalarGraphQLError = new GraphQLError(
'FaultyScalarErrorMessage',
{
extensions: {
code: 'FaultyScalarErrorExtensionCode',
},
},
);

const TestFaultyScalar = new GraphQLScalarType({
name: 'FaultyScalar',
parseValue() {
throw TestFaultyScalarGraphQLError;
},
parseLiteral() {
throw TestFaultyScalarGraphQLError;
},
});

const TestComplexScalar = new GraphQLScalarType({
name: 'ComplexScalar',
parseValue(value) {
Expand All @@ -45,6 +66,7 @@ const TestInputObject = new GraphQLInputObjectType({
b: { type: new GraphQLList(GraphQLString) },
c: { type: new GraphQLNonNull(GraphQLString) },
d: { type: TestComplexScalar },
e: { type: TestFaultyScalar },
},
});

Expand Down Expand Up @@ -225,6 +247,28 @@ describe('Execute: Handles inputs', () => {
},
});
});

it('errors on faulty scalar type input', () => {
const result = executeQuery(`
{
fieldWithObjectInput(input: {c: "foo", e: "bar"})
}
`);

expectJSON(result).toDeepEqual({
data: {
fieldWithObjectInput: null,
},
errors: [
{
message:
'Argument "input" has invalid value { c: "foo", e: "bar" }.',
path: ['fieldWithObjectInput'],
locations: [{ line: 3, column: 41 }],
},
],
});
});
});

describe('using variables', () => {
Expand Down Expand Up @@ -366,6 +410,22 @@ describe('Execute: Handles inputs', () => {
});
});

it('errors on faulty scalar type input', () => {
const params = { input: { c: 'foo', e: 'SerializedValue' } };
const result = executeQuery(doc, params);

expectJSON(result).toDeepEqual({
errors: [
{
message:
'Variable "$input" got invalid value "SerializedValue" at "input.e"; FaultyScalarErrorMessage',
locations: [{ line: 2, column: 16 }],
extensions: { code: 'FaultyScalarErrorExtensionCode' },
},
],
});
});

it('errors on null for nested non-null', () => {
const params = { input: { a: 'foo', b: 'bar', c: null } };
const result = executeQuery(doc, params);
Expand Down
2 changes: 1 addition & 1 deletion src/execution/values.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ function coerceVariableValues(
onError(
new GraphQLError(prefix + '; ' + error.message, {
nodes: varDefNode,
originalError: error.originalError,
originalError: error,
}),
);
},
Expand Down

0 comments on commit 76e47fc

Please sign in to comment.