Skip to content

Commit

Permalink
Merge b5689b5 into 13e79da
Browse files Browse the repository at this point in the history
  • Loading branch information
taion committed Oct 24, 2015
2 parents 13e79da + b5689b5 commit 29440e5
Show file tree
Hide file tree
Showing 3 changed files with 115 additions and 11 deletions.
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
90 changes: 87 additions & 3 deletions src/connection/__tests__/connection.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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),
},
}),
});

Expand Down Expand Up @@ -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 });
});
});
28 changes: 22 additions & 6 deletions src/connection/connection.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 29440e5

Please sign in to comment.