Skip to content

Commit

Permalink
fix: custom scalar original error propagation (#6306)
Browse files Browse the repository at this point in the history
* fix: custom scalar original error propagation (backport graphql/graphql-js#3837; graphql/graphql-js@076972e)

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

Co-authored-by: Arda TANRIKULU <[email protected]>

* okok

---------

Co-authored-by: Arda TANRIKULU <[email protected]>
  • Loading branch information
n1ru4l and ardatan committed Jul 1, 2024
1 parent a525ad3 commit 74f995f
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 1 deletion.
7 changes: 7 additions & 0 deletions .changeset/thirty-tigers-jump.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
'@graphql-tools/executor': patch
---

Properly propagate the original error in custom scalars.

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.
60 changes: 60 additions & 0 deletions packages/executor/src/execution/__tests__/variables-test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// eslint-disable-next-line import/no-extraneous-dependencies
import { inspect } from 'cross-inspect';
import {
GraphQLArgumentConfig,
Expand All @@ -12,7 +13,9 @@ import {
GraphQLString,
Kind,
parse,
versionInfo,
} from 'graphql';
import { createGraphQLError } from '@graphql-tools/utils';
import { expectJSON } from '../../__testUtils__/expectJSON.js';
import { executeSync } from '../execute.js';
import { getVariableValues } from '../values.js';
Expand All @@ -29,13 +32,30 @@ const TestComplexScalar = new GraphQLScalarType({
},
});

const TestFaultyScalarGraphQLError = createGraphQLError('FaultyScalarErrorMessage', {
extensions: {
code: 'FaultyScalarErrorMessageExtensionCode',
},
});

const TestFaultyScalar = new GraphQLScalarType({
name: 'FaultyScalar',
parseValue() {
throw TestFaultyScalarGraphQLError;
},
parseLiteral() {
throw TestFaultyScalarGraphQLError;
},
});

const TestInputObject = new GraphQLInputObjectType({
name: 'TestInputObject',
fields: {
a: { type: GraphQLString },
b: { type: new GraphQLList(GraphQLString) },
c: { type: new GraphQLNonNull(GraphQLString) },
d: { type: TestComplexScalar },
e: { type: TestFaultyScalar },
},
});

Expand Down Expand Up @@ -211,6 +231,30 @@ 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:
versionInfo.major === 17
? 'Argument "input" has invalid value { c: "foo", e: "bar" }.'
: 'Argument "input" has invalid value {c: "foo", e: "bar"}.',
path: ['fieldWithObjectInput'],
locations: [{ line: 3, column: 39 }],
},
],
});
});

describe('using variables', () => {
const doc = `
query ($input: TestInputObject) {
Expand Down Expand Up @@ -350,6 +394,22 @@ describe('Execute: Handles inputs', () => {
});
});

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

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 packages/executor/src/execution/values.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ function coerceVariableValues(
onError(
createGraphQLError(prefix + '; ' + error.message, {
nodes: varDefNode,
originalError: error.originalError,
originalError: error,
}),
);
});
Expand Down

0 comments on commit 74f995f

Please sign in to comment.