Home
Blog
Technical Insights Into Pagination With GraphQL And Ruby on Rails

Technical Insights Into Pagination With GraphQL And Ruby on Rails

・3 min read
Technical Insights Into Pagination With GraphQL And Ruby on Rails

You may also like:

Design SaaS Product: 4 Reasons Why You Should Choose Ruby On Rails Framework

Design SaaS Product: 4 Reasons Why You Should Choose Ruby On Rails Framework

Read more

In the last episode, we divided queries into separate files. You can find all the necessary information in this article. Today we will add a pagination endpoint and explain how to use it.

Adding GraphQL pagination might be tricky because there are quite a lot of different implementations. In this tutorial, we are going to use the one that is suggested by graphql-ruby.org. We will be preparing a separate query file so make sure that you are familiar with this concept first.

Adding pagination

Adding pagination is really simple. All you have to do is add this file app/graphql/queries/books_connection.rb

and of course edit app/graphql/types/query_type.rb with:

Please note that Types::BookType looks like:

If you wonder about Types::AuthorType and Enums::Genre, please visit the first article from this series.

As you can see implementation is trivial but we have to dive into some theory and naming convention in order to understand how to use this pagination correctly.

Left map imageRight map image
Looking to hire Ruby on Rails experts? Contact us for a quote.
Contact Us

Connections, edges and nodes

  • Connection - it exposes pagination-related metadata and access to the nodes and edges. In our example we have created BookConnection. Connections are objects that represent a one-to-many relation.
  • Edge - represents the relationship between the parent and child. In our example, BookEdge represents the link from Author to Book. Edges help when the relationship between two objects has special data associated with them. You might need a custom edge to model the relationship if you use a join table.
  • Node - actual list item. For our BookConnection, each node is a Book.

Pagination usage

We have pagination in place and we know what each of the object names stands for. Let’s try to use it! I think it is a good idea to compare pagination to regular BooksQuery execution:

Img

Simple connection usage looks like this:

Img

Left map imageRight map image
Need help with your RoR project? Schedule a consultation with an Expert.
Learn More

In order to access pagination features we have to use PageInfo. We also can receive cursor info for each node at edge level:

Img

We can use cursor to start a “new page”:

Img

Testing Pagination

Testing connection is no different than testing regular query:

Summary

I hope that you found this tutorial useful and comprehensive. Adding pagination for your query is relatively simple but if you are looking for more information please take a look at graphql-ruby.org.

Happy coding!


Rate this article:

5,0

based on 0 votes
Our services
See what we can create for You
Our services

Awards & Certificates

reviewed on
30 reviews
  • Top 1000 Companies Global 2021
  • Top Development Company Poland 2021
HR dream team
  • 2020 HR Dream Team Award
  • 2016 Employer Branding Featured
  • 2015 HR Dream Team Award
ISO CertificateISO Certificate
  • Information Security Management System compliant with PN-EN ISO/IEC 27001
  • Business Continuity Management compliant with ISO 22301