Skip to content

Commit cdd293d

Browse files
authored
fix(replaceVariables): handle missing individual variable source (#4236)
extracted from #4206
1 parent 70ad2f5 commit cdd293d

File tree

2 files changed

+21
-8
lines changed

2 files changed

+21
-8
lines changed

src/utilities/__tests__/replaceVariables-test.ts

+19-6
Original file line numberDiff line numberDiff line change
@@ -65,19 +65,32 @@ describe('replaceVariables', () => {
6565
);
6666
});
6767

68+
it('replaces missing variable declaration with null', () => {
69+
const ast = parseValue('$var');
70+
const vars = testVariables('', {});
71+
expect(replaceVariables(ast, vars)).to.deep.equal(parseValue('null'));
72+
});
73+
74+
it('replaces misspelled variable declaration with null', () => {
75+
const ast = parseValue('$var1');
76+
const vars = testVariables('($var2: Int)', { var2: 123 });
77+
expect(replaceVariables(ast, vars)).to.deep.equal(parseValue('null'));
78+
});
79+
6880
it('replaces missing Variables in lists with null', () => {
6981
const ast = parseValue('[1, $var]');
7082
expect(replaceVariables(ast, undefined)).to.deep.equal(
7183
parseValue('[1, null]'),
7284
);
7385
});
74-
});
7586

76-
it('omits missing Variables from objects', () => {
77-
const ast = parseValue('{ foo: 1, bar: $var }');
78-
expect(replaceVariables(ast, undefined)).to.deep.equal(
79-
parseValue('{ foo: 1 }'),
80-
);
87+
it('omits missing Variables from objects', () => {
88+
const ast = parseValue('{ foo: 1, bar: $var }');
89+
const vars = testVariables('($wrongVar: Int)', { var: 123 });
90+
expect(replaceVariables(ast, vars)).to.deep.equal(
91+
parseValue('{ foo: 1 }'),
92+
);
93+
});
8194
});
8295

8396
describe('Fragment Variables', () => {

src/utilities/replaceVariables.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,11 @@ export function replaceVariables(
3131
? fragmentVariableValues
3232
: variableValues;
3333

34-
if (scopedVariableValues == null) {
34+
const scopedVariableSource = scopedVariableValues?.sources[varName];
35+
if (scopedVariableSource == null) {
3536
return { kind: Kind.NULL };
3637
}
3738

38-
const scopedVariableSource = scopedVariableValues.sources[varName];
3939
if (scopedVariableSource.value === undefined) {
4040
const defaultValue = scopedVariableSource.signature.defaultValue;
4141
if (defaultValue !== undefined) {

0 commit comments

Comments
 (0)