How to implement service pagination with typescript?

SDL:

type LogPage {
  logs: [Log!]!
  count: Int!
}

type Query {
  logPage(page: Int, rowsPerPage: Int): LogPage @requireAuth
}

Service:


export const logPage: QueryResolvers['logPage'] = ({
  page = 0,
  rowsPerPage = 100,
}) => {
  return {
    logs: db.log.findMany({
      take: rowsPerPage,
      skip: rowsPerPage * page,
    }),
    count: db.log.count(),
  }
}

Hi. I was following pagination how-to but ts doesn’t like my implementation.

I’m getting an error for count property of the returned object Type 'PrismaPromise<number>' is not assignable to type 'number'.

I worked around it with

export const logPage: QueryResolvers['logPage'] = ({
  page = 0,
  rowsPerPage = 100,
}) => {
  return db
    .$transaction([
      db.log.findMany({
        take: rowsPerPage,
        skip: rowsPerPage * page,
      }),
      db.log.count(),
    ])
    .then(([logs, count]) => ({ logs, count }))
}

but it feels like a hack because services await db response for me.

Is there a better way?

While Redwood does await db responses for some simple services, it’s perfectly fine to make them async and call await explicitly.

In fact, I think it feels clearer what is happening and I often change the generated services.