npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@graphql-pagination/core

v1.12.0

Published

GraphQL Pagination Core module

Downloads

478

Readme

GraphQL Pagination - Core

Core module of GraphQL Pagination providing spec and ready to use implementations.

  1. CursorPager specification
  2. DataSource specification
  3. dataSourcePager implementation backed by DataSource
  4. dataloaderPagerWrapper pagination wrapper using dataloader
  5. ArrayDataSource implementation as fixed array of data
  6. OffsetDataSourceWrapper Offset pagination wrapper
  7. GraphQL Type Defs

Check additional modules:

  1. @graphql-pagination/sql-knex - SQL (Knex.js) DataSource

DataSourcePager

DataSource Pager implements CursorPager backed by a DataSource. It's up to you to either use built-in ArrayDataSource or implement your own.

Configuration:

  1. dataSource (optional) - pass your datasource at pager creation or pass on resolver level via forwardResolver or backwardResolver.
  2. typeName (optional) - name to generate GraphQL Pagination type defs like BookConnection, BookEdge.
  3. cursor (optional) - custom implementation how to encode/decode cursor
  4. validateForwardArgs (optional) - function (or array) to validate input args used by forward resolver
  5. validateBackwardArgs (optional) - function (or array) to validate input args used by backward resolver
  6. fetchTotalCountInResolver (optional) - if false then totalCount is not fetched as part of forward/backward resolvers but totalCount resolver in Connection object needs to be defined separately. Pager provides .resolvers field for it.
  7. typeDefDirectives (optional) - directives added to generated type definitions.

See more details in DataSourcePager.ts.

Basic Example

const { ArrayDataSource, DataSourcePager, dataSourcePager } = require("@graphql-pagination/core");

// Create Array Data Source from array of books
const ds = new ArrayDataSource(books);

const pager = dataSourcePager({
    dataSource: ds,
    typeName: "Book",
});

// BookConnection is generated by DataSourcePager
const typeDefs = gql`
    type Book {
        id: ID!
        title: String
        author: String
    }
    type Query {
        booksAsc(first: Int = 10 after: String): BookConnection
        booksDesc(last: Int = 10 before: String): BookConnection
    }
`;

const resolvers = {
    Query: {
        booksAsc: (_, args) => pager.forwardResolver(args),
        booksDesc: (_, args) => pager.backwardResolver(args),
    },
};

Advanced Example - DataLoader Wrapper Pager & Context

const { ArrayDataSource, DataSourcePager, dataSourcePager } = require("@graphql-pagination/core");

// Create Array Data Source from array of books
const dataSource = new ArrayDataSource(books);

// BookConnection is generated by DataSourcePager
const typeDefs = gql`
    type Book {
        id: ID!
        title: String
        author: String
    }
    type Query {
        booksAsc(first: Int = 10 after: String): BookConnection
        booksDesc(last: Int = 10 before: String): BookConnection
    }
`;

const resolvers = {
    Query: {
        booksAsc: (_, args, ctx) => ctx.pager.forwardResolver(args),
        booksDesc: (_, args, ctx) => ctx.pager.backwardResolver(args),
    },
};

// https://www.apollographql.com/docs/apollo-server/api/standalone/#example
const server = new ApolloServer({ typeDefs, resolvers });
const standAloneServer = await startStandaloneServer(server, {
    context: async () => ({ pager: dataSourceLoaderPager({ dataSource }) }),    // create new pager with dataloader for every request
    listen: { port: 4000 },
});
console.log(`🚀  Server ready at ${url}`);

Offset Data Source Paging

If your DS / API provides offset pagination resp. slicing (start + size) and you want to use this pagination then it's supported as wrapper.

You need to create your DS like any other but expect that Wrapper will store in encoded cursor the index value and not any field from your data. Then afterId / beforeId values in your DS will be index (start) value.

Example

const { ArrayDataSource, dataSourcePager, OffsetDataSourceWrapper } = require("@graphql-pagination/core");

class ArrayOffsetDs extends ArrayDataSource {

   async after(offset, size, args) {
      // No field data comparison involved. It's just offset slicing
      return this.getNodes(args).then(data => data.slice(offset, offset + size));
   }

}

const dsOffset = new ArrayOffsetDs(books, "_NOT_USED_");
const pagerOffset = dataSourcePager({
   dataSource: new OffsetDataSourceWrapper(dsOffset),
   typeName: "Book",
});

Complete Example

See fully working examples/in-memory.

The complete example includes:

  1. Input validation
  2. Extra input args for data filtering
  3. DataSource using Date type
  4. OffsetDataSourceWrapper
  5. Custom directives used in Type Objects