Best Practices for custom GraphQL resolvers

More of a How-To, than a best practice, but since Pagination with GraphQL resolvers is a bit confusing, here are some things on that:

Using Pagination Arguments in a UDF

Docs can be helpful, even if they get a bad reputation! They have a good example of how to use pagination arguments in a UDF.

I created a helper function that builds on that example:

const paginateWithOptions = (set, size, after, before) =>
  q.If(
    q.Equals(before, null),
    q.If(
      q.Equals(after, null),
      q.Paginate(set, { size: size }),
      q.Paginate(set, { size: size, after: after })
    ),
    q.Paginate(set, { size: size, before: before })
  )

Use like this:

{
  name: "paginatedResolver",
  body: q.Query(
    q.Lambda(
      ['input', 'size', 'after', 'before'],
      q.Map(
        paginateWithOptions(
          q.Call(q.Function('getSomeSet'), q.Var('input')),
          q.Var('size'),
          q.Var('after'),
          q.Var('before')
        ),
        q.Lambda('ref', q.Get(q.Var('ref')))
      )
    )
  )
})

Where getSomeSet UDF returns a Set, for example from Match(Index(...))

Other Forum references

4 Likes