diff --git a/README.md b/README.md index 418cc62..3188a5c 100644 --- a/README.md +++ b/README.md @@ -45,8 +45,12 @@ Helper functions are provided for both building the GraphQL types for connections and for implementing the `resolve` method for fields returning those types. - - `connectionArgs` returns the arguments that fields should provide when -they return a connection type. + - `connectionArgs` returns the arguments that fields should provide when they +return a connection type that supports bidirectional pagination. + - `forwardConnectionArgs` returns the arguments that fields should provide +when they return a connection type that only supports forward pagination. + - `backwardConnectionArgs` returns the arguments that fields should provide +when they return a connection type that only supports backward pagination. - `connectionDefinitions` returns a `connectionType` and its associated `edgeType`, given a name and a node type. - `connectionFromArray` is a helper method that takes an array and the diff --git a/src/connection/__tests__/connection.js b/src/connection/__tests__/connection.js index 28028d3..9f55ae4 100644 --- a/src/connection/__tests__/connection.js +++ b/src/connection/__tests__/connection.js @@ -12,16 +12,18 @@ import { GraphQLObjectType, GraphQLSchema, GraphQLString, - graphql + graphql, } from 'graphql'; import { - connectionFromArray + connectionFromArray, } from '../arrayconnection.js'; import { + backwardConnectionArgs, connectionArgs, - connectionDefinitions + connectionDefinitions, + forwardConnectionArgs, } from '../connection.js'; import { expect } from 'chai'; @@ -46,6 +48,16 @@ var userType = new GraphQLObjectType({ args: connectionArgs, resolve: (user, args) => connectionFromArray(allUsers, args), }, + friendsForward: { + type: friendConnection, + args: forwardConnectionArgs, + resolve: (user, args) => connectionFromArray(allUsers, args), + }, + friendsBackward: { + type: friendConnection, + args: backwardConnectionArgs, + resolve: (user, args) => connectionFromArray(allUsers, args), + }, }), }); @@ -121,4 +133,76 @@ describe('connectionDefinition()', () => { var result = await graphql(schema, query); expect(result).to.deep.equal({ data: expected }); }); + + it('works with forwardConnectionArgs', async () => { + var query = ` + query FriendsQuery { + user { + friendsForward(first: 2) { + edges { + node { + name + } + } + } + } + } + `; + var expected = { + user: { + friendsForward: { + edges: [ + { + node: { + name: 'Dan' + } + }, + { + node: { + name: 'Nick' + } + }, + ] + } + } + }; + var result = await graphql(schema, query); + expect(result).to.deep.equal({ data: expected }); + }); + + it('works with backwardConnectionArgs', async () => { + var query = ` + query FriendsQuery { + user { + friendsBackward(last: 2) { + edges { + node { + name + } + } + } + } + } + `; + var expected = { + user: { + friendsBackward: { + edges: [ + { + node: { + name: 'Joe' + } + }, + { + node: { + name: 'Tim' + } + }, + ] + } + } + }; + var result = await graphql(schema, query); + expect(result).to.deep.equal({ data: expected }); + }); }); diff --git a/src/connection/connection.js b/src/connection/connection.js index 9f1883f..6ce773b 100644 --- a/src/connection/connection.js +++ b/src/connection/connection.js @@ -23,24 +23,40 @@ import type { } from 'graphql'; /** - * Returns a GraphQLFieldConfigArgumentMap appropriate to include - * on a field whose return type is a connection type. + * Returns a GraphQLFieldConfigArgumentMap appropriate to include on a field + * whose return type is a connection type with forward pagination. */ -export var connectionArgs: GraphQLFieldConfigArgumentMap = { - before: { - type: GraphQLString - }, +export var forwardConnectionArgs: GraphQLFieldConfigArgumentMap = { after: { type: GraphQLString }, first: { type: GraphQLInt }, +}; + +/** + * Returns a GraphQLFieldConfigArgumentMap appropriate to include on a field + * whose return type is a connection type with backward pagination. + */ +export var backwardConnectionArgs: GraphQLFieldConfigArgumentMap = { + before: { + type: GraphQLString + }, last: { type: GraphQLInt }, }; +/** + * Returns a GraphQLFieldConfigArgumentMap appropriate to include on a field + * whose return type is a connection type with bidirectional pagination. + */ +export var connectionArgs: GraphQLFieldConfigArgumentMap = { + ...forwardConnectionArgs, + ...backwardConnectionArgs, +}; + type ConnectionConfig = { name: string, nodeType: GraphQLObjectType,