1- import type { ObjMap } from '../jsutils/ObjMap' ;
1+ import type { ObjMap , ReadOnlyObjMap } from '../jsutils/ObjMap' ;
22import type { Maybe } from '../jsutils/Maybe' ;
33import { keyMap } from '../jsutils/keyMap' ;
44import { inspect } from '../jsutils/inspect' ;
@@ -15,7 +15,7 @@ import { Kind } from '../language/kinds';
1515import { print } from '../language/printer' ;
1616
1717import type { GraphQLSchema } from '../type/schema' ;
18- import type { GraphQLField } from '../type/definition' ;
18+ import type { GraphQLInputType , GraphQLField } from '../type/definition' ;
1919import type { GraphQLDirective } from '../type/directives' ;
2020import { isInputType , isNonNullType } from '../type/definition' ;
2121
@@ -26,9 +26,20 @@ import {
2626 coerceDefaultValue ,
2727} from '../utilities/coerceInputValue' ;
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
@@ -46,11 +57,11 @@ export function getVariableValues(
4657 varDefNodes : ReadonlyArray < VariableDefinitionNode > ,
4758 inputs : { readonly [ variable : string ] : unknown } ,
4859 options ?: { maxErrors ?: number } ,
49- ) : CoercedVariableValues {
50- const errors = [ ] ;
60+ ) : VariableValuesOrErrors {
61+ const errors : Array < GraphQLError > = [ ] ;
5162 const maxErrors = options ?. maxErrors ;
5263 try {
53- const coerced = coerceVariableValues (
64+ const variableValues = coerceVariableValues (
5465 schema ,
5566 varDefNodes ,
5667 inputs ,
@@ -65,7 +76,7 @@ export function getVariableValues(
6576 ) ;
6677
6778 if ( errors . length === 0 ) {
68- return { coerced } ;
79+ return { variableValues } ;
6980 }
7081 } catch ( error ) {
7182 errors . push ( error ) ;
@@ -79,8 +90,9 @@ function coerceVariableValues(
7990 varDefNodes : ReadonlyArray < VariableDefinitionNode > ,
8091 inputs : { readonly [ variable : string ] : unknown } ,
8192 onError : ( error : GraphQLError ) => void ,
82- ) : { [ variable : string ] : unknown } {
83- const coercedValues : { [ variable : string ] : unknown } = { } ;
93+ ) : VariableValues {
94+ const sources : ObjMap < VariableValueSource > = Object . create ( null ) ;
95+ const coerced : ObjMap < unknown > = Object . create ( null ) ;
8496 for ( const varDefNode of varDefNodes ) {
8597 const varName = varDefNode . variable . name . value ;
8698 const varType = typeFromAST ( schema , varDefNode . type ) ;
@@ -98,11 +110,14 @@ function coerceVariableValues(
98110 }
99111
100112 if ( ! hasOwnProperty ( inputs , varName ) ) {
101- if ( varDefNode . defaultValue ) {
102- coercedValues [ varName ] = coerceInputLiteral (
103- varDefNode . defaultValue ,
104- varType ,
105- ) ;
113+ const defaultValue = varDefNode . defaultValue ;
114+ if ( defaultValue ) {
115+ sources [ varName ] = {
116+ variable : varDefNode ,
117+ type : varType ,
118+ value : undefined ,
119+ } ;
120+ coerced [ varName ] = coerceInputLiteral ( defaultValue , varType ) ;
106121 } else if ( isNonNullType ( varType ) ) {
107122 const varTypeStr = inspect ( varType ) ;
108123 onError (
@@ -127,7 +142,8 @@ function coerceVariableValues(
127142 continue ;
128143 }
129144
130- coercedValues [ varName ] = coerceInputValue (
145+ sources [ varName ] = { variable : varDefNode , type : varType , value } ;
146+ coerced [ varName ] = coerceInputValue (
131147 value ,
132148 varType ,
133149 ( path , invalidValue , error ) => {
@@ -150,7 +166,7 @@ function coerceVariableValues(
150166 ) ;
151167 }
152168
153- return coercedValues ;
169+ return { sources , coerced } ;
154170}
155171
156172/**
@@ -166,7 +182,7 @@ function coerceVariableValues(
166182export function getArgumentValues (
167183 def : GraphQLField < unknown , unknown > | GraphQLDirective ,
168184 node : FieldNode | DirectiveNode ,
169- variableValues ?: Maybe < ObjMap < unknown > > ,
185+ variableValues ?: Maybe < VariableValues > ,
170186) : { [ argument : string ] : unknown } {
171187 const coercedValues : { [ argument : string ] : unknown } = { } ;
172188
@@ -202,7 +218,7 @@ export function getArgumentValues(
202218 const variableName = valueNode . name . value ;
203219 if (
204220 variableValues == null ||
205- ! hasOwnProperty ( variableValues , variableName )
221+ variableValues . coerced [ variableName ] === undefined
206222 ) {
207223 if ( argDef . defaultValue ) {
208224 coercedValues [ name ] = coerceDefaultValue (
@@ -218,7 +234,7 @@ export function getArgumentValues(
218234 }
219235 continue ;
220236 }
221- isNull = variableValues [ variableName ] == null ;
237+ isNull = variableValues . coerced [ variableName ] == null ;
222238 }
223239
224240 if ( isNull && isNonNullType ( argType ) ) {
@@ -258,7 +274,7 @@ export function getArgumentValues(
258274export function getDirectiveValues (
259275 directiveDef : GraphQLDirective ,
260276 node : { readonly directives ?: ReadonlyArray < DirectiveNode > } ,
261- variableValues ?: Maybe < ObjMap < unknown > > ,
277+ variableValues ?: Maybe < VariableValues > ,
262278) : undefined | { [ argument : string ] : unknown } {
263279 // istanbul ignore next (See: 'https://github.com/graphql/graphql-js/issues/2203')
264280 const directiveNode = node . directives ?. find (
0 commit comments