Convert to Dictionary
Strawberry provides a utility function to convert a Strawberry object to a dictionary.
You can use strawberry.asdict(...) function:
@strawberry.typeclass User: name: str age: int
# should be {"name": "Lorem", "age": 25}user_dict = strawberry.asdict(User(name="Lorem", age=25)) Handling of Maybe and UNSET values
When using Maybe fields, strawberry.asdict unwraps the
value of that field, if present.
- A
Maybefield that is absent is excluded from output. - A field that returns
Some(None), an explicit null, is included with the valueNone. - A field that returns
UNSETis excluded from output.
import strawberryfrom strawberry import UNSET, Maybe, Some, asdict
@strawberry.inputclass Input: field_a: Maybe[str | None] field_b: Maybe[str | None] field_c: Maybe[str | None] field_d: Maybe[str | None]
# should be {"field_a": "hello", field_b=None}# `field_c` is excluded because it is an absent `Maybe` instead of an explicit null;# `field_d` is excluded because it is UNSETinput_dict = asdict( Input( field_a=Some("hello"), field_b=Some(None), field_c=None, field_d=UNSET, ))