Skip to content

Commit f1918b5

Browse files
committed
Merge pull request #39 from taion/split-connectionArgs
Split up connectionArgs
2 parents 13e79da + b5689b5 commit f1918b5

File tree

3 files changed

+115
-11
lines changed

3 files changed

+115
-11
lines changed

README.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,12 @@ Helper functions are provided for both building the GraphQL types
4545
for connections and for implementing the `resolve` method for fields
4646
returning those types.
4747

48-
- `connectionArgs` returns the arguments that fields should provide when
49-
they return a connection type.
48+
- `connectionArgs` returns the arguments that fields should provide when they
49+
return a connection type that supports bidirectional pagination.
50+
- `forwardConnectionArgs` returns the arguments that fields should provide
51+
when they return a connection type that only supports forward pagination.
52+
- `backwardConnectionArgs` returns the arguments that fields should provide
53+
when they return a connection type that only supports backward pagination.
5054
- `connectionDefinitions` returns a `connectionType` and its associated
5155
`edgeType`, given a name and a node type.
5256
- `connectionFromArray` is a helper method that takes an array and the

src/connection/__tests__/connection.js

Lines changed: 87 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,18 @@ import {
1212
GraphQLObjectType,
1313
GraphQLSchema,
1414
GraphQLString,
15-
graphql
15+
graphql,
1616
} from 'graphql';
1717

1818
import {
19-
connectionFromArray
19+
connectionFromArray,
2020
} from '../arrayconnection.js';
2121

2222
import {
23+
backwardConnectionArgs,
2324
connectionArgs,
24-
connectionDefinitions
25+
connectionDefinitions,
26+
forwardConnectionArgs,
2527
} from '../connection.js';
2628

2729
import { expect } from 'chai';
@@ -46,6 +48,16 @@ var userType = new GraphQLObjectType({
4648
args: connectionArgs,
4749
resolve: (user, args) => connectionFromArray(allUsers, args),
4850
},
51+
friendsForward: {
52+
type: friendConnection,
53+
args: forwardConnectionArgs,
54+
resolve: (user, args) => connectionFromArray(allUsers, args),
55+
},
56+
friendsBackward: {
57+
type: friendConnection,
58+
args: backwardConnectionArgs,
59+
resolve: (user, args) => connectionFromArray(allUsers, args),
60+
},
4961
}),
5062
});
5163

@@ -121,4 +133,76 @@ describe('connectionDefinition()', () => {
121133
var result = await graphql(schema, query);
122134
expect(result).to.deep.equal({ data: expected });
123135
});
136+
137+
it('works with forwardConnectionArgs', async () => {
138+
var query = `
139+
query FriendsQuery {
140+
user {
141+
friendsForward(first: 2) {
142+
edges {
143+
node {
144+
name
145+
}
146+
}
147+
}
148+
}
149+
}
150+
`;
151+
var expected = {
152+
user: {
153+
friendsForward: {
154+
edges: [
155+
{
156+
node: {
157+
name: 'Dan'
158+
}
159+
},
160+
{
161+
node: {
162+
name: 'Nick'
163+
}
164+
},
165+
]
166+
}
167+
}
168+
};
169+
var result = await graphql(schema, query);
170+
expect(result).to.deep.equal({ data: expected });
171+
});
172+
173+
it('works with backwardConnectionArgs', async () => {
174+
var query = `
175+
query FriendsQuery {
176+
user {
177+
friendsBackward(last: 2) {
178+
edges {
179+
node {
180+
name
181+
}
182+
}
183+
}
184+
}
185+
}
186+
`;
187+
var expected = {
188+
user: {
189+
friendsBackward: {
190+
edges: [
191+
{
192+
node: {
193+
name: 'Joe'
194+
}
195+
},
196+
{
197+
node: {
198+
name: 'Tim'
199+
}
200+
},
201+
]
202+
}
203+
}
204+
};
205+
var result = await graphql(schema, query);
206+
expect(result).to.deep.equal({ data: expected });
207+
});
124208
});

src/connection/connection.js

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,24 +23,40 @@ import type {
2323
} from 'graphql';
2424

2525
/**
26-
* Returns a GraphQLFieldConfigArgumentMap appropriate to include
27-
* on a field whose return type is a connection type.
26+
* Returns a GraphQLFieldConfigArgumentMap appropriate to include on a field
27+
* whose return type is a connection type with forward pagination.
2828
*/
29-
export var connectionArgs: GraphQLFieldConfigArgumentMap = {
30-
before: {
31-
type: GraphQLString
32-
},
29+
export var forwardConnectionArgs: GraphQLFieldConfigArgumentMap = {
3330
after: {
3431
type: GraphQLString
3532
},
3633
first: {
3734
type: GraphQLInt
3835
},
36+
};
37+
38+
/**
39+
* Returns a GraphQLFieldConfigArgumentMap appropriate to include on a field
40+
* whose return type is a connection type with backward pagination.
41+
*/
42+
export var backwardConnectionArgs: GraphQLFieldConfigArgumentMap = {
43+
before: {
44+
type: GraphQLString
45+
},
3946
last: {
4047
type: GraphQLInt
4148
},
4249
};
4350

51+
/**
52+
* Returns a GraphQLFieldConfigArgumentMap appropriate to include on a field
53+
* whose return type is a connection type with bidirectional pagination.
54+
*/
55+
export var connectionArgs: GraphQLFieldConfigArgumentMap = {
56+
...forwardConnectionArgs,
57+
...backwardConnectionArgs,
58+
};
59+
4460
type ConnectionConfig = {
4561
name: string,
4662
nodeType: GraphQLObjectType,

0 commit comments

Comments
 (0)