Strapi GraphQL - How to create pagination?

Viewed 1702

I'm using Strapi v.3.1.3 and am trying to put together a pagination system for a blog.

Currently on load of a blog post, I run a query that finds by slug:

_slug.vue:

apollo: {
      post: {
        prefetch: false,
        query: postQuery,
        variables() {
          return {
            slug: this.$route.params.slug,
          }
        }
      }
    },

post.gql:

query Post($slug: String!) {
  post: postBySlug(slug: $slug) {
    id
    title
    content
    slug
    updatedAt
    createdAt
    published_at
  }
}

./api/post/config/schema.graphql.js:

const { sanitizeEntity } = require('strapi-utils');

module.exports = {
  query: `
    postBySlug(id: ID slug: String): Post!
  `,
  resolver: {
    Query: {
      postBySlug: {
        resolverOf: 'Post.findOne',
        async resolver(_, { slug }) {
          const entity = await strapi.services.post.findOne({ slug });
          return sanitizeEntity(entity, { model: strapi.models.post });
        }
      }
    }
  }
}

This works fine for loading a single post by slug, however now I'm not sure of the approach I should take in figuring out previous/next posts. I'm not even entirely sure that Strapi GraphQL can handle something like this at the moment.

I could query all Posts because that allows me to use limit and start, but then how would I know which Post to find on page load if I'm using a slug instead of ID?

0 Answers
Related