Skip to content

Commit

Permalink
fix: fixed invalid originalError propagation on parseValue() errors i…
Browse files Browse the repository at this point in the history
…n custom scalars
  • Loading branch information
stenreijers committed Jan 29, 2023
1 parent 8be83d8 commit bdd24b7
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 1 deletion.
61 changes: 61 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: 'FaultyScalarErrorMessageExtensionCode'
}
}
);

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,29 @@ 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 +411,22 @@ describe('Execute: Handles inputs', () => {
});
});

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

expect(result.errors?.at(0)?.originalError).to.equal(TestFaultyScalarGraphQLError);
expectJSON(result).toDeepEqual({
errors: [
{
message: 'Variable "$input" got invalid value "SerializedValue" at "input.e"; FaultyScalarErrorMessage',
locations: [{ line: 2, column: 16 }],
extensions: { code: 'FaultyScalarErrorMessageExtensionCode' }
}
]
});
});

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 bdd24b7

Please sign in to comment.