-
-
Save NetOpWibby/cc15ceac6fbcab287e32824f20b1cf63 to your computer and use it in GitHub Desktop.
GraphQL Pagination Implementation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment