Skip to content
Merged
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
- Added @After middleware decorator - [#56](https://github.com/indigotech/graphql-schema-decorator/pull/56) - [@marcelorisse](https://github.com/marcelorisse)
- Bugfix - @After middleware changing return type - [#58](https://github.com/indigotech/graphql-schema-decorator/pull/58) - [@felipesabino](https://github.com/felipesabino)
- Removed wrong comparison on description - [#60](https://github.com/indigotech/graphql-schema-decorator/pull/60) - [@askmon](https://github.com/askmon)
- Bugfix - @Pagination middleware page info - [#61](https://github.com/indigotech/graphql-schema-decorator/pull/61) - [@marcelorisse](https://github.com/marcelorisse)

### Breaking changes

Expand Down
2 changes: 1 addition & 1 deletion src/middleware/pagination.middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export function PaginationMiddleware(target: any, propertyKey: string, methodDes
.map(fieldArguments => {
const paginationValues = fieldArguments
.reduce((indexMap, metadata) => {
indexMap[metadata.name] = metadata.index;
indexMap[metadata.name] = args[metadata.index];
return indexMap;
}, {} as { [name: string]: number; });
return new PaginationResponse(count, data, new PageInfo(count, paginationValues['offset'], paginationValues['limit']));
Expand Down
55 changes: 50 additions & 5 deletions src/specs/functional.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,10 +106,13 @@ describe('Functional', function () {
class QueryType {
@D.Field({ pagination: true, type: graphql.GraphQLString })
async paginated(
@D.Arg({ name: 'offset', type: graphql.GraphQLInt }) offset: number,
@D.Arg({ name: 'limit', type: graphql.GraphQLInt }) limit: number,
@D.Arg({ name: 'value', type: graphql.GraphQLString }) value: string,
@D.Ctx() context: any,
): Promise<[string[], number]> {
return [[`Hello, ${value}!`], 1];
const items = [`Hello, ${value}!`, `Hello again, ${value}!`, `Hi, ${value}!`];
return [items.slice(offset, offset + limit), items.length];
}
}

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

it('resolves @Field with pagination', async function () {
it('resolves @Field with pagination for first page', async function () {
const schema = schemaFactory(SchemaType);
const result = await graphql.graphql(schema, `
query {
paginated(value: "world") {
paginated(value: "world", offset: 0, limit: 1) {
count
nodes
pageInfo {
Expand All @@ -132,13 +135,55 @@ describe('Functional', function () {
}
}
`);
assert(result.data.paginated.count === 1);
assert(result.data.paginated.count === 3);
assert(result.data.paginated.nodes.length === 1);
assert(result.data.paginated.nodes[0] === 'Hello, world!');
assert(result.data.paginated.pageInfo.hasNextPage === false);
assert(result.data.paginated.pageInfo.hasNextPage === true);
assert(result.data.paginated.pageInfo.hasPreviousPage === false);
});

it('resolves @Field with pagination middle page', async function () {
const schema = schemaFactory(SchemaType);
const result = await graphql.graphql(schema, `
query {
paginated(value: "world", offset: 1, limit: 1) {
count
nodes
pageInfo {
hasNextPage
hasPreviousPage
}
}
}
`);
assert(result.data.paginated.count === 3);
assert(result.data.paginated.nodes.length === 1);
assert(result.data.paginated.nodes[0] === 'Hello again, world!');
assert(result.data.paginated.pageInfo.hasNextPage === true);
assert(result.data.paginated.pageInfo.hasPreviousPage === true);
});

it('resolves @Field with pagination for last page', async function () {
const schema = schemaFactory(SchemaType);
const result = await graphql.graphql(schema, `
query {
paginated(value: "world", offset: 2, limit: 1) {
count
nodes
pageInfo {
hasNextPage
hasPreviousPage
}
}
}
`);
assert(result.data.paginated.count === 3);
assert(result.data.paginated.nodes.length === 1);
assert(result.data.paginated.nodes[0] === 'Hi, world!');
assert(result.data.paginated.pageInfo.hasNextPage === false);
assert(result.data.paginated.pageInfo.hasPreviousPage === true);
});

});

describe('Before Middleware', function () {
Expand Down