Strawberry GraphQL @ 0.332.0

Released

This release adds a check for the fields of OneOf input types, when the schema is built. GraphQL requires them to be nullable and without a default value, as clients set exactly one of them, but Strawberry didn’t check it:

import strawberry
@strawberry.input(one_of=True)
class SearchBy:
name: str | None = None
email: str | None = None

The schema printed name: String = null and email: String = null , so GraphQL filled in the other field and every request failed with OneOf Input Object 'SearchBy' must specify exactly one key. A required field, like name: str , made the input only work with that field. The printed schema was also invalid for other GraphQL tools.

Strawberry now raises an InvalidOneOfInputFieldError when the schema is built instead. Declare the fields as nullable and without a default, and Strawberry sets the ones that the client didn’t set to None :

@strawberry.input(one_of=True)
class SearchBy:
name: str | None
email: str | None

Default values of OneOf input types now leave out the fields set to None , so by: SearchBy = SearchBy(name="Patrick", email=None) prints as { name: "Patrick" } instead of { name: "Patrick", email: null } , which isn’t a valid OneOf value.

The OneOf docs now use this style instead of strawberry.Maybe , which still works: OneOf fields can’t be set to null , so Maybe doesn’t add anything there.

This release was contributed by @patrick91 in https://github.com/strawberry-graphql/strawberry/pull/4676