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

/**
Expand Down Expand Up @@ -209,6 +210,7 @@ export class ValidationContext extends ASTValidationContext {
node: variable,
type: typeInfo.getInputType(),
defaultValue: typeInfo.getDefaultValue(),
parentType: typeInfo.getParentInputType(),
});
},
}),
Expand Down
31 changes: 31 additions & 0 deletions src/validation/__tests__/VariablesInAllowedPositionRule-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Up @@ -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';
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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] },
),
);
}
}
}
},
Expand Down