Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions src/execution/__tests__/variables-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -451,6 +451,22 @@ describe('Execute: Handles inputs', () => {
});
});

it('treats explicitly undefined variable values as omitted', () => {
const result = executeQuery(
`
query q($input: String = "Default value") {
fieldWithNullableStringInput(input: $input)
}`,
{ input: undefined },
);

expect(result).to.deep.equal({
data: {
fieldWithNullableStringInput: '"Default value"',
},
});
});

it('uses null default value when not provided', () => {
const result = executeQuery(
`
Expand Down Expand Up @@ -1711,4 +1727,27 @@ describe('Execute: Handles inputs', () => {
expect(result.variableValues.coerced.toString).to.equal('value');
});
});

describe('getVariableValues: explicit undefined values', () => {
const doc = parse(`
query ($input: String) {
fieldWithNullableStringInput(input: $input)
}
`);

const operation = doc.definitions[0];
assert(operation.kind === Kind.OPERATION_DEFINITION);
const { variableDefinitions } = operation;
assert(variableDefinitions != null);

it('treats explicit undefined values as omitted', () => {
const result = getVariableValues(schema, variableDefinitions, {
input: undefined,
});
assert('variableValues' in result);
expect(Object.hasOwn(result.variableValues.coerced, 'input')).to.equal(
false,
);
});
});
});
5 changes: 2 additions & 3 deletions src/execution/values.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,8 @@ function coerceVariableValues(
}

const { name: varName, type: varType } = varSignature;
let value: unknown;
if (!Object.hasOwn(inputs, varName)) {
const value = Object.hasOwn(inputs, varName) ? inputs[varName] : undefined;
if (value === undefined) {
sources[varName] = { signature: varSignature };
if (varDefNode.defaultValue) {
coerced[varName] = coerceInputLiteral(varDefNode.defaultValue, varType);
Expand All @@ -124,7 +124,6 @@ function coerceVariableValues(
continue;
}
} else {
value = inputs[varName];
sources[varName] = { signature: varSignature, value };
}

Expand Down
Loading