Skip to content

Ensure we validate for using nullable variables in oneOf input fields #4363

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 27, 2025
Merged
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
2 changes: 2 additions & 0 deletions src/validation/ValidationContext.ts
Original file line number Diff line number Diff line change
@@ -33,6 +33,7 @@ interface VariableUsage {
readonly node: VariableNode;
readonly type: Maybe<GraphQLInputType>;
readonly defaultValue: Maybe<unknown>;
readonly parentType: Maybe<GraphQLInputType>;
}

/**
@@ -209,6 +210,7 @@ export class ValidationContext extends ASTValidationContext {
node: variable,
type: typeInfo.getInputType(),
defaultValue: typeInfo.getDefaultValue(),
parentType: typeInfo.getParentInputType(),
});
},
}),
31 changes: 31 additions & 0 deletions src/validation/__tests__/VariablesInAllowedPositionRule-test.ts
Original file line number Diff line number Diff line change
@@ -358,3 +358,34 @@ describe('Validate: Variables are in allowed positions', () => {
});
});
});

describe('Validates OneOf Input Objects', () => {
it('Allows exactly one non-nullable variable', () => {
expectValid(`
query ($string: String!) {
complicatedArgs {
oneOfArgField(oneOfArg: { stringField: $string })
}
}
`);
});

it('Forbids one nullable variable', () => {
expectErrors(`
query ($string: String) {
complicatedArgs {
oneOfArgField(oneOfArg: { stringField: $string })
}
}
`).toDeepEqual([
{
message:
'Variable "$string" is of type "String" but must be non-nullable to be used for OneOf Input Object "OneOfInput".',
locations: [
{ line: 2, column: 14 },
{ line: 4, column: 50 },
],
},
]);
});
});
21 changes: 19 additions & 2 deletions src/validation/rules/VariablesInAllowedPositionRule.ts
Original file line number Diff line number Diff line change
@@ -8,7 +8,11 @@ import { Kind } from '../../language/kinds';
import type { ASTVisitor } from '../../language/visitor';

import type { GraphQLType } from '../../type/definition';
import { isNonNullType } from '../../type/definition';
import {
isInputObjectType,
isNonNullType,
isNullableType,
} from '../../type/definition';
import type { GraphQLSchema } from '../../type/schema';

import { isTypeSubTypeOf } from '../../utilities/typeComparators';
@@ -36,7 +40,7 @@ export function VariablesInAllowedPositionRule(
leave(operation) {
const usages = context.getRecursiveVariableUsages(operation);

for (const { node, type, defaultValue } of usages) {
for (const { node, type, defaultValue, parentType } of usages) {
const varName = node.name.value;
const varDef = varDefMap[varName];
if (varDef && type) {
@@ -66,6 +70,19 @@ export function VariablesInAllowedPositionRule(
),
);
}

if (
isInputObjectType(parentType) &&
parentType.isOneOf &&
isNullableType(varType)
) {
context.reportError(
new GraphQLError(
`Variable "$${varName}" is of type "${varType}" but must be non-nullable to be used for OneOf Input Object "${parentType}".`,
{ nodes: [varDef, node] },
),
);
}
}
}
},