From 5b535a860a4c4c8dcd638afdcc54a4d283efce62 Mon Sep 17 00:00:00 2001 From: Sten Reijers Date: Thu, 2 Feb 2023 15:22:20 +0100 Subject: [PATCH 1/2] fix: invalid originalError propagation in custom scalars (backport for 16.x.x) --- package-lock.json | 20 ++++---- src/execution/__tests__/variables-test.ts | 59 +++++++++++++++++++++++ src/execution/values.ts | 2 +- 3 files changed, 69 insertions(+), 12 deletions(-) diff --git a/package-lock.json b/package-lock.json index 1a7bfea8b3..43af8b4bac 100644 --- a/package-lock.json +++ b/package-lock.json @@ -7712,8 +7712,13 @@ "dev": true }, "node_modules/eslint-plugin-internal-rules": { - "resolved": "resources/eslint-internal-rules", - "link": true + "name": "eslint-plugin-graphql-internal", + "version": "0.0.0", + "resolved": "file:resources/eslint-internal-rules", + "dev": true, + "engines": { + "node": ">= 14.0.0" + } }, "node_modules/eslint-plugin-node": { "version": "11.1.0", @@ -17111,14 +17116,6 @@ "type": "github", "url": "https://github.com/sponsors/wooorm" } - }, - "resources/eslint-internal-rules": { - "name": "eslint-plugin-graphql-internal", - "version": "0.0.0", - "dev": true, - "engines": { - "node": ">= 14.0.0" - } } }, "dependencies": { @@ -23045,7 +23042,8 @@ } }, "eslint-plugin-internal-rules": { - "version": "file:resources/eslint-internal-rules" + "version": "npm:eslint-plugin-graphql-internal@0.0.0", + "dev": true }, "eslint-plugin-node": { "version": "11.1.0", 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, }), ); }, From 03299f5d98929b68d17801e01adf6196d5d42804 Mon Sep 17 00:00:00 2001 From: Sten Reijers Date: Thu, 2 Feb 2023 15:27:33 +0100 Subject: [PATCH 2/2] Fix: use old package-lock.json because of older NPM version at github --- package-lock.json | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/package-lock.json b/package-lock.json index 43af8b4bac..1a7bfea8b3 100644 --- a/package-lock.json +++ b/package-lock.json @@ -7712,13 +7712,8 @@ "dev": true }, "node_modules/eslint-plugin-internal-rules": { - "name": "eslint-plugin-graphql-internal", - "version": "0.0.0", - "resolved": "file:resources/eslint-internal-rules", - "dev": true, - "engines": { - "node": ">= 14.0.0" - } + "resolved": "resources/eslint-internal-rules", + "link": true }, "node_modules/eslint-plugin-node": { "version": "11.1.0", @@ -17116,6 +17111,14 @@ "type": "github", "url": "https://github.com/sponsors/wooorm" } + }, + "resources/eslint-internal-rules": { + "name": "eslint-plugin-graphql-internal", + "version": "0.0.0", + "dev": true, + "engines": { + "node": ">= 14.0.0" + } } }, "dependencies": { @@ -23042,8 +23045,7 @@ } }, "eslint-plugin-internal-rules": { - "version": "npm:eslint-plugin-graphql-internal@0.0.0", - "dev": true + "version": "file:resources/eslint-internal-rules" }, "eslint-plugin-node": { "version": "11.1.0",