docs/guides/convert-to-dictionary.md
Strawberry provides a utility function to convert a Strawberry object to a dictionary.
You can use strawberry.asdict(...) function:
@strawberry.type
class User:
name: str
age: int
# should be {"name": "Lorem", "age": 25}
user_dict = strawberry.asdict(User(name="Lorem", age=25))
Maybe and UNSET valuesWhen using Maybe fields, strawberry.asdict unwraps the
value of that field, if present.
Maybe field that is absent is excluded from output.Some(None), an explicit null, is included with the
value None.UNSET is excluded from output.import strawberry
from strawberry import UNSET, Maybe, Some, asdict
@strawberry.input
class 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 UNSET
input_dict = asdict(
Input(
field_a=Some("hello"),
field_b=Some(None),
field_c=None,
field_d=UNSET,
)
)