11import { inspect } from '../jsutils/inspect.js' ;
22import type { Maybe } from '../jsutils/Maybe.js' ;
3- import type { ObjMap } from '../jsutils/ObjMap.js ' ;
3+ import type { ObjMap , ReadOnlyObjMap } from '../jsutils/ObjMap' ;
44import { printPathArray } from '../jsutils/printPathArray.js' ;
55
66import { GraphQLError } from '../error/GraphQLError.js' ;
@@ -13,7 +13,7 @@ import type {
1313import { Kind } from '../language/kinds.js' ;
1414import { print } from '../language/printer.js' ;
1515
16- import type { GraphQLField } from '../type/definition.js' ;
16+ import type { GraphQLField , GraphQLInputType } from '../type/definition.js' ;
1717import { isInputType , isNonNullType } from '../type/definition.js' ;
1818import type { GraphQLDirective } from '../type/directives.js' ;
1919import type { GraphQLSchema } from '../type/schema.js' ;
@@ -25,9 +25,20 @@ import {
2525} from '../utilities/coerceInputValue.js' ;
2626import { typeFromAST } from '../utilities/typeFromAST.js' ;
2727
28- type CoercedVariableValues =
29- | { errors : ReadonlyArray < GraphQLError > ; coerced ?: never }
30- | { coerced : { [ variable : string ] : unknown } ; errors ?: never } ;
28+ export interface VariableValues {
29+ readonly sources : ReadOnlyObjMap < VariableValueSource > ;
30+ readonly coerced : ReadOnlyObjMap < unknown > ;
31+ }
32+
33+ interface VariableValueSource {
34+ readonly variable : VariableDefinitionNode ;
35+ readonly type : GraphQLInputType ;
36+ readonly value : unknown ;
37+ }
38+
39+ type VariableValuesOrErrors =
40+ | { variableValues : VariableValues ; errors ?: never }
41+ | { errors : ReadonlyArray < GraphQLError > ; variableValues ?: never } ;
3142
3243/**
3344 * Prepares an object map of variableValues of the correct type based on the
@@ -43,11 +54,11 @@ export function getVariableValues(
4354 varDefNodes : ReadonlyArray < VariableDefinitionNode > ,
4455 inputs : { readonly [ variable : string ] : unknown } ,
4556 options ?: { maxErrors ?: number } ,
46- ) : CoercedVariableValues {
47- const errors = [ ] ;
57+ ) : VariableValuesOrErrors {
58+ const errors : Array < GraphQLError > = [ ] ;
4859 const maxErrors = options ?. maxErrors ;
4960 try {
50- const coerced = coerceVariableValues (
61+ const variableValues = coerceVariableValues (
5162 schema ,
5263 varDefNodes ,
5364 inputs ,
@@ -62,7 +73,7 @@ export function getVariableValues(
6273 ) ;
6374
6475 if ( errors . length === 0 ) {
65- return { coerced } ;
76+ return { variableValues } ;
6677 }
6778 } catch ( error ) {
6879 errors . push ( error ) ;
@@ -76,8 +87,9 @@ function coerceVariableValues(
7687 varDefNodes : ReadonlyArray < VariableDefinitionNode > ,
7788 inputs : { readonly [ variable : string ] : unknown } ,
7889 onError : ( error : GraphQLError ) => void ,
79- ) : { [ variable : string ] : unknown } {
80- const coercedValues : { [ variable : string ] : unknown } = { } ;
90+ ) : VariableValues {
91+ const sources : ObjMap < VariableValueSource > = Object . create ( null ) ;
92+ const coerced : ObjMap < unknown > = Object . create ( null ) ;
8193 for ( const varDefNode of varDefNodes ) {
8294 const varName = varDefNode . variable . name . value ;
8395 const varType = typeFromAST ( schema , varDefNode . type ) ;
@@ -95,11 +107,14 @@ function coerceVariableValues(
95107 }
96108
97109 if ( ! Object . hasOwn ( inputs , varName ) ) {
98- if ( varDefNode . defaultValue ) {
99- coercedValues [ varName ] = coerceInputLiteral (
100- varDefNode . defaultValue ,
101- varType ,
102- ) ;
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 ) ;
103118 } else if ( isNonNullType ( varType ) ) {
104119 onError (
105120 new GraphQLError (
@@ -122,7 +137,8 @@ function coerceVariableValues(
122137 continue ;
123138 }
124139
125- coercedValues [ varName ] = coerceInputValue (
140+ sources [ varName ] = { variable : varDefNode , type : varType , value } ;
141+ coerced [ varName ] = coerceInputValue (
126142 value ,
127143 varType ,
128144 ( path , invalidValue , error ) => {
@@ -141,7 +157,7 @@ function coerceVariableValues(
141157 ) ;
142158 }
143159
144- return coercedValues ;
160+ return { sources , coerced } ;
145161}
146162
147163/**
@@ -155,7 +171,7 @@ function coerceVariableValues(
155171export function getArgumentValues (
156172 def : GraphQLField < unknown , unknown > | GraphQLDirective ,
157173 node : FieldNode | DirectiveNode ,
158- variableValues ?: Maybe < ObjMap < unknown > > ,
174+ variableValues ?: Maybe < VariableValues > ,
159175) : { [ argument : string ] : unknown } {
160176 const coercedValues : { [ argument : string ] : unknown } = { } ;
161177
@@ -191,7 +207,7 @@ export function getArgumentValues(
191207 const variableName = valueNode . name . value ;
192208 if (
193209 variableValues == null ||
194- ! Object . hasOwn ( variableValues , variableName )
210+ ! Object . hasOwn ( variableValues . coerced , variableName )
195211 ) {
196212 if ( argDef . defaultValue ) {
197213 coercedValues [ name ] = coerceDefaultValue (
@@ -207,7 +223,7 @@ export function getArgumentValues(
207223 }
208224 continue ;
209225 }
210- isNull = variableValues [ variableName ] == null ;
226+ isNull = variableValues . coerced [ variableName ] == null ;
211227 }
212228
213229 if ( isNull && isNonNullType ( argType ) ) {
@@ -248,7 +264,7 @@ export function getArgumentValues(
248264export function getDirectiveValues (
249265 directiveDef : GraphQLDirective ,
250266 node : { readonly directives ?: ReadonlyArray < DirectiveNode > | undefined } ,
251- variableValues ?: Maybe < ObjMap < unknown > > ,
267+ variableValues ?: Maybe < VariableValues > ,
252268) : undefined | { [ argument : string ] : unknown } {
253269 const directiveNode = node . directives ?. find (
254270 ( directive ) => directive . name . value === directiveDef . name ,
0 commit comments