Skip to content

Commit

Permalink
Allow for providing a resolution function for nodes
Browse files Browse the repository at this point in the history
This allows for the decoupling of creating the connection from fulfilling the nodes themselves, which is especially useful for arrayConnection where the array may be a list of IDs or URLs instead of the fulfilled items.
  • Loading branch information
leebyron committed Nov 2, 2015
1 parent f1918b5 commit 2967c37
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 14 deletions.
29 changes: 15 additions & 14 deletions src/connection/__tests__/connection.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,11 @@ import { expect } from 'chai';
import { describe, it } from 'mocha';

var allUsers = [
{ name: 'Dan' },
{ name: 'Nick' },
{ name: 'Lee' },
{ name: 'Joe' },
{ name: 'Tim' },
{ name: 'Dan', friends: [ 1, 2, 3, 4 ] },
{ name: 'Nick', friends: [ 0, 2, 3, 4 ] },
{ name: 'Lee', friends: [ 0, 1, 3, 4 ] },
{ name: 'Joe', friends: [ 0, 1, 2, 4 ] },
{ name: 'Tim', friends: [ 0, 1, 2, 3 ] },
];

var userType = new GraphQLObjectType({
Expand All @@ -46,24 +46,25 @@ var userType = new GraphQLObjectType({
friends: {
type: friendConnection,
args: connectionArgs,
resolve: (user, args) => connectionFromArray(allUsers, args),
resolve: (user, args) => connectionFromArray(user.friends, args),
},
friendsForward: {
type: friendConnection,
args: forwardConnectionArgs,
resolve: (user, args) => connectionFromArray(allUsers, args),
resolve: (user, args) => connectionFromArray(user.friends, args),
},
friendsBackward: {
type: friendConnection,
args: backwardConnectionArgs,
resolve: (user, args) => connectionFromArray(allUsers, args),
resolve: (user, args) => connectionFromArray(user.friends, args),
},
}),
});

var {connectionType: friendConnection} = connectionDefinitions({
name: 'Friend',
nodeType: userType,
resolveNode: edge => allUsers[edge.node],
edgeFields: () => ({
friendshipTime: {
type: GraphQLString,
Expand All @@ -73,7 +74,7 @@ var {connectionType: friendConnection} = connectionDefinitions({
connectionFields: () => ({
totalCount: {
type: GraphQLInt,
resolve: () => allUsers.length
resolve: () => allUsers.length - 1
}
}),
});
Expand Down Expand Up @@ -112,18 +113,18 @@ describe('connectionDefinition()', () => {
var expected = {
user: {
friends: {
totalCount: 5,
totalCount: 4,
edges: [
{
friendshipTime: 'Yesterday',
node: {
name: 'Dan'
name: 'Nick'
}
},
{
friendshipTime: 'Yesterday',
node: {
name: 'Nick'
name: 'Lee'
}
},
]
Expand Down Expand Up @@ -154,12 +155,12 @@ describe('connectionDefinition()', () => {
edges: [
{
node: {
name: 'Dan'
name: 'Nick'
}
},
{
node: {
name: 'Nick'
name: 'Lee'
}
},
]
Expand Down
6 changes: 6 additions & 0 deletions src/connection/connection.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ export var connectionArgs: GraphQLFieldConfigArgumentMap = {
type ConnectionConfig = {
name: string,
nodeType: GraphQLObjectType,
resolveNode?: ?Function,
resolveCursor?: ?Function,
edgeFields?: ?(() => GraphQLFieldConfigMap) | ?GraphQLFieldConfigMap,
connectionFields?: ?(() => GraphQLFieldConfigMap) | ?GraphQLFieldConfigMap,
}
Expand All @@ -83,16 +85,20 @@ export function connectionDefinitions(
var {name, nodeType} = config;
var edgeFields = config.edgeFields || {};
var connectionFields = config.connectionFields || {};
var resolveNode = config.resolveNode;
var resolveCursor = config.resolveCursor;
var edgeType = new GraphQLObjectType({
name: name + 'Edge',
description: 'An edge in a connection.',
fields: () => ({
node: {
type: nodeType,
resolve: resolveNode,
description: 'The item at the end of the edge',
},
cursor: {
type: new GraphQLNonNull(GraphQLString),
resolve: resolveCursor,
description: 'A cursor for use in pagination'
},
...resolveMaybeThunk(edgeFields)
Expand Down

0 comments on commit 2967c37

Please sign in to comment.