From 3c1f90ad04951905985e3a0139d6714249c635f7 Mon Sep 17 00:00:00 2001 From: Yaacov Rydzinski Date: Mon, 14 Oct 2024 13:05:04 +0300 Subject: [PATCH 1/2] fix(replaceVariables): handle missing individual variable source --- .../__tests__/replaceVariables-test.ts | 25 ++++++++++++++----- src/utilities/replaceVariables.ts | 4 +-- 2 files changed, 21 insertions(+), 8 deletions(-) diff --git a/src/utilities/__tests__/replaceVariables-test.ts b/src/utilities/__tests__/replaceVariables-test.ts index df6293fc78..25cb2f83b2 100644 --- a/src/utilities/__tests__/replaceVariables-test.ts +++ b/src/utilities/__tests__/replaceVariables-test.ts @@ -65,19 +65,32 @@ describe('replaceVariables', () => { ); }); + it('replaces missing variable declaration with null', () => { + const ast = parseValue('$var'); + const vars = testVariables('', {}); + expect(replaceVariables(ast, vars)).to.deep.equal(parseValue('null')); + }); + + it('replaces mispelled variable declaration with null', () => { + const ast = parseValue('$var1'); + const vars = testVariables('($var2: Int)', { var2: 123 }); + expect(replaceVariables(ast, vars)).to.deep.equal(parseValue('null')); + }); + it('replaces missing Variables in lists with null', () => { const ast = parseValue('[1, $var]'); expect(replaceVariables(ast, undefined)).to.deep.equal( parseValue('[1, null]'), ); }); - }); - it('omits missing Variables from objects', () => { - const ast = parseValue('{ foo: 1, bar: $var }'); - expect(replaceVariables(ast, undefined)).to.deep.equal( - parseValue('{ foo: 1 }'), - ); + it('omits missing Variables from objects', () => { + const ast = parseValue('{ foo: 1, bar: $var }'); + const vars = testVariables('($wrongVar: Int)', { var: 123 }); + expect(replaceVariables(ast, vars)).to.deep.equal( + parseValue('{ foo: 1 }'), + ); + }); }); describe('Fragment Variables', () => { diff --git a/src/utilities/replaceVariables.ts b/src/utilities/replaceVariables.ts index 307c987a01..531863779a 100644 --- a/src/utilities/replaceVariables.ts +++ b/src/utilities/replaceVariables.ts @@ -31,11 +31,11 @@ export function replaceVariables( ? fragmentVariableValues : variableValues; - if (scopedVariableValues == null) { + const scopedVariableSource = scopedVariableValues?.sources[varName]; + if (scopedVariableSource == null) { return { kind: Kind.NULL }; } - const scopedVariableSource = scopedVariableValues.sources[varName]; if (scopedVariableSource.value === undefined) { const defaultValue = scopedVariableSource.signature.defaultValue; if (defaultValue !== undefined) { From e5ea13b2d9ce6759e8b66d0c74d0bc41e2d72d1c Mon Sep 17 00:00:00 2001 From: Yaacov Rydzinski Date: Mon, 14 Oct 2024 13:18:50 +0300 Subject: [PATCH 2/2] there is an irony here --- src/utilities/__tests__/replaceVariables-test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/utilities/__tests__/replaceVariables-test.ts b/src/utilities/__tests__/replaceVariables-test.ts index 25cb2f83b2..2ab97164f2 100644 --- a/src/utilities/__tests__/replaceVariables-test.ts +++ b/src/utilities/__tests__/replaceVariables-test.ts @@ -71,7 +71,7 @@ describe('replaceVariables', () => { expect(replaceVariables(ast, vars)).to.deep.equal(parseValue('null')); }); - it('replaces mispelled variable declaration with null', () => { + it('replaces misspelled variable declaration with null', () => { const ast = parseValue('$var1'); const vars = testVariables('($var2: Int)', { var2: 123 }); expect(replaceVariables(ast, vars)).to.deep.equal(parseValue('null'));