-
Notifications
You must be signed in to change notification settings - Fork 63
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: allCollections query pagination and pagetype timeout errors (#1140)
- Loading branch information
Showing
8 changed files
with
112 additions
and
55 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
21 changes: 13 additions & 8 deletions
21
packages/api/src/platforms/vtex/clients/commerce/types/Portal.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 |
---|---|---|
@@ -1,14 +1,19 @@ | ||
export interface PortalPagetype { | ||
export type PortalPagetype = CollectionPageType | FallbackPageType | ||
|
||
export interface CollectionPageType { | ||
id: number | ||
name: string | ||
url: string | ||
title: string | ||
metaTagDescription: string | ||
pageType: | ||
| 'Brand' | ||
| 'Category' | ||
| 'Department' | ||
| 'Subcategory' | ||
| 'FullText' | ||
| 'NotFound' | ||
pageType: 'Brand' | 'Category' | 'Department' | 'Subcategory' | ||
} | ||
|
||
export interface FallbackPageType { | ||
id: null | ||
name: null | string | ||
url: null | string | ||
title: null | ||
metaTagDescription: null | ||
pageType: 'NotFound' | 'FullText' | ||
} |
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,50 @@ | ||
import DataLoader from 'dataloader' | ||
import pLimit from 'p-limit' | ||
|
||
import { NotFoundError } from '../utils/errors' | ||
import type { CollectionPageType } from '../clients/commerce/types/Portal' | ||
import type { Options } from '..' | ||
import type { Clients } from '../clients' | ||
|
||
// Limits concurrent requests to 20 so that they don't timeout | ||
const CONCURRENT_REQUESTS_MAX = 20 | ||
|
||
const collectionPageTypes = new Set([ | ||
'brand', | ||
'category', | ||
'department', | ||
'subcategory', | ||
] as const) | ||
|
||
export const isCollectionPageType = (x: any): x is CollectionPageType => | ||
typeof x.pageType === 'string' && | ||
collectionPageTypes.has(x.pageType.toLowerCase()) | ||
|
||
export const getCollectionLoader = (_: Options, clients: Clients) => { | ||
const limit = pLimit(CONCURRENT_REQUESTS_MAX) | ||
|
||
const loader = async ( | ||
slugs: readonly string[] | ||
): Promise<CollectionPageType[]> => { | ||
return Promise.all( | ||
slugs.map((slug: string) => | ||
limit(async () => { | ||
const page = await clients.commerce.catalog.portal.pagetype(slug) | ||
|
||
if (isCollectionPageType(page)) { | ||
return page | ||
} | ||
|
||
throw new NotFoundError( | ||
`Catalog returned ${page.pageType} for slug: ${slug}. This usually happens when there is more than one category with the same name in the same category tree level.` | ||
) | ||
}) | ||
) | ||
) | ||
} | ||
|
||
return new DataLoader<string, CollectionPageType>(loader, { | ||
// DataLoader is being used to cache requests, not to batch them | ||
batch: false, | ||
}) | ||
} |
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,15 +1,18 @@ | ||
import { getSimulationLoader } from './simulation' | ||
import { getSkuLoader } from './sku' | ||
import { getCollectionLoader } from './collection' | ||
import type { Context, Options } from '..' | ||
|
||
export type Loaders = ReturnType<typeof getLoaders> | ||
|
||
export const getLoaders = (options: Options, { clients }: Context) => { | ||
const skuLoader = getSkuLoader(options, clients) | ||
const simulationLoader = getSimulationLoader(options, clients) | ||
const collectionLoader = getCollectionLoader(options, clients) | ||
|
||
return { | ||
skuLoader, | ||
simulationLoader, | ||
collectionLoader, | ||
} | ||
} |
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