Strawberry GraphQL @ 0.266.1
Released
This release adds a new (preferable) way to handle optional updates. Up until
now when you wanted to inffer if an input value was null or absent you’d use
strawberry.UNSET
which is a bit cumbersome and error prone.
Now you can use strawberry.Maybe
to identify if a
value was provided or not.
e.g.
import strawberry
@strawberry.type
class User:
name: str
phone: str | None
@strawberry.input
class UpdateUserInput:
name: str
phone: strawberry.Maybe[str]
@strawberry.type
class Mutation:
def update_user(self, input: UpdateUserInput) -> None:
reveal_type(input.phone) # strawberry.Some[str | None] | None
if input.phone:
reveal_type(input.phone.value) # str | None
update_user_phone(input.phone.value)
Or, if you can use pattern matching:
@strawberry.type
class Mutation:
def update_user(self, input: UpdateUserInput) -> None:
match input.phone:
case strawberry.Some(value=value):
update_user_phone(input.phone.value)
You can also use strawberry.Maybe
as a field argument like so
import strawberry
@strawberry.field
def filter_users(self, phone: strawberry.Maybe[str] = None) -> list[User]:
if phone:
return filter_users_by_phone(phone.value)
return get_all_users()