2.0.1 • Published 6 years ago

graphql-connection v2.0.1

Weekly downloads
207
License
ISC
Repository
github
Last release
6 years ago

graphql-connection

GraphQL Relay Connection helper library

GraphQL forward pagination arguments are equivalent to backwards pagination arguments. I.e. you can rewrite any backwards pagination arguments as forward paginatino arguments and still get the same result back. This is useful because you'll only need to support forward pagination on your backend, and bidirectional pagination will still work like intended. The function transformToForward makes this possible.

Read more about connections.

Install

$ yarn add graphql-connection

Usage

Example:

import {
  globalIdField,
  connectionDefinitions,
  getOffsetWithDefault,
  connectionArgs,
} from 'graphql-relay';
import {
  connectionFromArray,
  transformToForward,
} from 'graphql-connection';

...

export const groupType = new GraphQLObjectType({
  name: 'Group',
  fields: () => ({
    id: globalIdField(),
    users: {
      type: userConnection,
      args: {
        ...connectionArgs,
      },
      resolve: async (obj, args) => {
        const { first, after } = transformToForward(args);
        const array = await db.getUsers({
          limit: first + 1,
          offset: getOffsetWithDefault(after, -1) + 1,
        });

        // Don't forget to pass the arguments returned from "transformToForward"
        return connectionFromArray(array, { first, after });
      },
    },
  }),
});

API

connectionFromArray(array, args)

Returns a connection from given array and forward pagination arguments.

array
Type: Array

args
Type: Object
Property: first, after
Note: Accepts only forward pagination arguments.

first
Type: Number

after
Type: String

transformToForward

A function that takes forward- and backward pagination arguments and returns only forward pagination arguments. Prioritizes forward pagination arguments.
Returns -1 as a cursor for after if it should not be in argument.

args
Type: Object
Property: first, after, last, before

first
Type: Number

after
Type: String

last
Type: Number

before
Type: String

Related

2.0.1

6 years ago

2.0.0

6 years ago

1.1.0

6 years ago

1.0.5

6 years ago

1.0.4

6 years ago

1.0.3

6 years ago

1.0.2

6 years ago

1.0.1

6 years ago

1.0.0

6 years ago