Skip to content

Instantly share code, notes, and snippets.

@mattmazzola
Last active December 6, 2018 06:17
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save mattmazzola/3809bdd77573ceca5806ea561102af48 to your computer and use it in GitHub Desktop.
Save mattmazzola/3809bdd77573ceca5806ea561102af48 to your computer and use it in GitHub Desktop.
GraphQL Pagination Implementation
var graphql = require('graphql');
export function Edge(itemType: any) {
return new graphql.GraphQLObjectType({
name: "Edge",
description: "Generic edge to allow cursors",
fields: () => ({
node: { type: itemType },
cursor: { type: graphql.GraphQLString }
})
});
}
export const PageInfo = new graphql.GraphQLObjectType({
name: "PageInfo",
description: "Information about current page",
fields: () => ({
startCursor: { type: graphql.GraphQLString },
endCursor: { type: graphql.GraphQLString },
hasNextPage: { type: graphql.GraphQLBoolean }
})
});
export function Page(itemType: any) {
return new graphql.GraphQLObjectType({
name: "Page",
description: "Page",
fields: () => ({
totalCount: { type: graphql.GraphQLInt },
edges: { type: new graphql.GraphQLList(Edge(itemType)) },
pageInfo: { type: PageInfo }
})
});
}
export function convertNodeToCursor(node: { id: number }): string {
return bota(node.id.toString());
}
export function bota(input: string): string {
return new Buffer(input.toString(), 'binary').toString("base64");
}
export function convertCursorToNodeId(cursor: string): number {
return parseInt(atob(cursor));
}
export function atob(input: string): string {
return new Buffer(input, 'base64').toString('binary');
}
@matthewlamhk01
Copy link

i guess it meant to be btoa instead of bota?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment