Skip to main content

Paging Type Helper

Types::PagingTypeHelper.paging_type_for_model builds a GraphQL paging type β€” a list page of records plus pagination metadata β€” without writing a type class. It ships with the cm-graphql companion library.

Quick example​

type Types::PagingTypeHelper.paging_type_for_model(model: Course), null: true

Options​

ArgumentDescription
modelThe ActiveRecord model.
record_data_typeThe GraphQL type for each record. Defaults to Types::Objects::<ModelName>Type.
graphql_type_nameOptional type name. Defaults to <ModelName>PagingType.

Response shape​

The generated type has two fields: data (the page of records) and paging (metadata):

FieldTypeDescription
data[Type]The records on this page.
paging.currentPageIntegerThe page that was returned.
paging.totalCountIntegerTotal records across all pages.
paging.totalPagesIntegerTotal number of pages.
{
courses(pagination: { pageNo: 1, perPage: 20 }) {
data {
id
name
}
paging {
currentPage
totalCount
totalPages
}
}
}
Types::PagingTypeHelper.paging_type_for_model(
model: Course,
record_data_type: Types::Objects::Course,
graphql_type_name: "CustomCoursePagingType"
)

Recipes​

Reuse the type as a constant​

Generate it once in an initializer:

config/initializers/graphql_paging_type_autogen.rb
Rails.application.config.to_prepare do
Types::CoursePagingType = Types::PagingTypeHelper.paging_type_for_model(
model: Course,
record_data_type: Types::Objects::Course
)
end