Skip to content

Manually resolve type from root #2366

Answered by jkimbo
Object905 asked this question in Q&A
Discussion options

You must be logged in to vote

@Object905 there isn't a way of automatically converting ORM models to Strawberry types (unless you use strawberry-django). The pattern I usually follow is to define a from_instance class method on the type where the conversion happens. Taking your example:

@federation.type(keys=["id"])
class Provider:
    id: strawberry.ID
    name: str
    slug: str
    consumed_by: "Provider | None"

    @classmethod
    def from_instance(cls, instance: models.Provider):
        return cls(
            id=obj.pk,
            name=obj.name,
            slug=obj.slug,
            consumed_by=obj.consumed_by,
        )

    @classmethod
    def resolve_reference(cls, info: Info, **kwargs):
        obj: mo…

Replies: 2 comments 1 reply

Comment options

You must be logged in to vote
0 replies
Answer selected by Object905
Comment options

You must be logged in to vote
1 reply
@jkimbo
Comment options

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants