11import { inspect } from '../jsutils/inspect.js' ;
22import { keyMap } from '../jsutils/keyMap.js' ;
33import type { Maybe } from '../jsutils/Maybe.js' ;
4- import type { ObjMap } from '../jsutils/ObjMap.js ' ;
4+ import type { ObjMap , ReadOnlyObjMap } from '../jsutils/ObjMap' ;
55import { printPathArray } from '../jsutils/printPathArray.js' ;
66
77import { GraphQLError } from '../error/GraphQLError.js' ;
@@ -14,7 +14,7 @@ import type {
1414import { Kind } from '../language/kinds.js' ;
1515import { print } from '../language/printer.js' ;
1616
17- import type { GraphQLField } from '../type/definition.js' ;
17+ import type { GraphQLField , GraphQLInputType } from '../type/definition.js' ;
1818import { isInputType , isNonNullType } from '../type/definition.js' ;
1919import type { GraphQLDirective } from '../type/directives.js' ;
2020import type { GraphQLSchema } from '../type/schema.js' ;
@@ -26,9 +26,20 @@ import {
2626} from '../utilities/coerceInputValue.js' ;
2727import { typeFromAST } from '../utilities/typeFromAST.js' ;
2828
29- type CoercedVariableValues =
30- | { errors : ReadonlyArray < GraphQLError > ; coerced ?: never }
31- | { coerced : { [ variable : string ] : unknown } ; errors ?: never } ;
29+ export interface VariableValues {
30+ readonly sources : ReadOnlyObjMap < VariableValueSource > ;
31+ readonly coerced : ReadOnlyObjMap < unknown > ;
32+ }
33+
34+ interface VariableValueSource {
35+ readonly variable : VariableDefinitionNode ;
36+ readonly type : GraphQLInputType ;
37+ readonly value : unknown ;
38+ }
39+
40+ type VariableValuesOrErrors =
41+ | { variableValues : VariableValues ; errors ?: never }
42+ | { errors : ReadonlyArray < GraphQLError > ; variableValues ?: never } ;
3243
3344/**
3445 * Prepares an object map of variableValues of the correct type based on the
@@ -44,11 +55,11 @@ export function getVariableValues(
4455 varDefNodes : ReadonlyArray < VariableDefinitionNode > ,
4556 inputs : { readonly [ variable : string ] : unknown } ,
4657 options ?: { maxErrors ?: number } ,
47- ) : CoercedVariableValues {
48- const errors = [ ] ;
58+ ) : VariableValuesOrErrors {
59+ const errors : Array < GraphQLError > = [ ] ;
4960 const maxErrors = options ?. maxErrors ;
5061 try {
51- const coerced = coerceVariableValues (
62+ const variableValues = coerceVariableValues (
5263 schema ,
5364 varDefNodes ,
5465 inputs ,
@@ -63,7 +74,7 @@ export function getVariableValues(
6374 ) ;
6475
6576 if ( errors . length === 0 ) {
66- return { coerced } ;
77+ return { variableValues } ;
6778 }
6879 } catch ( error ) {
6980 errors . push ( error ) ;
@@ -77,8 +88,9 @@ function coerceVariableValues(
7788 varDefNodes : ReadonlyArray < VariableDefinitionNode > ,
7889 inputs : { readonly [ variable : string ] : unknown } ,
7990 onError : ( error : GraphQLError ) => void ,
80- ) : { [ variable : string ] : unknown } {
81- const coercedValues : { [ variable : string ] : unknown } = { } ;
91+ ) : VariableValues {
92+ const sources : ObjMap < VariableValueSource > = Object . create ( null ) ;
93+ const coerced : ObjMap < unknown > = Object . create ( null ) ;
8294 for ( const varDefNode of varDefNodes ) {
8395 const varName = varDefNode . variable . name . value ;
8496 const varType = typeFromAST ( schema , varDefNode . type ) ;
@@ -96,11 +108,14 @@ function coerceVariableValues(
96108 }
97109
98110 if ( ! hasOwnProperty ( inputs , varName ) ) {
99- if ( varDefNode . defaultValue ) {
100- coercedValues [ varName ] = coerceInputLiteral (
101- varDefNode . defaultValue ,
102- varType ,
103- ) ;
111+ const defaultValue = varDefNode . defaultValue ;
112+ if ( defaultValue ) {
113+ sources [ varName ] = {
114+ variable : varDefNode ,
115+ type : varType ,
116+ value : undefined ,
117+ } ;
118+ coerced [ varName ] = coerceInputLiteral ( defaultValue , varType ) ;
104119 } else if ( isNonNullType ( varType ) ) {
105120 onError (
106121 new GraphQLError (
@@ -123,7 +138,8 @@ function coerceVariableValues(
123138 continue ;
124139 }
125140
126- coercedValues [ varName ] = coerceInputValue (
141+ sources [ varName ] = { variable : varDefNode , type : varType , value } ;
142+ coerced [ varName ] = coerceInputValue (
127143 value ,
128144 varType ,
129145 ( path , invalidValue , error ) => {
@@ -142,7 +158,7 @@ function coerceVariableValues(
142158 ) ;
143159 }
144160
145- return coercedValues ;
161+ return { sources , coerced } ;
146162}
147163
148164/**
@@ -156,7 +172,7 @@ function coerceVariableValues(
156172export function getArgumentValues (
157173 def : GraphQLField < unknown , unknown > | GraphQLDirective ,
158174 node : FieldNode | DirectiveNode ,
159- variableValues ?: Maybe < ObjMap < unknown > > ,
175+ variableValues ?: Maybe < VariableValues > ,
160176) : { [ argument : string ] : unknown } {
161177 const coercedValues : { [ argument : string ] : unknown } = { } ;
162178
@@ -192,7 +208,7 @@ export function getArgumentValues(
192208 const variableName = valueNode . name . value ;
193209 if (
194210 variableValues == null ||
195- ! hasOwnProperty ( variableValues , variableName )
211+ ! hasOwnProperty ( variableValues . coerced , variableName )
196212 ) {
197213 if ( argDef . defaultValue ) {
198214 coercedValues [ name ] = coerceDefaultValue (
@@ -208,7 +224,7 @@ export function getArgumentValues(
208224 }
209225 continue ;
210226 }
211- isNull = variableValues [ variableName ] == null ;
227+ isNull = variableValues . coerced [ variableName ] == null ;
212228 }
213229
214230 if ( isNull && isNonNullType ( argType ) ) {
@@ -249,7 +265,7 @@ export function getArgumentValues(
249265export function getDirectiveValues (
250266 directiveDef : GraphQLDirective ,
251267 node : { readonly directives ?: ReadonlyArray < DirectiveNode > | undefined } ,
252- variableValues ?: Maybe < ObjMap < unknown > > ,
268+ variableValues ?: Maybe < VariableValues > ,
253269) : undefined | { [ argument : string ] : unknown } {
254270 const directiveNode = node . directives ?. find (
255271 ( directive ) => directive . name . value === directiveDef . name ,
0 commit comments