From 076972e9c1944c9fe43a42046ed9d8be08d974dc Mon Sep 17 00:00:00 2001 From: Sten Reijers <37755871+stenreijers@users.noreply.github.com> Date: Wed, 15 Feb 2023 10:19:03 +0100 Subject: [PATCH] Fix/invalid error propagation custom scalars (backport for 16.x.x) (#3838) --- src/execution/__tests__/variables-test.ts | 59 +++++++++++++++++++++++ src/execution/values.ts | 2 +- 2 files changed, 60 insertions(+), 1 deletion(-) diff --git a/src/execution/__tests__/variables-test.ts b/src/execution/__tests__/variables-test.ts index 786a4810c2..3a859a0bdc 100644 --- a/src/execution/__tests__/variables-test.ts +++ b/src/execution/__tests__/variables-test.ts @@ -6,6 +6,8 @@ import { expectJSON } from '../../__testUtils__/expectJSON'; import { inspect } from '../../jsutils/inspect'; import { invariant } from '../../jsutils/invariant'; +import { GraphQLError } from '../../error/GraphQLError'; + import { Kind } from '../../language/kinds'; import { parse } from '../../language/parser'; @@ -27,6 +29,25 @@ import { GraphQLSchema } from '../../type/schema'; import { executeSync } from '../execute'; import { getVariableValues } from '../values'; +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) { @@ -46,6 +67,7 @@ const TestInputObject = new GraphQLInputObjectType({ b: { type: new GraphQLList(GraphQLString) }, c: { type: new GraphQLNonNull(GraphQLString) }, d: { type: TestComplexScalar }, + e: { type: TestFaultyScalar }, }, }); @@ -228,6 +250,27 @@ 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: 39 }], + }, + ], + }); + }); + describe('using variables', () => { const doc = ` query ($input: TestInputObject) { @@ -367,6 +410,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); diff --git a/src/execution/values.ts b/src/execution/values.ts index 023e028109..d65ea9cf20 100644 --- a/src/execution/values.ts +++ b/src/execution/values.ts @@ -131,7 +131,7 @@ function coerceVariableValues( onError( new GraphQLError(prefix + '; ' + error.message, { nodes: varDefNode, - originalError: error.originalError, + originalError: error, }), ); },