1
- import type { ObjMap } from '../jsutils/ObjMap' ;
1
+ import type { ObjMap , ReadOnlyObjMap } from '../jsutils/ObjMap' ;
2
2
import type { Maybe } from '../jsutils/Maybe' ;
3
3
import { keyMap } from '../jsutils/keyMap' ;
4
4
import { inspect } from '../jsutils/inspect' ;
@@ -15,7 +15,7 @@ import { Kind } from '../language/kinds';
15
15
import { print } from '../language/printer' ;
16
16
17
17
import type { GraphQLSchema } from '../type/schema' ;
18
- import type { GraphQLField } from '../type/definition' ;
18
+ import type { GraphQLInputType , GraphQLField } from '../type/definition' ;
19
19
import type { GraphQLDirective } from '../type/directives' ;
20
20
import { isInputType , isNonNullType } from '../type/definition' ;
21
21
@@ -26,9 +26,20 @@ import {
26
26
coerceDefaultValue ,
27
27
} from '../utilities/coerceInputValue' ;
28
28
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 } ;
32
43
33
44
/**
34
45
* Prepares an object map of variableValues of the correct type based on the
@@ -46,11 +57,11 @@ export function getVariableValues(
46
57
varDefNodes : ReadonlyArray < VariableDefinitionNode > ,
47
58
inputs : { readonly [ variable : string ] : unknown } ,
48
59
options ?: { maxErrors ?: number } ,
49
- ) : CoercedVariableValues {
50
- const errors = [ ] ;
60
+ ) : VariableValuesOrErrors {
61
+ const errors : Array < GraphQLError > = [ ] ;
51
62
const maxErrors = options ?. maxErrors ;
52
63
try {
53
- const coerced = coerceVariableValues (
64
+ const variableValues = coerceVariableValues (
54
65
schema ,
55
66
varDefNodes ,
56
67
inputs ,
@@ -65,7 +76,7 @@ export function getVariableValues(
65
76
) ;
66
77
67
78
if ( errors . length === 0 ) {
68
- return { coerced } ;
79
+ return { variableValues } ;
69
80
}
70
81
} catch ( error ) {
71
82
errors . push ( error ) ;
@@ -79,8 +90,9 @@ function coerceVariableValues(
79
90
varDefNodes : ReadonlyArray < VariableDefinitionNode > ,
80
91
inputs : { readonly [ variable : string ] : unknown } ,
81
92
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 ) ;
84
96
for ( const varDefNode of varDefNodes ) {
85
97
const varName = varDefNode . variable . name . value ;
86
98
const varType = typeFromAST ( schema , varDefNode . type ) ;
@@ -98,11 +110,14 @@ function coerceVariableValues(
98
110
}
99
111
100
112
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 ) ;
106
121
} else if ( isNonNullType ( varType ) ) {
107
122
const varTypeStr = inspect ( varType ) ;
108
123
onError (
@@ -127,7 +142,8 @@ function coerceVariableValues(
127
142
continue ;
128
143
}
129
144
130
- coercedValues [ varName ] = coerceInputValue (
145
+ sources [ varName ] = { variable : varDefNode , type : varType , value } ;
146
+ coerced [ varName ] = coerceInputValue (
131
147
value ,
132
148
varType ,
133
149
( path , invalidValue , error ) => {
@@ -150,7 +166,7 @@ function coerceVariableValues(
150
166
) ;
151
167
}
152
168
153
- return coercedValues ;
169
+ return { sources , coerced } ;
154
170
}
155
171
156
172
/**
@@ -166,7 +182,7 @@ function coerceVariableValues(
166
182
export function getArgumentValues (
167
183
def : GraphQLField < unknown , unknown > | GraphQLDirective ,
168
184
node : FieldNode | DirectiveNode ,
169
- variableValues ?: Maybe < ObjMap < unknown > > ,
185
+ variableValues ?: Maybe < VariableValues > ,
170
186
) : { [ argument : string ] : unknown } {
171
187
const coercedValues : { [ argument : string ] : unknown } = { } ;
172
188
@@ -202,7 +218,7 @@ export function getArgumentValues(
202
218
const variableName = valueNode . name . value ;
203
219
if (
204
220
variableValues == null ||
205
- ! hasOwnProperty ( variableValues , variableName )
221
+ variableValues . coerced [ variableName ] === undefined
206
222
) {
207
223
if ( argDef . defaultValue ) {
208
224
coercedValues [ name ] = coerceDefaultValue (
@@ -218,7 +234,7 @@ export function getArgumentValues(
218
234
}
219
235
continue ;
220
236
}
221
- isNull = variableValues [ variableName ] == null ;
237
+ isNull = variableValues . coerced [ variableName ] == null ;
222
238
}
223
239
224
240
if ( isNull && isNonNullType ( argType ) ) {
@@ -258,7 +274,7 @@ export function getArgumentValues(
258
274
export function getDirectiveValues (
259
275
directiveDef : GraphQLDirective ,
260
276
node : { readonly directives ?: ReadonlyArray < DirectiveNode > } ,
261
- variableValues ?: Maybe < ObjMap < unknown > > ,
277
+ variableValues ?: Maybe < VariableValues > ,
262
278
) : undefined | { [ argument : string ] : unknown } {
263
279
// istanbul ignore next (See: 'https://github.com/graphql/graphql-js/issues/2203')
264
280
const directiveNode = node . directives ?. find (
0 commit comments