@graphql-pagination/apollo-datasource
v1.2.5
Published
GraphQL Pagination - Apollo Datasource integration
Downloads
8
Maintainers
Readme
GraphQL Pagination / Apollo Datasource integration
Module integrates GraphQL Pagination and Apollo Datasources.
Usage
// BookConnection is generated by DataSourcePager
const typeDefs = gql`
type Book {
id: ID!
title: String
author: String
published: DateTime
}
type Query {
booksAsc(first: Int = 10 after: String): BookConnection
booksDesc(last: Int = 10 before: String): BookConnection
}
`;
const resolvers = {
Query: {
booksAsc: (_, args, { dataSources }) => dataSources.pager.forwardResolver(args),
booksDesc: (_, args, { dataSources }) => dataSources.pager.backwardResolver(args),
},
};
const apolloBooksPager = new ApolloDataSourcePager({
dataSource: new ArrayDataSource(books, "id"),
typeName: "Book",
});
const createApolloServer = () => {
return new ApolloServer({
typeDefs: [
typeDefs,
apolloBooksPager.typeDefs, // BookConnection, BookEdge, PageInfo typeDefs
scalarTypeDefs, // for DateTime
],
resolvers: [
resolvers,
scalarResolvers, // for DateTime
],
dataSources: () => {
return {
pager: apolloBooksPager,
};
},
});
};
See fully working example in examples/apollo-datasource.