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
@@ -25,9 +25,18 @@ import {
25
25
coerceDefaultValue ,
26
26
} from '../utilities/coerceInputValue' ;
27
27
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 > | } ;
31
40
32
41
/**
33
42
* Prepares an object map of variableValues of the correct type based on the
@@ -43,13 +52,13 @@ type CoercedVariableValues =
43
52
export function getVariableValues (
44
53
schema : GraphQLSchema ,
45
54
varDefNodes : $ReadOnlyArray < VariableDefinitionNode > ,
46
- inputs : { + [ variable : string ] : mixed , ... } ,
55
+ inputs : ReadOnlyObjMapLike < mixed > ,
47
56
options ?: { | maxErrors ?: number | } ,
48
- ) : CoercedVariableValues {
57
+ ) : VariableValuesOrErrors {
49
58
const errors = [ ] ;
50
59
const maxErrors = options ?. maxErrors ;
51
60
try {
52
- const coerced = coerceVariableValues (
61
+ const variableValues = coerceVariableValues (
53
62
schema ,
54
63
varDefNodes ,
55
64
inputs ,
@@ -64,7 +73,7 @@ export function getVariableValues(
64
73
) ;
65
74
66
75
if ( errors . length === 0 ) {
67
- return { coerced } ;
76
+ return { variableValues } ;
68
77
}
69
78
} catch ( error ) {
70
79
errors . push ( error ) ;
@@ -76,10 +85,11 @@ export function getVariableValues(
76
85
function coerceVariableValues (
77
86
schema : GraphQLSchema ,
78
87
varDefNodes : $ReadOnlyArray < VariableDefinitionNode > ,
79
- inputs : { + [ variable : string ] : mixed , ... } ,
88
+ inputs : ReadOnlyObjMapLike < mixed > ,
80
89
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 ) ;
83
93
for ( const varDefNode of varDefNodes ) {
84
94
const varName = varDefNode . variable . name . value ;
85
95
const varType = typeFromAST ( schema , varDefNode . type ) ;
@@ -97,11 +107,14 @@ function coerceVariableValues(
97
107
}
98
108
99
109
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 ) ;
105
118
} else if ( isNonNullType ( varType ) ) {
106
119
const varTypeStr = inspect ( varType ) ;
107
120
onError (
@@ -126,7 +139,8 @@ function coerceVariableValues(
126
139
continue ;
127
140
}
128
141
129
- coercedValues [ varName ] = coerceInputValue (
142
+ sources [ varName ] = { variable : varDefNode , type : varType , value } ;
143
+ coerced [ varName ] = coerceInputValue (
130
144
value ,
131
145
varType ,
132
146
( path , invalidValue , error ) => {
@@ -149,7 +163,7 @@ function coerceVariableValues(
149
163
) ;
150
164
}
151
165
152
- return coercedValues ;
166
+ return { sources , coerced } ;
153
167
}
154
168
155
169
/**
@@ -165,7 +179,7 @@ function coerceVariableValues(
165
179
export function getArgumentValues (
166
180
def : GraphQLField < mixed , mixed > | GraphQLDirective ,
167
181
node : FieldNode | DirectiveNode ,
168
- variableValues ?: ?ObjMap < mixed > ,
182
+ variableValues ?: ?VariableValues ,
169
183
) : { [ argument : string ] : mixed , ... } {
170
184
const coercedValues = { } ;
171
185
@@ -201,7 +215,7 @@ export function getArgumentValues(
201
215
const variableName = valueNode . name . value ;
202
216
if (
203
217
variableValues == null ||
204
- ! hasOwnProperty ( variableValues , variableName )
218
+ variableValues . coerced [ variableName ] === undefined
205
219
) {
206
220
if ( argDef . defaultValue ) {
207
221
coercedValues [ name ] = coerceDefaultValue (
@@ -217,7 +231,7 @@ export function getArgumentValues(
217
231
}
218
232
continue ;
219
233
}
220
- isNull = variableValues [ variableName ] == null ;
234
+ isNull = variableValues . coerced [ variableName ] == null ;
221
235
}
222
236
223
237
if ( isNull && isNonNullType ( argType ) ) {
@@ -257,7 +271,7 @@ export function getArgumentValues(
257
271
export function getDirectiveValues (
258
272
directiveDef : GraphQLDirective ,
259
273
node : { + directives ?: $ReadOnlyArray < DirectiveNode > , ... } ,
260
- variableValues ?: ?ObjMap < mixed > ,
274
+ variableValues ?: ?VariableValues ,
261
275
) : void | { [ argument : string ] : mixed , ... } {
262
276
// istanbul ignore next (See: 'https://github.com/graphql/graphql-js/issues/2203')
263
277
const directiveNode = node . directives ?. find (
0 commit comments