1- import type { ObjMap } from '../jsutils/ObjMap' ;
2- import { keyMap } from '../jsutils/keyMap' ;
1+ import type { ReadOnlyObjMap , ReadOnlyObjMapLike } from '../jsutils/ObjMap' ;
32import { inspect } from '../jsutils/inspect' ;
3+ import { keyMap } from '../jsutils/keyMap' ;
44import { printPathArray } from '../jsutils/printPathArray' ;
55
66import { GraphQLError } from '../error/GraphQLError' ;
@@ -14,7 +14,7 @@ import { Kind } from '../language/kinds';
1414import { print } from '../language/printer' ;
1515
1616import type { GraphQLSchema } from '../type/schema' ;
17- import type { GraphQLField } from '../type/definition' ;
17+ import type { GraphQLInputType , GraphQLField } from '../type/definition' ;
1818import type { GraphQLDirective } from '../type/directives' ;
1919import { isInputType , isNonNullType } from '../type/definition' ;
2020
@@ -25,9 +25,18 @@ import {
2525 coerceDefaultValue ,
2626} from '../utilities/coerceInputValue' ;
2727
28- type CoercedVariableValues =
29- | { | errors : $ReadOnlyArray < GraphQLError > | }
30- | { | coerced : { [ variable : string ] : mixed , ... } | } ;
28+ export type VariableValues = { |
29+ + sources : ReadOnlyObjMap < { |
30+ + variable : VariableDefinitionNode ,
31+ + type : GraphQLInputType ,
32+ + value : mixed ,
33+ | } > ,
34+ + coerced : ReadOnlyObjMap < mixed > ,
35+ | } ;
36+
37+ type VariableValuesOrErrors =
38+ | { | variableValues : VariableValues | }
39+ | { | errors : $ReadOnlyArray < GraphQLError > | } ;
3140
3241/**
3342 * Prepares an object map of variableValues of the correct type based on the
@@ -43,13 +52,13 @@ type CoercedVariableValues =
4352export function getVariableValues (
4453 schema : GraphQLSchema ,
4554 varDefNodes : $ReadOnlyArray < VariableDefinitionNode > ,
46- inputs : { + [ variable : string ] : mixed , ... } ,
55+ inputs : ReadOnlyObjMapLike < mixed > ,
4756 options ?: { | maxErrors ?: number | } ,
48- ) : CoercedVariableValues {
57+ ) : VariableValuesOrErrors {
4958 const errors = [ ] ;
5059 const maxErrors = options ?. maxErrors ;
5160 try {
52- const coerced = coerceVariableValues (
61+ const variableValues = coerceVariableValues (
5362 schema ,
5463 varDefNodes ,
5564 inputs ,
@@ -64,7 +73,7 @@ export function getVariableValues(
6473 ) ;
6574
6675 if ( errors . length === 0 ) {
67- return { coerced } ;
76+ return { variableValues } ;
6877 }
6978 } catch ( error ) {
7079 errors . push ( error ) ;
@@ -76,10 +85,11 @@ export function getVariableValues(
7685function coerceVariableValues (
7786 schema : GraphQLSchema ,
7887 varDefNodes : $ReadOnlyArray < VariableDefinitionNode > ,
79- inputs : { + [ variable : string ] : mixed , ... } ,
88+ inputs : ReadOnlyObjMapLike < mixed > ,
8089 onError : ( error : GraphQLError ) = > void ,
81- ) : { [ variable : string ] : mixed , ... } {
82- const coercedValues = { } ;
90+ ) : VariableValues {
91+ const sources = Object . create ( null ) ;
92+ const coerced = Object . create ( null ) ;
8393 for ( const varDefNode of varDefNodes ) {
8494 const varName = varDefNode . variable . name . value ;
8595 const varType = typeFromAST ( schema , varDefNode . type ) ;
@@ -97,11 +107,14 @@ function coerceVariableValues(
97107 }
98108
99109 if ( ! hasOwnProperty ( inputs , varName ) ) {
100- if ( varDefNode . defaultValue ) {
101- coercedValues [ varName ] = coerceInputLiteral (
102- varDefNode . defaultValue ,
103- varType ,
104- ) ;
110+ const defaultValue = varDefNode . defaultValue ;
111+ if ( defaultValue ) {
112+ sources [ varName ] = {
113+ variable : varDefNode ,
114+ type : varType ,
115+ value : undefined ,
116+ } ;
117+ coerced [ varName ] = coerceInputLiteral ( defaultValue , varType ) ;
105118 } else if ( isNonNullType ( varType ) ) {
106119 const varTypeStr = inspect ( varType ) ;
107120 onError (
@@ -126,7 +139,8 @@ function coerceVariableValues(
126139 continue ;
127140 }
128141
129- coercedValues [ varName ] = coerceInputValue (
142+ sources [ varName ] = { variable : varDefNode , type : varType , value } ;
143+ coerced [ varName ] = coerceInputValue (
130144 value ,
131145 varType ,
132146 ( path , invalidValue , error ) => {
@@ -149,7 +163,7 @@ function coerceVariableValues(
149163 ) ;
150164 }
151165
152- return coercedValues ;
166+ return { sources , coerced } ;
153167}
154168
155169/**
@@ -165,7 +179,7 @@ function coerceVariableValues(
165179export function getArgumentValues (
166180 def : GraphQLField < mixed , mixed > | GraphQLDirective ,
167181 node : FieldNode | DirectiveNode ,
168- variableValues ?: ?ObjMap < mixed > ,
182+ variableValues ?: ?VariableValues ,
169183) : { [ argument : string ] : mixed , ... } {
170184 const coercedValues = { } ;
171185
@@ -201,7 +215,7 @@ export function getArgumentValues(
201215 const variableName = valueNode . name . value ;
202216 if (
203217 variableValues == null ||
204- ! hasOwnProperty ( variableValues , variableName )
218+ variableValues . coerced [ variableName ] === undefined
205219 ) {
206220 if ( argDef . defaultValue ) {
207221 coercedValues [ name ] = coerceDefaultValue (
@@ -217,7 +231,7 @@ export function getArgumentValues(
217231 }
218232 continue ;
219233 }
220- isNull = variableValues [ variableName ] == null ;
234+ isNull = variableValues . coerced [ variableName ] == null ;
221235 }
222236
223237 if ( isNull && isNonNullType ( argType ) ) {
@@ -257,7 +271,7 @@ export function getArgumentValues(
257271export function getDirectiveValues (
258272 directiveDef : GraphQLDirective ,
259273 node : { + directives ?: $ReadOnlyArray < DirectiveNode > , ... } ,
260- variableValues ?: ?ObjMap < mixed > ,
274+ variableValues ?: ?VariableValues ,
261275) : void | { [ argument : string ] : mixed , ... } {
262276 // istanbul ignore next (See: 'https://github.com/graphql/graphql-js/issues/2203')
263277 const directiveNode = node . directives ?. find (
0 commit comments