Skip to content

Commit b4c1624

Browse files
fix(valueFromAST): restore variable own-property checks (#4671)
forward port of #4652 Co-authored-by: Abishek Kumar Giri <giriabishekkumar5@gmail.com>
1 parent e09f7da commit b4c1624

File tree

2 files changed

+24
-2
lines changed

2 files changed

+24
-2
lines changed

src/utilities/__tests__/valueFromAST-test.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,11 +252,19 @@ describe('valueFromAST', () => {
252252
expectValueFrom('$var', GraphQLBoolean, { var: true }).to.equal(true);
253253
expectValueFrom('$var', GraphQLBoolean, { var: null }).to.equal(null);
254254
expectValueFrom('$var', nonNullBool, { var: null }).to.equal(undefined);
255+
expectValueFrom('$toString', GraphQLBoolean, {}).to.equal(undefined);
256+
expectValueFrom('$var', GraphQLBoolean, { var: undefined }).to.equal(
257+
undefined,
258+
);
255259
});
256260

257261
it('asserts variables are provided as items in lists', () => {
258262
expectValueFrom('[ $foo ]', listOfBool, {}).to.deep.equal([null]);
263+
expectValueFrom('[ $foo ]', listOfBool, { foo: undefined }).to.deep.equal([
264+
null,
265+
]);
259266
expectValueFrom('[ $foo ]', listOfNonNullBool, {}).to.equal(undefined);
267+
expectValueFrom('[ $toString ]', listOfBool, {}).to.deep.equal([null]);
260268
expectValueFrom('[ $foo ]', listOfNonNullBool, {
261269
foo: true,
262270
}).to.deep.equal([true]);
@@ -285,5 +293,14 @@ describe('valueFromAST', () => {
285293
int: 42,
286294
requiredBool: true,
287295
});
296+
297+
expectValueFrom(
298+
'{ int: $toString, requiredBool: true }',
299+
testInputObj,
300+
{},
301+
).to.deep.equal({
302+
int: 42,
303+
requiredBool: true,
304+
});
288305
});
289306
});

src/utilities/valueFromAST.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,11 @@ export function valueFromAST(
4848

4949
if (valueNode.kind === Kind.VARIABLE) {
5050
const variableName = valueNode.name.value;
51-
const variableValue = variables?.[variableName];
51+
if (variables == null || !Object.hasOwn(variables, variableName)) {
52+
// No valid return value.
53+
return;
54+
}
55+
const variableValue = variables[variableName];
5256
if (variableValue === undefined) {
5357
// No valid return value.
5458
return;
@@ -172,6 +176,7 @@ function isMissingVariable(
172176
): boolean {
173177
return (
174178
valueNode.kind === Kind.VARIABLE &&
175-
variables?.[valueNode.name.value] === undefined
179+
(variables?.[valueNode.name.value] === undefined ||
180+
!Object.hasOwn(variables, valueNode.name.value))
176181
);
177182
}

0 commit comments

Comments
 (0)