Skip to content

Commit 9320872

Browse files
committed
fix: dont break pagination if there are no edges
1 parent 358fb44 commit 9320872

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed

src/relay.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,7 @@ export function sequelizeConnection({name, nodeType, target, orderBy: orderByEnu
188188
}
189189

190190
options.where = argsToWhere(args);
191+
options.required = false;
191192

192193
if (args.after || args.before) {
193194
let cursor = fromCursor(args.after || args.before);
@@ -237,6 +238,9 @@ export function sequelizeConnection({name, nodeType, target, orderBy: orderByEnu
237238
let lastEdge = edges[edges.length - 1];
238239
let fullCount = values[0] && values[0].dataValues.full_count && parseInt(values[0].dataValues.full_count, 10);
239240

241+
if (!values[0]) {
242+
fullCount = 0;
243+
}
240244
if (model.sequelize.dialect.name === 'postgres' && (args.first || args.last)) {
241245
if (fullCount === null || fullCount === undefined) throw new Error('No fullcount available');
242246
}

test/integration/relay/connection.test.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -597,6 +597,35 @@ if (helper.sequelize.dialect.name === 'postgres') {
597597
expect(result.data.user.tasks.totalCount).to.equal(4);
598598
expect(this.userTaskConnectionFieldSpy.firstCall.args[0].source.get('tasks')).to.be.undefined;
599599
});
600+
601+
it('should not barf on paging if there are no connection edges', async function () {
602+
let user = await this.User.create({});
603+
604+
let result = await graphql(this.schema, `
605+
{
606+
user(id: ${user.get('id')}) {
607+
tasks(first: 10) {
608+
totalCount
609+
610+
edges {
611+
node {
612+
id
613+
}
614+
}
615+
616+
pageInfo {
617+
hasNextPage
618+
}
619+
}
620+
}
621+
}
622+
`, {});
623+
624+
if (result.errors) throw new Error(result.errors[0].stack);
625+
expect(result.data.user).not.to.be.null;
626+
expect(result.data.user.tasks.totalCount).to.equal(0);
627+
expect(result.data.user.tasks.pageInfo.hasNextPage).to.equal(false);
628+
});
600629
});
601630
});
602631
}

0 commit comments

Comments
 (0)