-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: infinite query for products list (#9)
* chore: infinite query for products list * chore: manually removing useless infinite queries * chore: fixing display issue when navigating from orders * chore: we should stop sending calls when there is no more data
- Loading branch information
Showing
21 changed files
with
793 additions
and
238 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
libs/graphql/types/src/api.ts | ||
libs/graphql/types/src/index.ts | ||
apps/front/.next | ||
.dockerignore |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { Field, InputType, Int } from '@nestjs/graphql'; | ||
|
||
@InputType() | ||
export class GqlPaginationArgs { | ||
@Field(() => Int) | ||
offset = 0; | ||
|
||
@Field(() => Int) | ||
limit = 25; | ||
} |
29 changes: 29 additions & 0 deletions
29
apps/api/src/modules/products/closures/get-paginated.closure.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import { Injectable } from '@nestjs/common'; | ||
|
||
import { DatabaseService, selectProduct } from '@backend/database'; | ||
|
||
import { GqlPaginationArgs } from '../../dtos/pagination-args.dto'; | ||
import { GetAllSelectType } from './get-all.closure'; | ||
|
||
@Injectable() | ||
export class GetPaginatedClosure { | ||
public static Include = selectProduct({ | ||
Category: true, | ||
}); | ||
|
||
constructor(private readonly db: DatabaseService) {} | ||
|
||
async fetch({ | ||
limit, | ||
offset, | ||
}: GqlPaginationArgs): Promise<[GetAllSelectType[], number]> { | ||
return this.db.$transaction([ | ||
this.db.product.findMany({ | ||
include: GetPaginatedClosure.Include, | ||
skip: offset, | ||
take: limit, | ||
}), | ||
this.db.product.count(), | ||
]); | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
apps/api/src/modules/products/dtos/gql.paginated-products.dto.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import 'reflect-metadata'; | ||
import { ObjectType, Field, Int } from '@nestjs/graphql'; | ||
|
||
import { GqlProduct } from './gql.product.dto'; | ||
|
||
@ObjectType() | ||
export class GqlPaginatedProducts { | ||
@Field(() => Int) | ||
id: number; | ||
|
||
@Field(() => [GqlProduct]) | ||
data: Array<GqlProduct>; | ||
|
||
@Field(() => Boolean) | ||
hasMoreData: boolean; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 36 additions & 11 deletions
47
apps/front/src/components/specialized/shop/articles-list/ArticlesList.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,46 @@ | ||
import { ProductsQueryData } from '@front/api'; | ||
import { Fragment } from 'react'; | ||
|
||
import { ProductsByPageQuery } from '@front/api'; | ||
|
||
import { | ||
LoadMoreProducts, | ||
LoadMoreProductsProps, | ||
} from '../load-more-products/LoadMoreProducts'; | ||
import { Article } from './article/Article'; | ||
|
||
type ArticlesListProps = { | ||
products?: ProductsQueryData; | ||
}; | ||
interface ArticlesListProps extends Omit<LoadMoreProductsProps, 'hasMoreData'> { | ||
pages?: ProductsByPageQuery[]; | ||
} | ||
|
||
export const ArticlesList = ({ products }: ArticlesListProps) => { | ||
if (!products) { | ||
export const ArticlesList = ({ | ||
pages, | ||
pageParams, | ||
fetchNextPage, | ||
hasNextPage, | ||
isLoading, | ||
}: ArticlesListProps) => { | ||
if (!pages) { | ||
return null; | ||
} | ||
|
||
return ( | ||
<div className="grid place-content-center gap-3 sm:grid-cols-2 md:grid-cols-3 lg:grid-cols-4 xl:grid-cols-5"> | ||
{products.map((p) => ( | ||
<Article key={p.id} {...p} /> | ||
))} | ||
</div> | ||
<> | ||
<div className="grid place-content-center gap-3 sm:grid-cols-2 md:grid-cols-3 lg:grid-cols-4 xl:grid-cols-5"> | ||
{pages.map(({ productsByPage }) => ( | ||
<Fragment key={productsByPage.id}> | ||
{productsByPage.data.map((p) => ( | ||
<Article key={p.id} {...p} /> | ||
))} | ||
</Fragment> | ||
))} | ||
</div> | ||
<LoadMoreProducts | ||
hasMoreData={pages.at(-1)?.productsByPage.hasMoreData} | ||
fetchNextPage={fetchNextPage} | ||
hasNextPage={hasNextPage} | ||
pageParams={pageParams} | ||
isLoading={isLoading} | ||
/> | ||
</> | ||
); | ||
}; |
18 changes: 18 additions & 0 deletions
18
apps/front/src/components/specialized/shop/graphql/get-products-by-page.query.graphql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
query ProductsByPage($offset: Int!, $limit: Int!) { | ||
productsByPage(pagination: { offset: $offset, limit: $limit }) { | ||
id | ||
data { | ||
id | ||
name | ||
description | ||
image | ||
price | ||
stock | ||
category { | ||
id | ||
name | ||
} | ||
} | ||
hasMoreData | ||
} | ||
} |
5 changes: 3 additions & 2 deletions
5
...ed/shop/graphql/get-product.query.graphql → ...d/shop/graphql/get-products.query.graphql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
258a3f9
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
graphql-shop – ./
graphql-shop-git-main-jpb06.vercel.app
graphql-shop-jpb06.vercel.app
graphql-shop-seven.vercel.app