Skip to content

Commit 057379f

Browse files
authored
Merge pull request #61 from indigotech/bugfix/pagination-info
Bugfix - Pagination middleware page info
2 parents 238984d + fe3d167 commit 057379f

File tree

3 files changed

+52
-6
lines changed

3 files changed

+52
-6
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
- Added @After middleware decorator - [#56](https://github.com/indigotech/graphql-schema-decorator/pull/56) - [@marcelorisse](https://github.com/marcelorisse)
1919
- Bugfix - @After middleware changing return type - [#58](https://github.com/indigotech/graphql-schema-decorator/pull/58) - [@felipesabino](https://github.com/felipesabino)
2020
- Removed wrong comparison on description - [#60](https://github.com/indigotech/graphql-schema-decorator/pull/60) - [@askmon](https://github.com/askmon)
21+
- Bugfix - @Pagination middleware page info - [#61](https://github.com/indigotech/graphql-schema-decorator/pull/61) - [@marcelorisse](https://github.com/marcelorisse)
2122

2223
### Breaking changes
2324

src/middleware/pagination.middleware.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ export function PaginationMiddleware(target: any, propertyKey: string, methodDes
1414
.map(fieldArguments => {
1515
const paginationValues = fieldArguments
1616
.reduce((indexMap, metadata) => {
17-
indexMap[metadata.name] = metadata.index;
17+
indexMap[metadata.name] = args[metadata.index];
1818
return indexMap;
1919
}, {} as { [name: string]: number; });
2020
return new PaginationResponse(count, data, new PageInfo(count, paginationValues['offset'], paginationValues['limit']));

src/specs/functional.spec.ts

Lines changed: 50 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -106,10 +106,13 @@ describe('Functional', function () {
106106
class QueryType {
107107
@D.Field({ pagination: true, type: graphql.GraphQLString })
108108
async paginated(
109+
@D.Arg({ name: 'offset', type: graphql.GraphQLInt }) offset: number,
110+
@D.Arg({ name: 'limit', type: graphql.GraphQLInt }) limit: number,
109111
@D.Arg({ name: 'value', type: graphql.GraphQLString }) value: string,
110112
@D.Ctx() context: any,
111113
): Promise<[string[], number]> {
112-
return [[`Hello, ${value}!`], 1];
114+
const items = [`Hello, ${value}!`, `Hello again, ${value}!`, `Hi, ${value}!`];
115+
return [items.slice(offset, offset + limit), items.length];
113116
}
114117
}
115118

@@ -118,11 +121,11 @@ describe('Functional', function () {
118121
@D.Query() query: QueryType;
119122
}
120123

121-
it('resolves @Field with pagination', async function () {
124+
it('resolves @Field with pagination for first page', async function () {
122125
const schema = schemaFactory(SchemaType);
123126
const result = await graphql.graphql(schema, `
124127
query {
125-
paginated(value: "world") {
128+
paginated(value: "world", offset: 0, limit: 1) {
126129
count
127130
nodes
128131
pageInfo {
@@ -132,13 +135,55 @@ describe('Functional', function () {
132135
}
133136
}
134137
`);
135-
assert(result.data.paginated.count === 1);
138+
assert(result.data.paginated.count === 3);
136139
assert(result.data.paginated.nodes.length === 1);
137140
assert(result.data.paginated.nodes[0] === 'Hello, world!');
138-
assert(result.data.paginated.pageInfo.hasNextPage === false);
141+
assert(result.data.paginated.pageInfo.hasNextPage === true);
139142
assert(result.data.paginated.pageInfo.hasPreviousPage === false);
140143
});
141144

145+
it('resolves @Field with pagination middle page', async function () {
146+
const schema = schemaFactory(SchemaType);
147+
const result = await graphql.graphql(schema, `
148+
query {
149+
paginated(value: "world", offset: 1, limit: 1) {
150+
count
151+
nodes
152+
pageInfo {
153+
hasNextPage
154+
hasPreviousPage
155+
}
156+
}
157+
}
158+
`);
159+
assert(result.data.paginated.count === 3);
160+
assert(result.data.paginated.nodes.length === 1);
161+
assert(result.data.paginated.nodes[0] === 'Hello again, world!');
162+
assert(result.data.paginated.pageInfo.hasNextPage === true);
163+
assert(result.data.paginated.pageInfo.hasPreviousPage === true);
164+
});
165+
166+
it('resolves @Field with pagination for last page', async function () {
167+
const schema = schemaFactory(SchemaType);
168+
const result = await graphql.graphql(schema, `
169+
query {
170+
paginated(value: "world", offset: 2, limit: 1) {
171+
count
172+
nodes
173+
pageInfo {
174+
hasNextPage
175+
hasPreviousPage
176+
}
177+
}
178+
}
179+
`);
180+
assert(result.data.paginated.count === 3);
181+
assert(result.data.paginated.nodes.length === 1);
182+
assert(result.data.paginated.nodes[0] === 'Hi, world!');
183+
assert(result.data.paginated.pageInfo.hasNextPage === false);
184+
assert(result.data.paginated.pageInfo.hasPreviousPage === true);
185+
});
186+
142187
});
143188

144189
describe('Before Middleware', function () {

0 commit comments

Comments
 (0)