Duplicated code
Before looking at cursor pagination and relay I wanted to address one thing that I noticed in the previous examples: we have some duplicated code in our resolvers. We convert the django models into strawberry types in every resolver by doing:
Podcast(
id=db_podcast.id,
title=db_podcast.title,
description=db_podcast.description,
)
I usually move this code on a class method of the type, like this:
import strawberry
from db import models
@strawberry.type
class Podcast:
id: strawberry.ID
title: str
description: str
@classmethod
def from_db(cls, db_podcast: models.Podcast) -> "Podcast":
return cls(
id=strawberry.ID(db_podcast.id),
title=db_podcast.title,
description=db_podcast.description,
)
This way we can use it in our resolvers:
@strawberry.type
class PodcastsQuery:
# keep the previous fields
...
@strawberry.field
async def podcasts(self, page: int = 1, per_page: int = 10) -> List[Podcast]:
db_podcasts = await data.paginate_podcast(page=page, per_page=per_page)
return [Podcast.from_db(db_podcast) for db_podcast in db_podcasts]
And we'll avoid having to change the code in multiple places if we need to change the type.
To make our code easier to use, all our types in future will have this method.