strawberry
strawberry copied to clipboard
Expose `dataclasses.asdict` to `strawberry.asdict` and document it
A common use case is to convert types to dicts, which we already support through dataclasses.asdict.
We should expose asdict via the strawberry namespace, so we can make sure there's always a supported way to convert types to dictionaries. And we can also add additional code checks if needed :)
Example here: https://play.strawberry.rocks/?gist=c8e729c52efef8b75f1599c759c069c8
- should the
asdictresolve the child types as dict as well? - what would be the API?
@strawberry.type
class Foo:
bar: str
d = Foo("baz").asdict()
vs
d = asdict(Foo("baz"))
vs
d = dict(Foo("baz"))
- Should we do type checks while casting to dict?
- what should we do with fields with resolvers?
This seems helpful and would like to know if it would support resolvers. Here is a simple example:
from dataclasses import asdict
import strawberry
@strawberry.type
class Position:
trading_pl: float
position_pl: float
@strawberry.field
def desk_pl(self) -> float:
return self.trading_pl + self.position_pl
dict_data = asdict(Position(1, 2))
In the above example, dict_data["desk_pl"] will be a method and you have to call it to get the result, e.g.:
dict_data["desk_pl"] = dict_data["desk_pl"]()
The same issue exists with dataclasses support in Pandas. The same behavior occurs with pd.DataFrame([Position(1,2)]).
I can see it may be challenging to implement built-in behavior to support all scenarios.
In my examples, I would to avoid hardcoding calls to the methods generated from resolvers. Does Strawberry expose a public API to get the list of resolvers for a given type?
@bperkins24 it's not really possible to support resolvers in any kind of strawberry type serialisation because resolvers have to be called in the context of a GraphQL execution since they can rely on the info object: https://strawberry.rocks/docs/types/resolvers#accessing-execution-information
When I using dataclasses.asdict(...) PyCharm or other lint always said "asdict() should be called on dataclass instances". So I really agreed with @patrick91's feature request to expose strawberry.asdict(...).
I want to try to solve this issue. I'm newcomer of strawberry's internal. But... Can I try this?
@Hazealign sure! I think we can start by exposing a function called asdict that calls dataclasses.asdict, then we can improve on it later :)
def asdict(o: object) -> dict:
return dataclasses.asdict(o)
something like this :)