Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

totalCount for query #11

Open
wants to merge 4 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,16 +96,15 @@ function getConnectionFromSlice(inSlice, mapper, args, count) {

var firstEdge = edges[0];
var lastEdge = edges[edges.length - 1];
var lowerBound = after ? afterOffset + 1 : 0;
var upperBound = before ? Math.min(beforeOffset, count) : count;

return {
totalCount: count,
edges: edges,
pageInfo: {
startCursor: firstEdge ? firstEdge.cursor : null,
endCursor: lastEdge ? lastEdge.cursor : null,
hasPreviousPage: last !== null ? startOffset > lowerBound : false,
hasNextPage: first !== null ? endOffset < upperBound : false
hasPreviousPage: count > 0 && offsetsFromArgs.skip > 0,
hasNextPage: (offsetsFromArgs.skip + offsetsFromArgs.limit) < count
}
};
}
Expand Down
20 changes: 20 additions & 0 deletions test/connectionFromMongoCursor.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ async function resultEqual(t, args, expected) {
}

test('returns all elements without filters', resultEqual, [], {
totalCount: 5,
edges: [
{
node: { letter: 'A', _id: 'letter_A' },
Expand Down Expand Up @@ -58,6 +59,7 @@ test('returns all elements without filters', resultEqual, [], {
});

test('respects a smaller first', resultEqual, [{ first: 2 }], {
totalCount: 5,
edges: [
{
node: { letter: 'A', _id: 'letter_A' },
Expand All @@ -77,6 +79,7 @@ test('respects a smaller first', resultEqual, [{ first: 2 }], {
});

test('respects an overly large first', resultEqual, [{ first: 10 }], {
totalCount: 5,
edges: [
{
node: { letter: 'A', _id: 'letter_A' },
Expand Down Expand Up @@ -108,6 +111,7 @@ test('respects an overly large first', resultEqual, [{ first: 10 }], {
});

test('respects a smaller last', resultEqual, [{ last: 2 }], {
totalCount: 5,
edges: [
{
node: { letter: 'D', _id: 'letter_D' },
Expand All @@ -127,6 +131,7 @@ test('respects a smaller last', resultEqual, [{ last: 2 }], {
});

test('respects an overly large last', resultEqual, [{ last: 10 }], {
totalCount: 5,
edges: [
{
node: { letter: 'A', _id: 'letter_A' },
Expand Down Expand Up @@ -160,6 +165,7 @@ test('respects an overly large last', resultEqual, [{ last: 10 }], {
test('respects first and after', resultEqual, [{
first: 2, after: 'bW9uZ29kYmNvbm5lY3Rpb246MQ==',
}], {
totalCount: 5,
edges: [
{
node: { letter: 'C', _id: 'letter_C' },
Expand All @@ -181,6 +187,7 @@ test('respects first and after', resultEqual, [{
test('respects first and after with long first', resultEqual, [{
first: 10, after: 'bW9uZ29kYmNvbm5lY3Rpb246MQ==',
}], {
totalCount: 5,
edges: [
{
node: { letter: 'C', _id: 'letter_C' },
Expand All @@ -206,6 +213,7 @@ test('respects first and after with long first', resultEqual, [{
test('respects last and before', resultEqual, [{
last: 2, before: 'bW9uZ29kYmNvbm5lY3Rpb246Mw==',
}], {
totalCount: 5,
edges: [
{
node: { letter: 'B', _id: 'letter_B' },
Expand All @@ -227,6 +235,7 @@ test('respects last and before', resultEqual, [{
test('respects last and before with long last', resultEqual, [{
last: 10, before: 'bW9uZ29kYmNvbm5lY3Rpb246Mw==',
}], {
totalCount: 5,
edges: [
{
node: { letter: 'A', _id: 'letter_A' },
Expand Down Expand Up @@ -254,6 +263,7 @@ test('respects first and after and before, too few', resultEqual, [{
after: 'bW9uZ29kYmNvbm5lY3Rpb246MA==',
before: 'bW9uZ29kYmNvbm5lY3Rpb246NA==',
}], {
totalCount: 5,
edges: [
{
node: { letter: 'B', _id: 'letter_B' },
Expand All @@ -277,6 +287,7 @@ test('respects first and after and before, too many', resultEqual, [{
after: 'bW9uZ29kYmNvbm5lY3Rpb246MA==',
before: 'bW9uZ29kYmNvbm5lY3Rpb246NA==',
}], {
totalCount: 5,
edges: [
{
node: { letter: 'B', _id: 'letter_B' },
Expand Down Expand Up @@ -304,6 +315,7 @@ test('respects first and after and before, exactly right', resultEqual, [{
after: 'bW9uZ29kYmNvbm5lY3Rpb246MA==',
before: 'bW9uZ29kYmNvbm5lY3Rpb246NA==',
}], {
totalCount: 5,
edges: [
{
node: { letter: 'B', _id: 'letter_B' },
Expand Down Expand Up @@ -331,6 +343,7 @@ test('respects last and after and before, too few', resultEqual, [{
after: 'bW9uZ29kYmNvbm5lY3Rpb246MA==',
before: 'bW9uZ29kYmNvbm5lY3Rpb246NA==',
}], {
totalCount: 5,
edges: [
{
node: { letter: 'C', _id: 'letter_C' },
Expand All @@ -354,6 +367,7 @@ test('respects last and after and before, too many', resultEqual, [{
after: 'bW9uZ29kYmNvbm5lY3Rpb246MA==',
before: 'bW9uZ29kYmNvbm5lY3Rpb246NA==',
}], {
totalCount: 5,
edges: [
{
node: { letter: 'B', _id: 'letter_B' },
Expand Down Expand Up @@ -381,6 +395,7 @@ test('respects last and after and before, exactly right', resultEqual, [{
after: 'bW9uZ29kYmNvbm5lY3Rpb246MA==',
before: 'bW9uZ29kYmNvbm5lY3Rpb246NA==',
}], {
totalCount: 5,
edges: [
{
node: { letter: 'B', _id: 'letter_B' },
Expand All @@ -406,6 +421,7 @@ test('respects last and after and before, exactly right', resultEqual, [{
test('returns no elements if first is 0', resultEqual, [{
first: 0
}], {
totalCount: 5,
edges: [],
pageInfo: {
startCursor: null,
Expand All @@ -419,6 +435,7 @@ test('returns all elements if cursors are invalid', resultEqual, [{
before: 'invalid',
after: 'invalid',
}], {
totalCount: 5,
edges: [
{
node: { letter: 'A', _id: 'letter_A' },
Expand Down Expand Up @@ -453,6 +470,7 @@ test('returns all elements if cursors are on the outside', resultEqual, [{
before: 'bW9uZ29kYmNvbm5lY3Rpb246Ng==',
after: 'bW9uZ29kYmNvbm5lY3Rpb246LTE=',
}], {
totalCount: 5,
edges: [
{
node: { letter: 'A', _id: 'letter_A' },
Expand Down Expand Up @@ -487,6 +505,7 @@ test('returns no elements if cursors cross', resultEqual, [{
before: 'bW9uZ29kYmNvbm5lY3Rpb246Mg==',
after: 'bW9uZ29kYmNvbm5lY3Rpb246NA==',
}], {
totalCount: 5,
edges: [],
pageInfo: {
startCursor: null,
Expand All @@ -499,6 +518,7 @@ test('returns no elements if cursors cross', resultEqual, [{
test('uses mapper function if supplied', resultEqual, [
{}, doc => ({ ...doc, number: doc.letter.charCodeAt(0) })
], {
totalCount: 5,
edges: [
{
node: { letter: 'A', _id: 'letter_A', number: 65 },
Expand Down
26 changes: 24 additions & 2 deletions test/connectionFromMongooseAggregate.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ import { connectionFromMongooseAggregate } from '../src';
import mongoose from 'mongoose';

const SCHEMA = new mongoose.Schema({
letter: String,
_id: String,
letter: String,
_id: String,
}, {
versionKey: false
});

const MODEL = mongoose.model('aggr_letter', SCHEMA);
Expand Down Expand Up @@ -37,6 +39,7 @@ async function resultEqual(t, args, expected) {
}

test('returns all elements without filters', resultEqual, [], {
totalCount: 5,
edges: [
{
node: { letter: 'A', _id: 'letter_A' },
Expand Down Expand Up @@ -68,6 +71,7 @@ test('returns all elements without filters', resultEqual, [], {
});

test('respects a smaller first', resultEqual, [{ first: 2 }], {
totalCount: 5,
edges: [
{
node: { letter: 'A', _id: 'letter_A' },
Expand All @@ -87,6 +91,7 @@ test('respects a smaller first', resultEqual, [{ first: 2 }], {
});

test('respects an overly large first', resultEqual, [{ first: 10 }], {
totalCount: 5,
edges: [
{
node: { letter: 'A', _id: 'letter_A' },
Expand Down Expand Up @@ -118,6 +123,7 @@ test('respects an overly large first', resultEqual, [{ first: 10 }], {
});

test('respects a smaller last', resultEqual, [{ last: 2 }], {
totalCount: 5,
edges: [
{
node: { letter: 'D', _id: 'letter_D' },
Expand All @@ -137,6 +143,7 @@ test('respects a smaller last', resultEqual, [{ last: 2 }], {
});

test('respects an overly large last', resultEqual, [{ last: 10 }], {
totalCount: 5,
edges: [
{
node: { letter: 'A', _id: 'letter_A' },
Expand Down Expand Up @@ -170,6 +177,7 @@ test('respects an overly large last', resultEqual, [{ last: 10 }], {
test('respects first and after', resultEqual, [{
first: 2, after: 'bW9uZ29kYmNvbm5lY3Rpb246MQ==',
}], {
totalCount: 5,
edges: [
{
node: { letter: 'C', _id: 'letter_C' },
Expand All @@ -191,6 +199,7 @@ test('respects first and after', resultEqual, [{
test('respects first and after with long first', resultEqual, [{
first: 10, after: 'bW9uZ29kYmNvbm5lY3Rpb246MQ==',
}], {
totalCount: 5,
edges: [
{
node: { letter: 'C', _id: 'letter_C' },
Expand All @@ -216,6 +225,7 @@ test('respects first and after with long first', resultEqual, [{
test('respects last and before', resultEqual, [{
last: 2, before: 'bW9uZ29kYmNvbm5lY3Rpb246Mw==',
}], {
totalCount: 5,
edges: [
{
node: { letter: 'B', _id: 'letter_B' },
Expand All @@ -237,6 +247,7 @@ test('respects last and before', resultEqual, [{
test('respects last and before with long last', resultEqual, [{
last: 10, before: 'bW9uZ29kYmNvbm5lY3Rpb246Mw==',
}], {
totalCount: 5,
edges: [
{
node: { letter: 'A', _id: 'letter_A' },
Expand Down Expand Up @@ -264,6 +275,7 @@ test('respects first and after and before, too few', resultEqual, [{
after: 'bW9uZ29kYmNvbm5lY3Rpb246MA==',
before: 'bW9uZ29kYmNvbm5lY3Rpb246NA==',
}], {
totalCount: 5,
edges: [
{
node: { letter: 'B', _id: 'letter_B' },
Expand All @@ -287,6 +299,7 @@ test('respects first and after and before, too many', resultEqual, [{
after: 'bW9uZ29kYmNvbm5lY3Rpb246MA==',
before: 'bW9uZ29kYmNvbm5lY3Rpb246NA==',
}], {
totalCount: 5,
edges: [
{
node: { letter: 'B', _id: 'letter_B' },
Expand Down Expand Up @@ -314,6 +327,7 @@ test('respects first and after and before, exactly right', resultEqual, [{
after: 'bW9uZ29kYmNvbm5lY3Rpb246MA==',
before: 'bW9uZ29kYmNvbm5lY3Rpb246NA==',
}], {
totalCount: 5,
edges: [
{
node: { letter: 'B', _id: 'letter_B' },
Expand Down Expand Up @@ -341,6 +355,7 @@ test('respects last and after and before, too few', resultEqual, [{
after: 'bW9uZ29kYmNvbm5lY3Rpb246MA==',
before: 'bW9uZ29kYmNvbm5lY3Rpb246NA==',
}], {
totalCount: 5,
edges: [
{
node: { letter: 'C', _id: 'letter_C' },
Expand All @@ -364,6 +379,7 @@ test('respects last and after and before, too many', resultEqual, [{
after: 'bW9uZ29kYmNvbm5lY3Rpb246MA==',
before: 'bW9uZ29kYmNvbm5lY3Rpb246NA==',
}], {
totalCount: 5,
edges: [
{
node: { letter: 'B', _id: 'letter_B' },
Expand Down Expand Up @@ -391,6 +407,7 @@ test('respects last and after and before, exactly right', resultEqual, [{
after: 'bW9uZ29kYmNvbm5lY3Rpb246MA==',
before: 'bW9uZ29kYmNvbm5lY3Rpb246NA==',
}], {
totalCount: 5,
edges: [
{
node: { letter: 'B', _id: 'letter_B' },
Expand All @@ -416,6 +433,7 @@ test('respects last and after and before, exactly right', resultEqual, [{
test('returns no elements if first is 0', resultEqual, [{
first: 0
}], {
totalCount: 5,
edges: [],
pageInfo: {
startCursor: null,
Expand All @@ -429,6 +447,7 @@ test('returns all elements if cursors are invalid', resultEqual, [{
before: 'invalid',
after: 'invalid',
}], {
totalCount: 5,
edges: [
{
node: { letter: 'A', _id: 'letter_A' },
Expand Down Expand Up @@ -463,6 +482,7 @@ test('returns all elements if cursors are on the outside', resultEqual, [{
before: 'bW9uZ29kYmNvbm5lY3Rpb246Ng==',
after: 'bW9uZ29kYmNvbm5lY3Rpb246LTE=',
}], {
totalCount: 5,
edges: [
{
node: { letter: 'A', _id: 'letter_A' },
Expand Down Expand Up @@ -497,6 +517,7 @@ test('returns no elements if cursors cross', resultEqual, [{
before: 'bW9uZ29kYmNvbm5lY3Rpb246Mg==',
after: 'bW9uZ29kYmNvbm5lY3Rpb246NA==',
}], {
totalCount: 5,
edges: [],
pageInfo: {
startCursor: null,
Expand All @@ -509,6 +530,7 @@ test('returns no elements if cursors cross', resultEqual, [{
test('uses mapper function if supplied', resultEqual, [
{}, doc => ({ ...doc, number: doc.letter.charCodeAt(0) })
], {
totalCount: 5,
edges: [
{
node: { letter: 'A', _id: 'letter_A', number: 65 },
Expand Down
Loading