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
74 changes: 74 additions & 0 deletions crates/libgraphql-core-v1/src/schema/tests/schema_builder_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -872,3 +872,77 @@ fn build_enum_with_no_values_fails() {
});
assert!(has_error, "expected EnumWithNoValues error");
}

// Verifies end-to-end (parse -> build -> validate) that a
// `@oneOf` input object with a non-nullable field and a defaulted
// field is rejected with both oneOf violations.
//
// See https://spec.graphql.org/September2025/#sec-Input-Objects.Type-Validation
//
// Written by Claude Code, reviewed by a human.
#[test]
fn build_oneof_input_object_violations_rejected() {
let result = SchemaBuilder::build_from_str(
"type Query { x: Int }\n\
input UserLookup @oneOf {\n\
byId: Int!\n\
byName: String = \"anonymous\"\n\
}",
);
assert!(result.is_err());
let errors = result.unwrap_err();
let has_non_nullable_err = errors.errors().iter().any(|e| {
if let SchemaBuildErrorKind::TypeValidation(tve) = e.kind() {
matches!(
tve.kind(),
TypeValidationErrorKind::InvalidNonNullableOneOfInputField {
field_name,
parent_type_name,
} if field_name == "byId"
&& parent_type_name == "UserLookup",
)
} else {
false
}
});
let has_default_err = errors.errors().iter().any(|e| {
if let SchemaBuildErrorKind::TypeValidation(tve) = e.kind() {
matches!(
tve.kind(),
TypeValidationErrorKind::InvalidOneOfInputFieldWithDefaultValue {
field_name,
parent_type_name,
} if field_name == "byName"
&& parent_type_name == "UserLookup",
)
} else {
false
}
});
assert!(
has_non_nullable_err,
"expected InvalidNonNullableOneOfInputField for byId",
);
assert!(
has_default_err,
"expected InvalidOneOfInputFieldWithDefaultValue for byName",
);
}

// Verifies end-to-end that a spec-conformant `@oneOf` input
// object (all fields nullable, no defaults) builds successfully.
//
// See https://spec.graphql.org/September2025/#sec-Input-Objects.Type-Validation
//
// Written by Claude Code, reviewed by a human.
#[test]
fn build_valid_oneof_input_object_succeeds() {
let schema = SchemaBuilder::build_from_str(
"type Query { x: Int }\n\
input UserLookup @oneOf {\n\
byId: Int\n\
byName: String\n\
}",
).unwrap();
assert!(schema.input_object_type("UserLookup").is_some());
}
20 changes: 20 additions & 0 deletions crates/libgraphql-core-v1/src/schema/type_validation_error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,26 @@ pub enum TypeValidationErrorKind {
type_name: String,
},

#[error(
"input field `{parent_type_name}.{field_name}` must have a \
nullable type because `{parent_type_name}` is a `@oneOf` \
input object"
)]
InvalidNonNullableOneOfInputField {
field_name: String,
parent_type_name: String,
},

#[error(
"input field `{parent_type_name}.{field_name}` must not \
declare a default value because `{parent_type_name}` is a \
`@oneOf` input object"
)]
InvalidOneOfInputFieldWithDefaultValue {
field_name: String,
parent_type_name: String,
},

#[error(
"output field `{parent_type_name}.{field_name}` has \
type `{input_type_name}` which is not an output type"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,15 @@ use indexmap::IndexMap;
use std::collections::HashSet;

/// Validates an input object type's field type references,
/// input-type legality, and circular non-nullable reference
/// chains.
/// input-type legality, circular non-nullable reference chains,
/// and `@oneOf` constraints.
///
/// Per the GraphQL spec, all input object fields must reference
/// valid input types (scalars, enums, or other input objects) and
/// input object types must not form non-nullable circular
/// references (which would make them impossible to construct).
/// Additionally, every field of a `@oneOf` input object must have
/// a nullable type and must not declare a default value.
///
/// See [Input Objects](https://spec.graphql.org/September2025/#sec-Input-Objects).
pub(crate) struct InputObjectTypeValidator<'a> {
Expand All @@ -40,6 +42,7 @@ impl<'a> InputObjectTypeValidator<'a> {
}

pub fn validate(mut self) -> Vec<TypeValidationError> {
self.validate_oneof_constraints();
let fields = self.type_.fields();
self.validate_fields_recursive(
self.type_.name(),
Expand All @@ -50,6 +53,52 @@ impl<'a> InputObjectTypeValidator<'a> {
self.errors
}

/// Enforces the `@oneOf` input object constraints: every field
/// must have a nullable type and must not declare a default
/// value.
///
/// See [Input Objects — Type Validation](https://spec.graphql.org/September2025/#sec-Input-Objects.Type-Validation).
fn validate_oneof_constraints(&mut self) {
let is_oneof = self.type_.directives().iter().any(
|annot| annot.name().as_str() == "oneOf",
);
if !is_oneof {
return;
}

for (field_name, field) in self.type_.fields() {
if !field.type_annotation().nullable() {
// https://spec.graphql.org/September2025/#sec-Input-Objects.Type-Validation
self.errors.push(TypeValidationError::new(
TypeValidationErrorKind::InvalidNonNullableOneOfInputField {
field_name: field_name.to_string(),
parent_type_name:
self.type_.name().to_string(),
},
field.type_annotation().span(),
vec![ErrorNote::spec(
"https://spec.graphql.org/September2025/#sec-Input-Objects.Type-Validation",
)],
));
}

if field.default_value().is_some() {
// https://spec.graphql.org/September2025/#sec-Input-Objects.Type-Validation
self.errors.push(TypeValidationError::new(
TypeValidationErrorKind::InvalidOneOfInputFieldWithDefaultValue {
field_name: field_name.to_string(),
parent_type_name:
self.type_.name().to_string(),
},
field.span(),
vec![ErrorNote::spec(
"https://spec.graphql.org/September2025/#sec-Input-Objects.Type-Validation",
)],
));
}
}
}

fn validate_fields_recursive(
&mut self,
type_name: &'a TypeName,
Expand Down
Loading
Loading