diff --git a/src/execution/__tests__/variables-test.ts b/src/execution/__tests__/variables-test.ts index 78d206dff9..2bd0ec7d99 100644 --- a/src/execution/__tests__/variables-test.ts +++ b/src/execution/__tests__/variables-test.ts @@ -1412,11 +1412,16 @@ describe('Execute: Handles inputs', () => { } `); - expect(result).to.have.property('errors'); - expect(result.errors).to.have.length(1); - expect(result.errors?.at(0)?.message).to.match( - /Argument "value" of required type "String!"/, - ); + expectJSON(result).toDeepEqual({ + data: null, + errors: [ + { + message: + 'Variable "$value" defined by fragment "a" of required type "String!" was not provided.', + locations: [{ line: 3, column: 11 }], + }, + ], + }); }); it('when the definition has a default and is provided', () => { @@ -1480,11 +1485,16 @@ describe('Execute: Handles inputs', () => { } `); - expect(result).to.have.property('errors'); - expect(result.errors).to.have.length(1); - expect(result.errors?.at(0)?.message).to.match( - /Argument "value" has invalid value: Expected value of non-null type "String!" not to be null./, - ); + expectJSON(result).toDeepEqual({ + data: null, + errors: [ + { + message: + 'Variable "$value" defined by fragment "a" has invalid value: Expected value of non-null type "String!" not to be null.', + locations: [{ line: 3, column: 23 }], + }, + ], + }); }); it('when the definition has no default and is not provided', () => { diff --git a/src/execution/values.ts b/src/execution/values.ts index 0de08bc9db..ce5fd6f78d 100644 --- a/src/execution/values.ts +++ b/src/execution/values.ts @@ -244,7 +244,7 @@ function coerceArgument( // continue with an invalid argument value. throw new GraphQLError( // TODO: clean up the naming of isRequiredArgument(), isArgument(), and argDef if/when experimental fragment variables are merged - `Argument "${isArgument(argDef) ? argDef : argName}" of required type "${argType}" was not provided.`, + `${printArgumentOrFragmentVariable(argDef, node)} of required type "${argType}" was not provided.`, { nodes: node }, ); } @@ -291,7 +291,7 @@ function coerceArgument( argType, (error, path) => { // TODO: clean up the naming of isRequiredArgument(), isArgument(), and argDef if/when experimental fragment variables are merged - error.message = `Argument "${isArgument(argDef) ? argDef : argDef.name}" has invalid value${printPathArray( + error.message = `${printArgumentOrFragmentVariable(argDef, node)} has invalid value${printPathArray( path, )}: ${error.message}`; throw error; @@ -306,6 +306,16 @@ function coerceArgument( coercedValues[argName] = coercedValue; } +// TODO: clean up the naming of isRequiredArgument(), isArgument(), and argDef if/when experimental fragment variables are merged +function printArgumentOrFragmentVariable( + argDef: GraphQLArgument | GraphQLVariableSignature, + node: FieldNode | DirectiveNode | FragmentSpreadNode, +): string { + return isArgument(argDef) + ? `Argument "${argDef}"` + : `Variable "$${argDef.name}" defined by fragment "${node.name.value}"`; +} + /** * Prepares an object map of argument values given a directive definition * and a AST node which may contain directives. Optionally also accepts a map