Skip to content

Commit 5a76837

Browse files
committed
execution: Preserve extensions in parseValue errors
Previously, the only fields observed on an error thrown by (for example) parseValue were `message` and `originalError`. Now, the error itself is used as the `originalError`; this may be mildly backwards-incompatible if you added extensions on the error itself which you for some reason wanted to disappear, but that seems unlikely. Addresses an issue raised in apollographql/apollo-server#7178
1 parent 6b5c8af commit 5a76837

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

src/execution/__tests__/variables-test.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import { expectJSON } from '../../__testUtils__/expectJSON.js';
55

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

8+
import { GraphQLError } from '../../error/GraphQLError.js';
9+
810
import { Kind } from '../../language/kinds.js';
911
import { parse } from '../../language/parser.js';
1012

@@ -29,6 +31,11 @@ import { getVariableValues } from '../values.js';
2931
const TestComplexScalar = new GraphQLScalarType({
3032
name: 'ComplexScalar',
3133
parseValue(value) {
34+
if (value === 'ThrowAnError') {
35+
throw new GraphQLError('parseValue throws', {
36+
extensions: { custom: 'extension' },
37+
});
38+
}
3239
expect(value).to.equal('SerializedValue');
3340
return 'DeserializedValue';
3441
},
@@ -366,6 +373,22 @@ describe('Execute: Handles inputs', () => {
366373
});
367374
});
368375

376+
it('propagates GraphQLError from parseValue', () => {
377+
const params = { input: { c: 'foo', d: 'ThrowAnError' } };
378+
const result = executeQuery(doc, params);
379+
380+
expectJSON(result).toDeepEqual({
381+
errors: [
382+
{
383+
message:
384+
'Variable "$input" got invalid value "ThrowAnError" at "input.d"; parseValue throws',
385+
locations: [{ column: 16, line: 2 }],
386+
extensions: { custom: 'extension' },
387+
},
388+
],
389+
});
390+
});
391+
369392
it('errors on null for nested non-null', () => {
370393
const params = { input: { a: 'foo', b: 'bar', c: null } };
371394
const result = executeQuery(doc, params);

src/execution/values.ts

Lines changed: 1 addition & 1 deletion
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)