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
32 changes: 32 additions & 0 deletions src/execution/__tests__/variables-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -404,6 +404,38 @@ describe('Execute: Handles inputs', () => {
});
});

it('preserves explicit null variables within input object literals', () => {
const result = executeQuery(
`
query q($input: String) {
fieldWithObjectInput(input: { a: $input, c: "baz" })
}`,
{ input: null },
);

expect(result).to.deep.equal({
data: {
fieldWithObjectInput: '{ a: null, c: "baz" }',
},
});
});

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 default value when not provided', () => {
const result = executeQuery(`
query ($input: TestInputObject = {a: "foo", b: ["bar"], c: "baz"}) {
Expand Down
10 changes: 10 additions & 0 deletions src/utilities/__tests__/coerceInputValue-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -640,6 +640,16 @@ describe('coerceInputLiteral', () => {
{ int: 42, requiredBool: true },
);
});

it('preserves explicit null variables in input object fields', () => {
testWithVariables(
'($foo: Boolean)',
{ foo: null },
'{ int: $foo, requiredBool: true }',
testInputObj,
{ int: null, requiredBool: true },
);
});
});

describe('coerceDefaultValue', () => {
Expand Down
17 changes: 15 additions & 2 deletions src/utilities/coerceInputValue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -229,11 +229,11 @@ export function coerceInputLiteral(
if (
!fieldNode ||
(fieldNode.value.kind === Kind.VARIABLE &&
getCoercedVariableValue(
isMissingVariable(
fieldNode.value,
variableValues,
fragmentVariableValues,
) == null)
))
) {
if (isRequiredInputField(field)) {
return; // Invalid: intentionally return no value.
Expand Down Expand Up @@ -296,6 +296,19 @@ function getCoercedVariableValue(
return variableValues?.coerced[varName];
}

function isMissingVariable(
variableNode: VariableNode,
variableValues: Maybe<VariableValues>,
fragmentVariableValues: Maybe<FragmentVariableValues>,
): boolean {
const varName = variableNode.name.value;
const scopedValues =
fragmentVariableValues?.sources[varName] !== undefined
? fragmentVariableValues.coerced
: variableValues?.coerced;
return scopedValues?.[varName] === undefined;
}

interface InputValue {
type: GraphQLInputType;
default?: GraphQLDefaultInput | undefined;
Expand Down
Loading