Skip to content

Commit

Permalink
fix(replaceVariables): handle missing individual variable source (#4236)
Browse files Browse the repository at this point in the history
extracted from #4206
  • Loading branch information
yaacovCR authored Oct 14, 2024
1 parent 70ad2f5 commit cdd293d
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 8 deletions.
25 changes: 19 additions & 6 deletions src/utilities/__tests__/replaceVariables-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 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'));
});

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', () => {
Expand Down
4 changes: 2 additions & 2 deletions src/utilities/replaceVariables.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit cdd293d

Please sign in to comment.