Skip to content

Remove oneof validation from values of correct type #4453

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
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
16 changes: 0 additions & 16 deletions src/validation/__tests__/ValuesOfCorrectTypeRule-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1094,22 +1094,6 @@ describe('Validate: Values of correct type', () => {
]);
});

it('Exactly one nullable variable', () => {
expectErrors(`
query ($string: String) {
complicatedArgs {
oneOfArgField(oneOfArg: { stringField: $string })
}
}
`).toDeepEqual([
{
message:
'Variable "string" must be non-nullable to be used for OneOf Input Object "OneOfInput".',
locations: [{ line: 4, column: 37 }],
},
]);
});

it('More than one field', () => {
expectErrors(`
{
Expand Down
10 changes: 10 additions & 0 deletions src/validation/__tests__/VariablesInAllowedPositionRule-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,16 @@ describe('Validates OneOf Input Objects', () => {
`);
});

it('Undefined variable in oneOf input object', () => {
expectErrors(`
{
complicatedArgs {
oneOfArgField(oneOfArg: { stringField: $undefinedVariable })
}
}
`).toDeepEqual([]);
});

it('Forbids one nullable variable', () => {
expectErrors(`
query ($string: String) {
Expand Down
26 changes: 1 addition & 25 deletions src/validation/rules/ValuesOfCorrectTypeRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,13 +82,7 @@ export function ValuesOfCorrectTypeRule(
}

if (type.isOneOf) {
validateOneOfInputObject(
context,
node,
type,
fieldNodeMap,
variableDefinitions,
);
validateOneOfInputObject(context, node, type, fieldNodeMap);
}
},
ObjectField(node) {
Expand Down Expand Up @@ -185,7 +179,6 @@ function validateOneOfInputObject(
node: ObjectValueNode,
type: GraphQLInputObjectType,
fieldNodeMap: ObjMap<ObjectFieldNode>,
variableDefinitions: { [key: string]: VariableDefinitionNode },
): void {
const keys = Object.keys(fieldNodeMap);
const isNotExactlyOneField = keys.length !== 1;
Expand All @@ -202,29 +195,12 @@ function validateOneOfInputObject(

const value = fieldNodeMap[keys[0]]?.value;
const isNullLiteral = !value || value.kind === Kind.NULL;
const isVariable = value?.kind === Kind.VARIABLE;

if (isNullLiteral) {
context.reportError(
new GraphQLError(`Field "${type.name}.${keys[0]}" must be non-null.`, {
nodes: [node],
}),
);
return;
}

if (isVariable) {
const variableName = value.name.value;
const definition = variableDefinitions[variableName];
const isNullableVariable = definition.type.kind !== Kind.NON_NULL_TYPE;

if (isNullableVariable) {
context.reportError(
new GraphQLError(
`Variable "${variableName}" must be non-nullable to be used for OneOf Input Object "${type.name}".`,
{ nodes: [node] },
),
);
}
}
}
Loading