Skip to content

Commit

Permalink
execution: Preserve extensions in parseValue errors
Browse files Browse the repository at this point in the history
Previously, the only fields observed on an error thrown by (for example)
parseValue were `message` and `originalError`. Now, if an error has no
`originalError`, it is itself used as the original error.

Addresses an issue raised in
apollographql/apollo-server#7178
  • Loading branch information
glasser committed Nov 22, 2022
1 parent 6b5c8af commit d1f34a1
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
23 changes: 23 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 @@ -29,6 +31,11 @@ import { getVariableValues } from '../values.js';
const TestComplexScalar = new GraphQLScalarType({
name: 'ComplexScalar',
parseValue(value) {
if (value === 'ThrowAnError') {
throw new GraphQLError('parseValue throws', {
extensions: { custom: 'extension' },
});
}
expect(value).to.equal('SerializedValue');
return 'DeserializedValue';
},
Expand Down Expand Up @@ -366,6 +373,22 @@ describe('Execute: Handles inputs', () => {
});
});

it('propagates GraphQLError from parseValue', () => {
const params = { input: { c: 'foo', d: 'ThrowAnError' } };
const result = executeQuery(doc, params);

expectJSON(result).toDeepEqual({
errors: [
{
message:
'Variable "$input" got invalid value "ThrowAnError" at "input.d"; parseValue throws',
locations: [{ column: 16, line: 2 }],
extensions: { custom: 'extension' },
},
],
});
});

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.originalError ?? error,
}),
);
},
Expand Down

0 comments on commit d1f34a1

Please sign in to comment.