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

[SERVICES-2370] Pairs query pagination, sorting and filtering #1347

Merged
Show file tree
Hide file tree
Changes from 10 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
18 changes: 18 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@
"express": "^4.18.1",
"graphql": "^16.5.0",
"graphql-redis-subscriptions": "^2.4.2",
"graphql-relay": "^0.10.1",
"graphql-subscriptions": "^2.0.0",
"ioredis": "^5.2.4",
"jest": "^29.6.4",
Expand Down
2 changes: 2 additions & 0 deletions src/modules/auto-router/specs/auto-router.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import { ApiConfigService } from 'src/helpers/api.config.service';
import winston from 'winston';
import { DynamicModuleUtils } from 'src/utils/dynamic.module.utils';
import { MXApiServiceProvider } from 'src/services/multiversx-communication/mx.api.service.mock';
import { PairFilteringService } from 'src/modules/pair/services/pair.filtering.service';

describe('AutoRouterService', () => {
let service: AutoRouterService;
Expand Down Expand Up @@ -80,6 +81,7 @@ describe('AutoRouterService', () => {
AutoRouterTransactionService,
ApiConfigService,
MXApiServiceProvider,
PairFilteringService,
],
exports: [],
}).compile();
Expand Down
8 changes: 8 additions & 0 deletions src/modules/common/collection.type.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export class CollectionType<T> {
count: number;
items: T[];

constructor(init?: Partial<CollectionType<T>>) {
Object.assign(this, init);
}
}
98 changes: 98 additions & 0 deletions src/modules/common/filters/connection.args.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
import {
ConnectionArguments,
ConnectionCursor,
fromGlobalId,
} from 'graphql-relay';
import { Field, Int, InputType } from '@nestjs/graphql';
import { IsOptional, Max } from 'class-validator';

type PagingMeta =
| { pagingType: 'forward'; after?: string; first: number }
| { pagingType: 'backward'; before?: string; last: number }
| { pagingType: 'none' };

function checkPagingSanity(args: ConnectionArgs): PagingMeta {
if (!args) {
return { pagingType: 'none' };
}
const { first = 0, last = 0, after, before } = args;

const isForwardPaging = !!first || !!after;
const isBackwardPaging = !!last || !!before;
if (isForwardPaging && isBackwardPaging) {
throw new Error('Relay pagination cannot be forwards AND backwards!');
bogdan-rosianu marked this conversation as resolved.
Show resolved Hide resolved
}
if ((isForwardPaging && before) || (isBackwardPaging && after)) {
throw new Error('Paging must use either first/after or last/before!');
}
if ((isForwardPaging && first < 0) || (isBackwardPaging && last < 0)) {
throw new Error('Paging limit must be positive!');
}
if (last && !before) {
throw new Error(
"When paging backwards, a 'before' argument is required!",
);
}

// eslint-disable-next-line no-nested-ternary
return isForwardPaging
? { pagingType: 'forward', after, first }
: isBackwardPaging
? { pagingType: 'backward', before, last }
: { pagingType: 'none' };
}

const getId = (cursor: ConnectionCursor) =>
parseInt(fromGlobalId(cursor).id, 10);
const nextId = (cursor: ConnectionCursor) => getId(cursor) + 1;

export function getPagingParameters(args: ConnectionArgs) {
const meta = checkPagingSanity(args);

if (!args) {
args = new ConnectionArgs();
}

switch (meta.pagingType) {
case 'forward': {
return {
limit: meta.first,
offset: meta.after ? nextId(meta.after) : 0,
};
}
case 'backward': {
const { last, before } = meta;
let limit = last;
let offset = getId(before!) - last;

if (offset < 0) {
limit = Math.max(last + offset, 0);
offset = 0;
}

return { offset, limit };
}
default:
args.first = 10;
return { offset: 0, limit: 10 };
}
}

@InputType()
export default class ConnectionArgs implements ConnectionArguments {
@Field({ nullable: true, description: 'Paginate before opaque cursor' })
public before?: ConnectionCursor;

@Field({ nullable: true, description: 'Paginate after opaque cursor' })
public after?: ConnectionCursor;

@IsOptional()
@Max(100)
@Field(() => Int, { nullable: true, description: 'Paginate first' })
public first?: number;

@IsOptional()
@Max(100)
@Field(() => Int, { nullable: true, description: 'Paginate last' })
public last?: number;
}
13 changes: 13 additions & 0 deletions src/modules/common/page.data.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { Field, Int, ObjectType } from '@nestjs/graphql';

@ObjectType()
export default class PageData {
@Field(() => Int)
public count: number;

@Field(() => Int)
public limit: number;

@Field(() => Int)
public offset: number;
}
22 changes: 22 additions & 0 deletions src/modules/common/page.response.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { connectionFromArraySlice } from 'graphql-relay';
import ConnectionArgs from './filters/connection.args';

export default class PageResponse {
static mapResponse<T>(
returnList: T[],
args: ConnectionArgs,
count: number,
offset: number,
limit: number,
) {
const page = connectionFromArraySlice(returnList, args, {
arrayLength: count,
sliceStart: offset || 0,
});
return {
edges: page.edges,
pageInfo: page.pageInfo,
pageData: { count, limit, offset },
};
}
}
6 changes: 6 additions & 0 deletions src/modules/pair/models/pairs.response.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { ObjectType } from '@nestjs/graphql';
import relayTypes from 'src/utils/relay.types';
import { PairModel } from './pair.model';

@ObjectType()
export class PairsResponse extends relayTypes<PairModel>(PairModel) {}
3 changes: 3 additions & 0 deletions src/modules/pair/pair.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import { RemoteConfigModule } from '../remote-config/remote-config.module';
import { StakingProxyModule } from '../staking-proxy/staking.proxy.module';
import { ElasticService } from 'src/helpers/elastic.service';
import { FarmModuleV2 } from '../farm/v2/farm.v2.module';
import { PairFilteringService } from './services/pair.filtering.service';
@Module({
imports: [
CommonAppModule,
Expand All @@ -39,12 +40,14 @@ import { FarmModuleV2 } from '../farm/v2/farm.v2.module';
PairTransactionService,
PairResolver,
ElasticService,
PairFilteringService,
],
exports: [
PairService,
PairSetterService,
PairComputeService,
PairAbiService,
PairFilteringService,
],
})
export class PairModule {}
Loading
Loading