Skip to content

Commit

Permalink
Update pagination handling
Browse files Browse the repository at this point in the history
  • Loading branch information
danielailie committed Feb 21, 2024
1 parent ccfb765 commit a65f3c9
Show file tree
Hide file tree
Showing 18 changed files with 37 additions and 41 deletions.
4 changes: 2 additions & 2 deletions src/modules/analytics/collections-analytics.resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { CollectionsAnalyticsModel } from './models/collections-stats.model';
import { BaseResolver } from '../common/base.resolver';
import { CollectionsAnalyticsResponse } from './models/collections-analytics.response';
import PageResponse from '../common/PageResponse';
import ConnectionArgs from '../common/filters/ConnectionArgs';
import ConnectionArgs, { getPagingParameters } from '../common/filters/ConnectionArgs';
import { CollectionDetailsProvider } from './loaders/collection-details.loader';
import { CollectionsDetailsModel } from './models/collections-details.model';
import { AnalyticsArgs, CollectionAnalyticsArgs } from './models/analytics-args.model';
Expand All @@ -26,7 +26,7 @@ export class CollectionsAnalyticsResolver extends BaseResolver(CollectionsAnalyt
@Args('input', { type: () => CollectionAnalyticsArgs, nullable: true })
input: CollectionAnalyticsArgs,
): Promise<CollectionsAnalyticsResponse> {
const { limit, offset } = pagination.pagingParams();
const { limit, offset } = getPagingParameters(pagination);
const [collections, count] = await this.collectionsAnalyticsService.getCollectionsOrderByVolum(limit, offset, input.series);
return PageResponse.mapResponse<CollectionsAnalyticsModel>(collections || [], pagination, count || 0, 0, limit);
}
Expand Down
4 changes: 2 additions & 2 deletions src/modules/artists/artists.resolver.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Query, Resolver, Args } from '@nestjs/graphql';
import { Account } from '../account-stats/models';
import ConnectionArgs from '../common/filters/ConnectionArgs';
import ConnectionArgs, { getPagingParameters } from '../common/filters/ConnectionArgs';
import PageResponse from '../common/PageResponse';
import { ArtistsService } from './artists.service';
import { ArtistFilters } from './models/Artists.Filter';
Expand All @@ -17,7 +17,7 @@ export class ArtistsResolver {
@Args({ name: 'pagination', type: () => ConnectionArgs, nullable: true })
pagination: ConnectionArgs,
): Promise<ArtistResponse> {
const { limit, offset } = pagination.pagingParams();
const { limit, offset } = getPagingParameters(pagination);
const [accounts, count] = await this.artistsService.getArtists(filters, offset, limit);
return PageResponse.mapResponse<Account>(accounts || [], pagination, count || 0, offset, limit);
}
Expand Down
4 changes: 2 additions & 2 deletions src/modules/assets/asset-auction.resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { Asset } from './models';
import { Auction, AuctionResponse } from '../auctions/models';
import { AuctionEntity } from 'src/db/auctions';
import { AuctionsForAssetProvider } from '../auctions/loaders/asset-auctions.loader';
import ConnectionArgs from '../common/filters/ConnectionArgs';
import ConnectionArgs, { getPagingParameters } from '../common/filters/ConnectionArgs';
import PageResponse from '../common/PageResponse';

@Resolver(() => Asset)
Expand All @@ -19,7 +19,7 @@ export class AssetAuctionResolver extends BaseResolver(Asset) {
@Args({ name: 'pagination', type: () => ConnectionArgs, nullable: true })
pagination: ConnectionArgs,
) {
const { limit, offset } = pagination.pagingParams();
const { limit, offset } = getPagingParameters(pagination);
const { identifier } = asset;
if (!identifier) {
return null;
Expand Down
4 changes: 2 additions & 2 deletions src/modules/assets/assets-queries.resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { AssetsSupplyLoader } from './loaders/assets-supply.loader';
import { AssetScamInfoProvider } from './loaders/assets-scam-info.loader';
import { IsAssetLikedProvider } from './loaders/asset-is-liked.loader';
import { LowestAuctionProvider } from '../auctions/loaders/lowest-auctions.loader';
import ConnectionArgs from '../common/filters/ConnectionArgs';
import ConnectionArgs, { getPagingParameters } from '../common/filters/ConnectionArgs';
import { AssetsFilter } from '../common/filters/filtersTypes';
import PageResponse from '../common/PageResponse';
import { AssetsViewsLoader } from './loaders/assets-views.loader';
Expand Down Expand Up @@ -63,7 +63,7 @@ export class AssetsQueriesResolver extends BaseResolver(Asset) {
@Args({ name: 'sorting', type: () => AssetsSortingEnum, nullable: true })
sorting: AssetsSortingEnum,
): Promise<AssetsResponse> {
const { limit, offset } = pagination.pagingParams();
const { limit, offset } = getPagingParameters(pagination);

Check warning on line 66 in src/modules/assets/assets-queries.resolver.ts

View check run for this annotation

Codecov / codecov/patch

src/modules/assets/assets-queries.resolver.ts#L66

Added line #L66 was not covered by tests
const response = await this.assetsService.getAssets(offset, limit, filters, sorting);

return PageResponse.mapResponse<Asset>(response?.items || [], pagination, response?.count || 0, offset, limit);
Expand Down
4 changes: 2 additions & 2 deletions src/modules/auctions/auction-orders.resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { AuctionsOrdersProvider } from '.';
import { Auction } from './models';
import { Order, OrdersResponse } from '../orders/models';
import { OrderEntity } from 'src/db/orders';
import ConnectionArgs from '../common/filters/ConnectionArgs';
import ConnectionArgs, { getPagingParameters } from '../common/filters/ConnectionArgs';
import PageResponse from '../common/PageResponse';

@Resolver(() => Auction)
Expand All @@ -19,7 +19,7 @@ export class AuctionOrdersResolver extends BaseResolver(Auction) {
@Args({ name: 'pagination', type: () => ConnectionArgs, nullable: true })
pagination: ConnectionArgs,
) {
const { limit, offset } = pagination.pagingParams();
const { limit, offset } = getPagingParameters(pagination);
const { id } = auction;
if (!id) {
return null;
Expand Down
8 changes: 4 additions & 4 deletions src/modules/auctions/auctions-queries.resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { UseGuards } from '@nestjs/common';
import { AccountsProvider } from '../account-stats/loaders/accounts.loader';
import { AssetsProvider } from '../assets/loaders/assets.loader';
import { Account } from '../account-stats/models';
import ConnectionArgs from '../common/filters/ConnectionArgs';
import ConnectionArgs, { getPagingParameters } from '../common/filters/ConnectionArgs';
import { FiltersExpression, Sorting, Grouping } from '../common/filters/filtersTypes';
import { AuctionCustomFilter } from '../common/filters/AuctionCustomFilters';
import PageResponse from '../common/PageResponse';
Expand Down Expand Up @@ -68,7 +68,7 @@ export class AuctionsQueriesResolver extends BaseResolver(Auction) {
@Args({ name: 'pagination', type: () => ConnectionArgs, nullable: true })
pagination: ConnectionArgs,
) {
const { limit, offset } = pagination.pagingParams();
const { limit, offset } = getPagingParameters(pagination);
const [auctions, count, priceRange] = await this.auctionsGetterService.getAuctions(
new QueryRequest({
limit,
Expand Down Expand Up @@ -97,7 +97,7 @@ export class AuctionsQueriesResolver extends BaseResolver(Auction) {
@Args({ name: 'pagination', type: () => ConnectionArgs, nullable: true })
pagination: ConnectionArgs,
) {
const { limit, offset } = pagination.pagingParams();
const { limit, offset } = getPagingParameters(pagination);

const [auctions, count, priceRange] = await this.auctionsGetterService.getAuctionsOrderByNoBids(
new QueryRequest({ limit, offset, filters, groupByOption: groupBy }),
Expand Down Expand Up @@ -134,7 +134,7 @@ export class AuctionsQueriesResolver extends BaseResolver(Auction) {
@Args({ name: 'pagination', type: () => ConnectionArgs, nullable: true })
pagination: ConnectionArgs,
) {
const { limit, offset } = pagination.pagingParams();
const { limit, offset } = getPagingParameters(pagination);
const [auctions, count] = await this.auctionsGetterService.getClaimableAuctions(limit, offset, user.address, filters?.marketplaceKey);
return PageResponse.mapResponse<Auction>(auctions, pagination, count, offset, limit);
}
Expand Down
4 changes: 2 additions & 2 deletions src/modules/campaigns/campaigns-queries.resolver.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Resolver, Query, Args, ResolveField, Parent } from '@nestjs/graphql';
import { BaseResolver } from '../common/base.resolver';
import { Campaign, CampaignsResponse } from './models';
import ConnectionArgs from '../common/filters/ConnectionArgs';
import ConnectionArgs, { getPagingParameters } from '../common/filters/ConnectionArgs';
import PageResponse from '../common/PageResponse';
import { CampaignStatusEnum } from './models/CampaignStatus.enum';
import { DateUtils } from 'src/utils/date-utils';
Expand All @@ -21,7 +21,7 @@ export class CampaignsQueriesResolver extends BaseResolver(Campaign) {
@Args({ name: 'pagination', type: () => ConnectionArgs, nullable: true })
pagination: ConnectionArgs,
) {
const { limit, offset } = pagination.pagingParams();
const { limit, offset } = getPagingParameters(pagination);
const campaigns = await this.campaignsService.getCampaigns(limit, offset, filters);
return PageResponse.mapResponse<Campaign>(campaigns?.items || [], pagination, campaigns?.count || 0, offset, limit);
}
Expand Down
6 changes: 1 addition & 5 deletions src/modules/common/filters/ConnectionArgs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ function checkPagingSanity(args: ConnectionArgs): PagingMeta {
const getId = (cursor: ConnectionCursor) => parseInt(fromGlobalId(cursor).id, 10);
const nextId = (cursor: ConnectionCursor) => getId(cursor) + 1;

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

switch (meta.pagingType) {
Expand Down Expand Up @@ -81,10 +81,6 @@ export default class ConnectionArgs implements ConnectionArguments {
@Max(100)
@Field(() => Int, { nullable: true, description: 'Paginate last' })
public last?: number;

pagingParams() {
return getPagingParameters(this);
}
}

@InputType()
Expand Down
4 changes: 2 additions & 2 deletions src/modules/featured/featured-collections.resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { Asset, AssetsResponse } from '../assets/models';
import { GqlAdminAuthGuard } from '../auth/gql-admin.auth-guard';
import { JwtOrNativeAuthGuard } from '../auth/jwt.or.native.auth-guard';
import { BaseResolver } from '../common/base.resolver';
import ConnectionArgs from '../common/filters/ConnectionArgs';
import ConnectionArgs, { getPagingParameters } from '../common/filters/ConnectionArgs';
import PageResponse from '../common/PageResponse';
import { Collection } from '../nftCollections/models';
import CollectionResponse from '../nftCollections/models/CollectionResponse';
Expand All @@ -29,7 +29,7 @@ export class FeaturedCollectionsResolver extends BaseResolver(Collection) {
@Args({ name: 'pagination', type: () => ConnectionArgs, nullable: true })
pagination: ConnectionArgs,
): Promise<AssetsResponse> {
const { limit, offset } = pagination.pagingParams();
const { limit, offset } = getPagingParameters(pagination);
const [collections, count] = await this.featuredService.getFeaturedCollections(filters, limit, offset);
return PageResponse.mapResponse<Collection>(collections || [], pagination, count || 0, offset, limit);
}
Expand Down
4 changes: 2 additions & 2 deletions src/modules/featured/featured-nfts.resolver.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Resolver, Query, Args } from '@nestjs/graphql';
import { Asset, AssetsResponse } from '../assets/models';
import { BaseResolver } from '../common/base.resolver';
import ConnectionArgs from '../common/filters/ConnectionArgs';
import ConnectionArgs, { getPagingParameters } from '../common/filters/ConnectionArgs';
import PageResponse from '../common/PageResponse';
import { FeaturedService } from './featured.service';

Expand All @@ -16,7 +16,7 @@ export class FeaturedNftsResolver extends BaseResolver(Asset) {
@Args({ name: 'pagination', type: () => ConnectionArgs, nullable: true })
pagination: ConnectionArgs,
): Promise<AssetsResponse> {
const { limit, offset } = pagination.pagingParams();
const { limit, offset } = getPagingParameters(pagination);
const [assets, count] = await this.featuredNftsService.getFeaturedNfts(limit, offset);
return PageResponse.mapResponse<Asset>(assets || [], pagination, count || 0, offset, limit);
}
Expand Down
4 changes: 2 additions & 2 deletions src/modules/marketplaces/marketplaces-queries.resolver.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Resolver, Query, Args, ResolveField, Parent } from '@nestjs/graphql';
import { BaseResolver } from '../common/base.resolver';
import ConnectionArgs from '../common/filters/ConnectionArgs';
import ConnectionArgs, { getPagingParameters } from '../common/filters/ConnectionArgs';
import PageResponse from '../common/PageResponse';
import { MarketplacesService } from './marketplaces.service';
import { Marketplace } from './models/Marketplace.dto';
Expand Down Expand Up @@ -28,7 +28,7 @@ export class MarketplacesQueriesResolver extends BaseResolver(Marketplace) {
@Args({ name: 'pagination', type: () => ConnectionArgs, nullable: true })
pagination: ConnectionArgs,
) {
const { limit, offset } = pagination.pagingParams();
const { limit, offset } = getPagingParameters(pagination);
const marketplaces = await this.marketplaceService.getMarketplaces(limit, offset, filters);
// todo: investigate
pagination.after = undefined;
Expand Down
4 changes: 2 additions & 2 deletions src/modules/minters/minters-queries.resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { JwtOrNativeAuthGuard } from '../auth/jwt.or.native.auth-guard';
import { MintersService } from './minters.service';
import { GqlAdminAuthGuard } from '../auth/gql-admin.auth-guard';
import { MinterFilters } from './models/MinterFilters';
import ConnectionArgs from '../common/filters/ConnectionArgs';
import ConnectionArgs, { getPagingParameters } from '../common/filters/ConnectionArgs';
import PageResponse from '../common/PageResponse';
import { MintersResponse } from './models/MintersResponse';

Expand All @@ -23,7 +23,7 @@ export class MintersQueriesResolver extends BaseResolver(MintersResponse) {
@Args({ name: 'pagination', type: () => ConnectionArgs, nullable: true })
pagination: ConnectionArgs,
): Promise<MintersResponse> {
const { limit, offset } = pagination.pagingParams();
const { limit, offset } = getPagingParameters(pagination);
const minters = await this.minterService.getMinters(filters);
return PageResponse.mapResponse<Minter>(minters || [], pagination, minters?.length || 0, offset, limit);
}
Expand Down
4 changes: 2 additions & 2 deletions src/modules/nftCollections/collections-queries.resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { Collection, CollectionAsset } from './models';
import CollectionResponse from './models/CollectionResponse';
import { AccountsProvider } from '../account-stats/loaders/accounts.loader';
import { Account } from '../account-stats/models';
import ConnectionArgs from '../common/filters/ConnectionArgs';
import ConnectionArgs, { getPagingParameters } from '../common/filters/ConnectionArgs';
import PageResponse from '../common/PageResponse';
import { OnSaleAssetsCountForCollectionProvider } from './loaders/onsale-assets-count.loader';
import { Address } from '@multiversx/sdk-core';
Expand Down Expand Up @@ -45,7 +45,7 @@ export class CollectionsQueriesResolver extends BaseResolver(Collection) {
@Args({ name: 'pagination', type: () => ConnectionArgs, nullable: true })
pagination: ConnectionArgs,
): Promise<CollectionResponse> {
const { limit, offset } = pagination.pagingParams();
const { limit, offset } = getPagingParameters(pagination);
const [collections, count] = await this.collectionsGetterService.getCollections(offset, limit, filters, sorting);

return PageResponse.mapResponse<Collection>(collections || [], pagination, count || 0, offset, limit);
Expand Down
4 changes: 2 additions & 2 deletions src/modules/notifications/notifications.resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { BaseResolver } from '../common/base.resolver';
import { NotificationsService } from './notifications.service';
import { NotificationsResponse, Notification } from './models';
import { connectionFromArraySlice } from 'graphql-relay';
import ConnectionArgs from '../common/filters/ConnectionArgs';
import ConnectionArgs, { getPagingParameters } from '../common/filters/ConnectionArgs';
import { UseGuards } from '@nestjs/common';
import { NotificationsFilters } from './models/Notifications.Filter';
import { JwtOrNativeAuthGuard } from '../auth/jwt.or.native.auth-guard';
Expand All @@ -25,7 +25,7 @@ export class NotificationsResolver extends BaseResolver(Notification) {
pagination: ConnectionArgs,
@AuthUser() user: UserAuthResult,
) {
const { limit, offset } = pagination.pagingParams();
const { limit, offset } = getPagingParameters(pagination);
const [notifications, count] = await this.notificationsService.getNotifications(user.address, filters?.marketplaceKey);
const page = connectionFromArraySlice(notifications, pagination, {
arrayLength: count,
Expand Down
4 changes: 2 additions & 2 deletions src/modules/offers/offers.resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { connectionFromArraySlice } from 'graphql-relay';
import { AccountsProvider } from '../account-stats/loaders/accounts.loader';
import { NftMarketplaceAbiService } from '../auctions';
import { Account } from '../account-stats/models';
import ConnectionArgs from '../common/filters/ConnectionArgs';
import ConnectionArgs, { getPagingParameters } from '../common/filters/ConnectionArgs';
import { UseGuards } from '@nestjs/common';
import { TransactionNode } from '../common/transaction';
import { AssetsProvider } from '../assets';
Expand Down Expand Up @@ -40,7 +40,7 @@ export class OffersResolver extends BaseResolver(Offer) {
@Args({ name: 'pagination', type: () => ConnectionArgs, nullable: true })
pagination: ConnectionArgs,
) {
const { limit, offset } = pagination.pagingParams();
const { limit, offset } = getPagingParameters(pagination);
const [offers, count] = await this.offersService.getOffers(filters, offset, limit);
const page = connectionFromArraySlice(offers, pagination, {
arrayLength: count,
Expand Down
4 changes: 2 additions & 2 deletions src/modules/orders/orders.resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { AccountsProvider } from '../account-stats/loaders/accounts.loader';
import { AuctionProvider } from '../auctions';
import { Account } from '../account-stats/models';
import { FiltersExpression, Sorting } from '../common/filters/filtersTypes';
import ConnectionArgs from '../common/filters/ConnectionArgs';
import ConnectionArgs, { getPagingParameters } from '../common/filters/ConnectionArgs';
import { QueryRequest } from '../common/filters/QueryRequest';

@Resolver(() => Order)
Expand All @@ -26,7 +26,7 @@ export class OrdersResolver extends BaseResolver(Order) {
@Args({ name: 'pagination', type: () => ConnectionArgs, nullable: true })
pagination: ConnectionArgs,
) {
const { limit, offset } = pagination.pagingParams();
const { limit, offset } = getPagingParameters(pagination);
const [orders, count] = await this.ordersService.getOrders(new QueryRequest({ limit, offset, filters, sorting }));
const page = connectionFromArraySlice(orders, pagination, {
arrayLength: count,
Expand Down
4 changes: 2 additions & 2 deletions src/modules/owners/owners.resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Query, Resolver, Args, ResolveField, Parent } from '@nestjs/graphql';
import { OwnersService } from './owners.service';
import { AccountsProvider } from '../account-stats/loaders/accounts.loader';
import { Account } from '../account-stats/models';
import ConnectionArgs from '../common/filters/ConnectionArgs';
import ConnectionArgs, { getPagingParameters } from '../common/filters/ConnectionArgs';
import PageResponse from '../common/PageResponse';

@Resolver(() => Owner)
Expand All @@ -17,7 +17,7 @@ export class OwnersResolver {
@Args({ name: 'pagination', type: () => ConnectionArgs, nullable: true })
pagination: ConnectionArgs,
): Promise<OwnerResponse> {
const { limit, offset } = pagination.pagingParams();
const { limit, offset } = getPagingParameters(pagination);
const [owners, count] = await this.ownersService.getOwnersForIdentifier(filters?.identifier, offset, limit);
return PageResponse.mapResponse<Owner>(owners || [], pagination, count || 0, offset, limit);
}
Expand Down
4 changes: 2 additions & 2 deletions src/modules/tags/tags.resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Tag } from './models';
import { Args, Query, Resolver } from '@nestjs/graphql';
import { TagsService } from './tags.service';
import { TagsResponse } from './models/TagsResponse';
import ConnectionArgs from '../common/filters/ConnectionArgs';
import ConnectionArgs, { getPagingParameters } from '../common/filters/ConnectionArgs';
import PageResponse from '../common/PageResponse';
import { TagsFilter } from './models/Tags.Filter';

Expand All @@ -17,7 +17,7 @@ export class TagsResolver {
@Args({ name: 'pagination', type: () => ConnectionArgs, nullable: true })
pagination: ConnectionArgs,
): Promise<TagsResponse> {
const { limit, offset } = pagination.pagingParams();
const { limit, offset } = getPagingParameters(pagination);
const [tags, count] = await this.tagsService.getTags(offset, limit, filters);

return PageResponse.mapResponse<Tag>(tags || [], pagination, count || 0, offset, limit);
Expand Down

0 comments on commit a65f3c9

Please sign in to comment.