Skip to content

Commit 9e447ac

Browse files
committed
Merge pull request #48 from taion/connection-name
Default connection name from node type name
2 parents 2d61e1a + 6d12d14 commit 9e447ac

File tree

4 files changed

+17
-11
lines changed

4 files changed

+17
-11
lines changed

README.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ when they return a connection type that only supports forward pagination.
5252
- `backwardConnectionArgs` returns the arguments that fields should provide
5353
when they return a connection type that only supports backward pagination.
5454
- `connectionDefinitions` returns a `connectionType` and its associated
55-
`edgeType`, given a name and a node type.
55+
`edgeType`, given a node type.
5656
- `connectionFromArray` is a helper method that takes an array and the
5757
arguments from `connectionArgs`, does pagination and filtering, and returns
5858
an object in the shape expected by a `connectionType`'s `resolve` function.
@@ -66,7 +66,7 @@ An example usage of these methods from the [test schema](src/__tests__/starWarsS
6666

6767
```js
6868
var {connectionType: ShipConnection} =
69-
connectionDefinitions({name: 'Ship', nodeType: shipType});
69+
connectionDefinitions({nodeType: shipType});
7070
var factionType = new GraphQLObjectType({
7171
name: 'Faction',
7272
fields: () => ({
@@ -83,10 +83,10 @@ var factionType = new GraphQLObjectType({
8383
```
8484

8585
This shows adding a `ships` field to the `Faction` object that is a connection.
86-
It uses `connectionDefinitions({name: 'Ship', nodeType: shipType})` to create
87-
the connection type, adds `connectionArgs` as arguments on this function, and
88-
then implements the resolve function by passing the array of ships and the
89-
arguments to `connectionFromArray`.
86+
It uses `connectionDefinitions({nodeType: shipType})` to create the connection
87+
type, adds `connectionArgs` as arguments on this function, and then implements
88+
the resolve function by passing the array of ships and the arguments to
89+
`connectionFromArray`.
9090

9191
### Object Identification
9292

src/__tests__/starWarsSchema.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ var shipType = new GraphQLObjectType({
178178
* }
179179
*/
180180
var {connectionType: shipConnection} =
181-
connectionDefinitions({name: 'Ship', nodeType: shipType});
181+
connectionDefinitions({nodeType: shipType});
182182

183183
/**
184184
* We define our faction type, which implements the node interface.

src/connection/__tests__/connection.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,12 +49,12 @@ var userType = new GraphQLObjectType({
4949
resolve: (user, args) => connectionFromArray(user.friends, args),
5050
},
5151
friendsForward: {
52-
type: friendConnection,
52+
type: userConnection,
5353
args: forwardConnectionArgs,
5454
resolve: (user, args) => connectionFromArray(user.friends, args),
5555
},
5656
friendsBackward: {
57-
type: friendConnection,
57+
type: userConnection,
5858
args: backwardConnectionArgs,
5959
resolve: (user, args) => connectionFromArray(user.friends, args),
6060
},
@@ -79,6 +79,11 @@ var {connectionType: friendConnection} = connectionDefinitions({
7979
}),
8080
});
8181

82+
var {connectionType: userConnection} = connectionDefinitions({
83+
nodeType: userType,
84+
resolveNode: edge => allUsers[edge.node],
85+
});
86+
8287
var queryType = new GraphQLObjectType({
8388
name: 'Query',
8489
fields: () => ({

src/connection/connection.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ export var connectionArgs: GraphQLFieldConfigArgumentMap = {
5858
};
5959

6060
type ConnectionConfig = {
61-
name: string,
61+
name?: ?string,
6262
nodeType: GraphQLObjectType,
6363
resolveNode?: ?Function,
6464
resolveCursor?: ?Function,
@@ -82,7 +82,8 @@ function resolveMaybeThunk<T>(thingOrThunk: T | () => T): T {
8282
export function connectionDefinitions(
8383
config: ConnectionConfig
8484
): GraphQLConnectionDefinitions {
85-
var {name, nodeType} = config;
85+
var {nodeType} = config;
86+
var name = config.name != null ? config.name : nodeType.name;
8687
var edgeFields = config.edgeFields || {};
8788
var connectionFields = config.connectionFields || {};
8889
var resolveNode = config.resolveNode;

0 commit comments

Comments
 (0)