Pagination Page vanilla GraphQL
const PaginationArgType = new GraphQLInputObjectType({
name: 'PaginationArg',
fields: {
offset: {
type: GraphQLInt,
description: "Skip n rows."
},
first: {
type: GraphQLInt,
description: "First n rows after the offset."
},
}
})
const PaginatedListType = (ItemType) => new GraphQLObjectType({
name: 'Paginated' + ItemType,
fields: {
count: { type: GraphQLInt },
items: { type: new GraphQLList(ItemType) }
}
})
const PersonType = new GraphQLObjectType({
name: 'Person',
fields: {
id: { type: new GraphQLNonNull(GraphQLID) },
name: { type: GraphQLString },
}
})
const PersonQueryTypes = {
people: {
type: PaginatedListType(PersonType),
args: {
pagination: {
type: PaginationArgType,
defaultValue: { offset: 0, first: 10 }
},
},
resolve: (_, args) => {
const { offset, first } = args.pagination
return {
items: People.find().skip(offset).limit(first).exec()
count: People.count()
}
},
}
}
const QueryType = new GraphQLObjectType({
name: 'QueryType',
fields: {
...PersonQueryTypes,
},
});
const Schema = new GraphQLSchema({
query: QueryType
});
and upon request:
{
people(pagination: {offset: 0, first: 10}) {
items {
id
name
}
count
}
}
Have created a launchpad here .
Bless source
share