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
onError (
108
123
new GraphQLError (
@@ -125,7 +140,8 @@ function coerceVariableValues(
125
140
continue ;
126
141
}
127
142
128
- coercedValues [ varName ] = coerceInputValue (
143
+ sources [ varName ] = { variable : varDefNode , type : varType , value } ;
144
+ coerced [ varName ] = coerceInputValue (
129
145
value ,
130
146
varType ,
131
147
( path , invalidValue , error ) => {
@@ -148,7 +164,7 @@ function coerceVariableValues(
148
164
) ;
149
165
}
150
166
151
- return coercedValues ;
167
+ return { sources , coerced } ;
152
168
}
153
169
154
170
/**
@@ -164,7 +180,7 @@ function coerceVariableValues(
164
180
export function getArgumentValues (
165
181
def : GraphQLField < unknown , unknown > | GraphQLDirective ,
166
182
node : FieldNode | DirectiveNode ,
167
- variableValues ?: Maybe < ObjMap < unknown > > ,
183
+ variableValues ?: Maybe < VariableValues > ,
168
184
) : { [ argument : string ] : unknown } {
169
185
const coercedValues : { [ argument : string ] : unknown } = { } ;
170
186
@@ -199,7 +215,7 @@ export function getArgumentValues(
199
215
const variableName = valueNode . name . value ;
200
216
if (
201
217
variableValues == null ||
202
- ! hasOwnProperty ( variableValues , variableName )
218
+ variableValues . coerced [ variableName ] === undefined
203
219
) {
204
220
if ( argDef . defaultValue ) {
205
221
coercedValues [ name ] = coerceDefaultValue (
@@ -215,7 +231,7 @@ export function getArgumentValues(
215
231
}
216
232
continue ;
217
233
}
218
- isNull = variableValues [ variableName ] == null ;
234
+ isNull = variableValues . coerced [ variableName ] == null ;
219
235
}
220
236
221
237
if ( isNull && isNonNullType ( argType ) ) {
@@ -256,7 +272,7 @@ export function getArgumentValues(
256
272
export function getDirectiveValues (
257
273
directiveDef : GraphQLDirective ,
258
274
node : { readonly directives ?: ReadonlyArray < DirectiveNode > } ,
259
- variableValues ?: Maybe < ObjMap < unknown > > ,
275
+ variableValues ?: Maybe < VariableValues > ,
260
276
) : undefined | { [ argument : string ] : unknown } {
261
277
// istanbul ignore next (See: 'https://github.com/graphql/graphql-js/issues/2203')
262
278
const directiveNode = node . directives ?. find (
0 commit comments