Skip to content

Commit 9824b17

Browse files
committed
fix: custom scalar original error propagation (backport graphql/graphql-js#3837; graphql/graphql-js@076972e)
1 parent a525ad3 commit 9824b17

File tree

3 files changed

+64
-1
lines changed

3 files changed

+64
-1
lines changed

.changeset/thirty-tigers-jump.md

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
'@graphql-tools/executor': patch
3+
---
4+
5+
Properly propagate the original error in custom scalars.
6+
7+
Errors thrown in the `parseValue` function for custom scalars were not propagated correctly using the `originalError` property of the `GraphQLError` on invalid input. As a result, error codes from the `extensions.code` were not propagated correctly.

packages/executor/src/execution/__tests__/variables-test.ts

+56
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1+
// eslint-disable-next-line import/no-extraneous-dependencies
12
import { inspect } from 'cross-inspect';
23
import {
34
GraphQLArgumentConfig,
45
GraphQLEnumType,
6+
GraphQLError,
57
GraphQLFieldConfig,
68
GraphQLInputObjectType,
79
GraphQLList,
@@ -29,13 +31,30 @@ const TestComplexScalar = new GraphQLScalarType({
2931
},
3032
});
3133

34+
const TestFaultyScalarGraphQLError = new GraphQLError('FaultyScalarErrorMessage', {
35+
extensions: {
36+
code: 'FaultyScalarErrorMessageExtensionCode',
37+
},
38+
});
39+
40+
const TestFaultyScalar = new GraphQLScalarType({
41+
name: 'FaultyScalar',
42+
parseValue() {
43+
throw TestFaultyScalarGraphQLError;
44+
},
45+
parseLiteral() {
46+
throw TestFaultyScalarGraphQLError;
47+
},
48+
});
49+
3250
const TestInputObject = new GraphQLInputObjectType({
3351
name: 'TestInputObject',
3452
fields: {
3553
a: { type: GraphQLString },
3654
b: { type: new GraphQLList(GraphQLString) },
3755
c: { type: new GraphQLNonNull(GraphQLString) },
3856
d: { type: TestComplexScalar },
57+
e: { type: TestFaultyScalar },
3958
},
4059
});
4160

@@ -211,6 +230,27 @@ describe('Execute: Handles inputs', () => {
211230
});
212231
});
213232

233+
it('errors on faulty scalar type input', () => {
234+
const result = executeQuery(`
235+
{
236+
fieldWithObjectInput(input: {c: "foo", e: "bar"})
237+
}
238+
`);
239+
240+
expectJSON(result).toDeepEqual({
241+
data: {
242+
fieldWithObjectInput: null,
243+
},
244+
errors: [
245+
{
246+
message: 'Argument "input" has invalid value {c: "foo", e: "bar"}.',
247+
path: ['fieldWithObjectInput'],
248+
locations: [{ line: 3, column: 39 }],
249+
},
250+
],
251+
});
252+
});
253+
214254
describe('using variables', () => {
215255
const doc = `
216256
query ($input: TestInputObject) {
@@ -350,6 +390,22 @@ describe('Execute: Handles inputs', () => {
350390
});
351391
});
352392

393+
it('errors on faulty scalar type input', () => {
394+
const params = { input: { c: 'foo', e: 'SerializedValue' } };
395+
const result = executeQuery(doc, params);
396+
397+
expectJSON(result).toDeepEqual({
398+
errors: [
399+
{
400+
message:
401+
'Variable "$input" got invalid value "SerializedValue" at "input.e"; FaultyScalarErrorMessage',
402+
locations: [{ line: 2, column: 16 }],
403+
extensions: { code: 'FaultyScalarErrorMessageExtensionCode' },
404+
},
405+
],
406+
});
407+
});
408+
353409
it('errors on null for nested non-null', () => {
354410
const params = { input: { a: 'foo', b: 'bar', c: null } };
355411
const result = executeQuery(doc, params);

packages/executor/src/execution/values.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ function coerceVariableValues(
116116
onError(
117117
createGraphQLError(prefix + '; ' + error.message, {
118118
nodes: varDefNode,
119-
originalError: error.originalError,
119+
originalError: error,
120120
}),
121121
);
122122
});

0 commit comments

Comments
 (0)