1
- import type { ObjMap } from '../jsutils/ObjMap' ;
2
- import { keyMap } from '../jsutils/keyMap' ;
1
+ import type { ReadOnlyObjMap , ReadOnlyObjMapLike } from '../jsutils/ObjMap' ;
3
2
import { inspect } from '../jsutils/inspect' ;
3
+ import { keyMap } from '../jsutils/keyMap' ;
4
4
import { printPathArray } from '../jsutils/printPathArray' ;
5
5
6
6
import { GraphQLError } from '../error/GraphQLError' ;
@@ -14,7 +14,7 @@ import { Kind } from '../language/kinds';
14
14
import { print } from '../language/printer' ;
15
15
16
16
import type { GraphQLSchema } from '../type/schema' ;
17
- import type { GraphQLField } from '../type/definition' ;
17
+ import type { GraphQLInputType , GraphQLField } from '../type/definition' ;
18
18
import type { GraphQLDirective } from '../type/directives' ;
19
19
import { isInputType , isNonNullType } from '../type/definition' ;
20
20
import { getCoercedDefaultValue } from '../type/defaultValues' ;
@@ -23,9 +23,18 @@ import { typeFromAST } from '../utilities/typeFromAST';
23
23
import { valueFromAST } from '../utilities/valueFromAST' ;
24
24
import { coerceInputValue } from '../utilities/coerceInputValue' ;
25
25
26
+ export type VariableValues = { |
27
+ + sources : ReadOnlyObjMap < { |
28
+ + variable : VariableDefinitionNode ,
29
+ + type : GraphQLInputType ,
30
+ + value : mixed ,
31
+ | } > ,
32
+ + coerced : ReadOnlyObjMap < mixed > ,
33
+ | } ;
34
+
26
35
type CoercedVariableValues =
27
36
| { | errors : $ReadOnlyArray < GraphQLError > | }
28
- | { | coerced : { [ variable : string ] : mixed , ... } | } ;
37
+ | { | coerced : VariableValues | } ;
29
38
30
39
/**
31
40
* Prepares an object map of variableValues of the correct type based on the
@@ -41,7 +50,7 @@ type CoercedVariableValues =
41
50
export function getVariableValues (
42
51
schema : GraphQLSchema ,
43
52
varDefNodes : $ReadOnlyArray < VariableDefinitionNode > ,
44
- inputs : { + [ variable : string ] : mixed , ... } ,
53
+ inputs : ReadOnlyObjMapLike < mixed > ,
45
54
options ?: { | maxErrors ?: number | } ,
46
55
) : CoercedVariableValues {
47
56
const errors = [ ] ;
@@ -74,10 +83,11 @@ export function getVariableValues(
74
83
function coerceVariableValues (
75
84
schema : GraphQLSchema ,
76
85
varDefNodes : $ReadOnlyArray < VariableDefinitionNode > ,
77
- inputs : { + [ variable : string ] : mixed , ... } ,
86
+ inputs : ReadOnlyObjMapLike < mixed > ,
78
87
onError : ( error : GraphQLError ) = > void ,
79
- ) : { [ variable : string ] : mixed , ... } {
80
- const coercedValues = { } ;
88
+ ) : VariableValues {
89
+ const sources = Object . create ( null ) ;
90
+ const coerced = Object . create ( null ) ;
81
91
for ( const varDefNode of varDefNodes ) {
82
92
const varName = varDefNode . variable . name . value ;
83
93
const varType = typeFromAST ( schema , varDefNode . type ) ;
@@ -96,7 +106,12 @@ function coerceVariableValues(
96
106
97
107
if ( ! hasOwnProperty ( inputs , varName ) ) {
98
108
if ( varDefNode . defaultValue ) {
99
- coercedValues [ varName ] = valueFromAST ( varDefNode . defaultValue , varType ) ;
109
+ sources [ varName ] = {
110
+ variable : varDefNode ,
111
+ type : varType ,
112
+ value : undefined ,
113
+ } ;
114
+ coerced [ varName ] = valueFromAST ( varDefNode . defaultValue , varType ) ;
100
115
} else if ( isNonNullType ( varType ) ) {
101
116
const varTypeStr = inspect ( varType ) ;
102
117
onError (
@@ -121,7 +136,8 @@ function coerceVariableValues(
121
136
continue ;
122
137
}
123
138
124
- coercedValues [ varName ] = coerceInputValue (
139
+ sources [ varName ] = { variable : varDefNode , type : varType , value } ;
140
+ coerced [ varName ] = coerceInputValue (
125
141
value ,
126
142
varType ,
127
143
( path , invalidValue , error ) => {
@@ -144,7 +160,7 @@ function coerceVariableValues(
144
160
) ;
145
161
}
146
162
147
- return coercedValues ;
163
+ return { sources , coerced } ;
148
164
}
149
165
150
166
/**
@@ -160,7 +176,7 @@ function coerceVariableValues(
160
176
export function getArgumentValues (
161
177
def : GraphQLField < mixed , mixed > | GraphQLDirective ,
162
178
node : FieldNode | DirectiveNode ,
163
- variableValues ?: ?ObjMap < mixed > ,
179
+ variableValues ?: ?VariableValues ,
164
180
) : { [ argument : string ] : mixed , ... } {
165
181
const coercedValues = { } ;
166
182
@@ -196,7 +212,7 @@ export function getArgumentValues(
196
212
const variableName = valueNode . name . value ;
197
213
if (
198
214
variableValues == null ||
199
- ! hasOwnProperty ( variableValues , variableName )
215
+ variableValues . coerced [ variableName ] === undefined
200
216
) {
201
217
if ( argDef . defaultValue ) {
202
218
coercedValues [ name ] = getCoercedDefaultValue (
@@ -212,7 +228,7 @@ export function getArgumentValues(
212
228
}
213
229
continue ;
214
230
}
215
- isNull = variableValues [ variableName ] == null ;
231
+ isNull = variableValues . coerced [ variableName ] == null ;
216
232
}
217
233
218
234
if ( isNull && isNonNullType ( argType ) ) {
@@ -252,7 +268,7 @@ export function getArgumentValues(
252
268
export function getDirectiveValues (
253
269
directiveDef : GraphQLDirective ,
254
270
node : { + directives ?: $ReadOnlyArray < DirectiveNode > , ... } ,
255
- variableValues ?: ?ObjMap < mixed > ,
271
+ variableValues ?: ?VariableValues ,
256
272
) : void | { [ argument : string ] : mixed , ... } {
257
273
// istanbul ignore next (See: 'https://github.com/graphql/graphql-js/issues/2203')
258
274
const directiveNode = node . directives ?. find (
0 commit comments