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β
| Argument | Description |
|---|---|
model | The ActiveRecord model. |
record_data_type | The GraphQL type for each record. Defaults to Types::Objects::<ModelName>Type. |
graphql_type_name | Optional type name. Defaults to <ModelName>PagingType. |
Response shapeβ
The generated type has two fields: data (the page of records) and paging (metadata):
| Field | Type | Description |
|---|---|---|
data | [Type] | The records on this page. |
paging.currentPage | Integer | The page that was returned. |
paging.totalCount | Integer | Total records across all pages. |
paging.totalPages | Integer | Total 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