Skip to content

Commit 076972e

Browse files
authored
Fix/invalid error propagation custom scalars (backport for 16.x.x) (#3838)
1 parent 4a82557 commit 076972e

File tree

2 files changed

+60
-1
lines changed

2 files changed

+60
-1
lines changed

src/execution/__tests__/variables-test.ts

+59
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import { expectJSON } from '../../__testUtils__/expectJSON';
66
import { inspect } from '../../jsutils/inspect';
77
import { invariant } from '../../jsutils/invariant';
88

9+
import { GraphQLError } from '../../error/GraphQLError';
10+
911
import { Kind } from '../../language/kinds';
1012
import { parse } from '../../language/parser';
1113

@@ -27,6 +29,25 @@ import { GraphQLSchema } from '../../type/schema';
2729
import { executeSync } from '../execute';
2830
import { getVariableValues } from '../values';
2931

32+
const TestFaultyScalarGraphQLError = new GraphQLError(
33+
'FaultyScalarErrorMessage',
34+
{
35+
extensions: {
36+
code: 'FaultyScalarErrorMessageExtensionCode',
37+
},
38+
},
39+
);
40+
41+
const TestFaultyScalar = new GraphQLScalarType({
42+
name: 'FaultyScalar',
43+
parseValue() {
44+
throw TestFaultyScalarGraphQLError;
45+
},
46+
parseLiteral() {
47+
throw TestFaultyScalarGraphQLError;
48+
},
49+
});
50+
3051
const TestComplexScalar = new GraphQLScalarType({
3152
name: 'ComplexScalar',
3253
parseValue(value) {
@@ -46,6 +67,7 @@ const TestInputObject = new GraphQLInputObjectType({
4667
b: { type: new GraphQLList(GraphQLString) },
4768
c: { type: new GraphQLNonNull(GraphQLString) },
4869
d: { type: TestComplexScalar },
70+
e: { type: TestFaultyScalar },
4971
},
5072
});
5173

@@ -228,6 +250,27 @@ describe('Execute: Handles inputs', () => {
228250
});
229251
});
230252

253+
it('errors on faulty scalar type input', () => {
254+
const result = executeQuery(`
255+
{
256+
fieldWithObjectInput(input: {c: "foo", e: "bar"})
257+
}
258+
`);
259+
260+
expectJSON(result).toDeepEqual({
261+
data: {
262+
fieldWithObjectInput: null,
263+
},
264+
errors: [
265+
{
266+
message: 'Argument "input" has invalid value {c: "foo", e: "bar"}.',
267+
path: ['fieldWithObjectInput'],
268+
locations: [{ line: 3, column: 39 }],
269+
},
270+
],
271+
});
272+
});
273+
231274
describe('using variables', () => {
232275
const doc = `
233276
query ($input: TestInputObject) {
@@ -367,6 +410,22 @@ describe('Execute: Handles inputs', () => {
367410
});
368411
});
369412

413+
it('errors on faulty scalar type input', () => {
414+
const params = { input: { c: 'foo', e: 'SerializedValue' } };
415+
const result = executeQuery(doc, params);
416+
417+
expectJSON(result).toDeepEqual({
418+
errors: [
419+
{
420+
message:
421+
'Variable "$input" got invalid value "SerializedValue" at "input.e"; FaultyScalarErrorMessage',
422+
locations: [{ line: 2, column: 16 }],
423+
extensions: { code: 'FaultyScalarErrorMessageExtensionCode' },
424+
},
425+
],
426+
});
427+
});
428+
370429
it('errors on null for nested non-null', () => {
371430
const params = { input: { a: 'foo', b: 'bar', c: null } };
372431
const result = executeQuery(doc, params);

src/execution/values.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ function coerceVariableValues(
131131
onError(
132132
new GraphQLError(prefix + '; ' + error.message, {
133133
nodes: varDefNode,
134-
originalError: error.originalError,
134+
originalError: error,
135135
}),
136136
);
137137
},

0 commit comments

Comments
 (0)