Skip to content

Commit db2987c

Browse files
authored
fix(valueFromAST): restore variable own-property checks (#4652)
1 parent 123e958 commit db2987c

File tree

2 files changed

+29
-2
lines changed

2 files changed

+29
-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: 12 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-
if (variables == null || variables[variableName] === undefined) {
51+
if (
52+
variables == null ||
53+
variables[variableName] === undefined ||
54+
!hasOwnProperty(variables, variableName)
55+
) {
5256
// No valid return value.
5357
return;
5458
}
@@ -168,6 +172,12 @@ function isMissingVariable(
168172
): boolean {
169173
return (
170174
valueNode.kind === Kind.VARIABLE &&
171-
(variables == null || variables[valueNode.name.value] === undefined)
175+
(variables == null ||
176+
variables[valueNode.name.value] === undefined ||
177+
!hasOwnProperty(variables, valueNode.name.value))
172178
);
173179
}
180+
181+
function hasOwnProperty(obj: ObjMap<unknown>, prop: string): boolean {
182+
return Object.prototype.hasOwnProperty.call(obj, prop);
183+
}

0 commit comments

Comments
 (0)