Skip to content

Commit

Permalink
add support for edgeFields
Browse files Browse the repository at this point in the history
  • Loading branch information
mickhansen committed Jan 21, 2016
1 parent 0fc0ca4 commit 2c601f2
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 10 deletions.
23 changes: 23 additions & 0 deletions docs/relay.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,29 @@ const userTaskConnection = sequelizeConnection({
// for custom args other than connectionArgs return a sequelize where parameter

return {[key]: value};
},
connectionFields: {
total: {
type: GraphQLInt,
resolve: ({source}) => {
/*
* We return a object containing the source, edges and more as the connection result
* You there for need to extract source from the usual source argument
*/
return source.countTasks();
}
}
},
edgeFields: {
wasCreatedByUser: {
type: GraphQLBoolean,
resolve: (edge) => {
/*
* We attach the connection source to edges
*/
return edge.node.createdBy === edge.source.id;
}
}
}
});

Expand Down
12 changes: 7 additions & 5 deletions src/relay.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,14 +88,15 @@ export function nodeType(connectionType) {
return connectionType._fields.edges.type.ofType._fields.node.type;
}

export function sequelizeConnection({name, nodeType, target, orderBy: orderByEnum, before, connectionFields, where}) {
export function sequelizeConnection({name, nodeType, target, orderBy: orderByEnum, before, connectionFields, edgeFields, where}) {
const {
edgeType,
connectionType
} = connectionDefinitions({
name,
nodeType,
connectionFields
connectionFields,
edgeFields
});

const model = target.target ? target.target : target;
Expand Down Expand Up @@ -154,14 +155,15 @@ export function sequelizeConnection({name, nodeType, target, orderBy: orderByEnu
return result;
};

let resolveEdge = function (item, args = {}) {
let resolveEdge = function (item, args = {}, source) {
if (!args.orderBy) {
args.orderBy = [defaultOrderBy];
}

return {
cursor: toCursor(item, args.orderBy),
node: item
node: item,
source: source
};
};

Expand Down Expand Up @@ -242,7 +244,7 @@ export function sequelizeConnection({name, nodeType, target, orderBy: orderByEnu
},
after: function (values, args, root, {source}) {
let edges = values.map((value) => {
return resolveEdge(value, args);
return resolveEdge(value, args, source);
});

let firstEdge = edges[0];
Expand Down
53 changes: 48 additions & 5 deletions test/integration/relay/connection.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ if (helper.sequelize.dialect.name === 'postgres') {
this.Project.Tasks = this.Project.hasMany(this.Task, {as: 'tasks', foreignKey: 'projectId'});
this.Task.Project = this.Task.belongsTo(this.Project, {as: 'project', foreignKey: 'projectId'});

this.Project.Owner = this.Project.belongsTo(this.User, {as: 'owner', foreignKey: 'ownerId'});

this.taskType = new GraphQLObjectType({
name: this.Task.name,
fields: {
Expand All @@ -76,7 +78,7 @@ if (helper.sequelize.dialect.name === 'postgres') {

this.projectTaskConnectionFieldSpy = sinon.spy();
this.projectTaskConnection = sequelizeConnection({
name: this.Project.name + this.Task.name,
name: 'projectTask',
nodeType: this.taskType,
target: this.Project.Tasks,
orderBy: new GraphQLEnumType({
Expand Down Expand Up @@ -110,13 +112,13 @@ if (helper.sequelize.dialect.name === 'postgres') {
type: this.projectTaskConnection.connectionType,
args: this.projectTaskConnection.connectionArgs,
resolve: this.projectTaskConnection.resolve
},
}
}
});

this.userTaskConnectionFieldSpy = sinon.spy();
this.userTaskConnection = sequelizeConnection({
name: this.User.name + this.Task.name,
name: 'userTask',
nodeType: this.taskType,
target: this.User.Tasks,
orderBy: new GraphQLEnumType({
Expand Down Expand Up @@ -148,15 +150,23 @@ if (helper.sequelize.dialect.name === 'postgres') {
});

this.userProjectConnection = sequelizeConnection({
name: this.Project.name,
name: 'userProject',
nodeType: this.projectType,
target: this.User.Projects,
orderBy: new GraphQLEnumType({
name: this.User.name + this.Project.name + 'ConnectionOrder',
values: {
ID: {value: [this.Project.primaryKeyAttribute, 'ASC']}
}
})
}),
edgeFields: {
isOwner: {
type: GraphQLBoolean,
resolve: function(edge) {
return edge.node.ownerId === edge.source.id;
}
}
}
});

this.userType = new GraphQLObjectType({
Expand Down Expand Up @@ -260,6 +270,9 @@ if (helper.sequelize.dialect.name === 'postgres') {
});

await Promise.join(
this.projectA.update({
ownerId: this.userA.get('id')
}),
this.ProjectMember.create({
projectId: this.projectA.get('id'),
userId: this.userA.get('id')
Expand Down Expand Up @@ -612,6 +625,36 @@ if (helper.sequelize.dialect.name === 'postgres') {
expect(this.projectTaskConnectionFieldSpy.firstCall.args[0].source.get('tasks')).to.be.undefined;
});

it('should support edgeFields', async function () {
let sqlSpy = sinon.spy();

let result = await graphql(this.schema, `
{
user(id: ${this.userA.id}) {
projects {
edges {
...projectOwner
node {
id
}
}
}
}
}
fragment projectOwner on userProjectEdge {
isOwner
}
`, {
logging: sqlSpy
});

if (result.errors) throw new Error(result.errors[0].stack);

let isOwner = result.data.user.projects.edges.map(edge => edge.isOwner);
expect(isOwner.sort()).to.deep.equal([true, false].sort());
});

it('should support connection fields with args/where', async function () {
let sqlSpy = sinon.spy();

Expand Down

0 comments on commit 2c601f2

Please sign in to comment.