docs/types/unset.md
UNSET is considered legacy. For new code, we recommend using
Maybe instead, which provides better type safety and clearer
semantics for handling optional input fields.
UNSET is a sentinel value that can be used to represent an unset value in a
field or argument. Similar to undefined in JavaScript, this value can be used
to differentiate between a field that was not set and a field that was set to
None or null.
import strawberry
@strawberry.input
class UpdateUserInput:
name: str | None = strawberry.UNSET
phone: str | None = strawberry.UNSET
@strawberry.type
class Mutation:
@strawberry.mutation
def update_user(self, input: UpdateUserInput) -> User:
if input.name is not strawberry.UNSET:
user.name = input.name
if input.phone is not strawberry.UNSET:
user.phone = input.phone
return user
Use identity comparison to check if a value is unset:
# Correct way to check
if value is strawberry.UNSET:
print("Value was not provided")
# Also correct
if value is not strawberry.UNSET:
print(f"Value was provided: {value}")
The is_unset() helper function has been deprecated. Use
value is strawberry.UNSET instead.
UNSET has some limitations compared to Maybe:
UNSET doesn't work well with type checkers since it's
typed as Anyfield: str | None = UNSET, it's not
immediately clear whether None means "set to null" or "not provided"None valueWe recommend migrating to Maybe for new code. See the
migration guide for details.
| Aspect | UNSET | Maybe |
|---|---|---|
| Check absent | value is strawberry.UNSET | value is None |
| Access value | value (direct) | value.value (via Some) |
| Type annotation | T | None = UNSET | Maybe[T] or Maybe[T | None] |
| Null handling | Implicit | Explicit with T | None |