graphql-compose-pagination
Version:
Plugin for `graphql-compose` which provide a pagination resolver for types.
62 lines (57 loc) • 1.93 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.preparePaginationTC = exports.preparePaginationInfoTC = void 0;
const graphql_compose_1 = require("graphql-compose");
const PaginationInfoTC = graphql_compose_1.ObjectTypeComposer.createTemp(`
# Information about pagination.
type PaginationInfo {
# Current page number
currentPage: Int!
# Number of items per page
perPage: Int!
# Total number of pages
pageCount: Int
# Total number of items
itemCount: Int
# When paginating forwards, are there more items?
hasNextPage: Boolean
# When paginating backwards, are there more items?
hasPreviousPage: Boolean
}
`);
function preparePaginationInfoTC(sc) {
if (sc.has('PaginationInfo')) {
return sc.getOTC('PaginationInfo');
}
sc.set('PaginationInfo', PaginationInfoTC);
return PaginationInfoTC;
}
exports.preparePaginationInfoTC = preparePaginationInfoTC;
function preparePaginationTC(tc, resolverName) {
const schemaComposer = tc.schemaComposer;
const name = `${tc.getTypeName()}${graphql_compose_1.upperFirst(resolverName || 'pagination')}`;
if (schemaComposer.has(name)) {
return schemaComposer.getOTC(name);
}
const paginationTC = schemaComposer.createObjectTC({
name,
description: 'List of items with pagination.',
fields: {
count: {
type: 'Int',
description: 'Total object count.',
},
items: {
type: () => tc.NonNull.List,
description: 'Array of objects.',
},
pageInfo: {
type: preparePaginationInfoTC(schemaComposer).NonNull,
description: 'Information to aid in pagination.',
},
},
});
return paginationTC;
}
exports.preparePaginationTC = preparePaginationTC;
//# sourceMappingURL=types.js.map