-
Notifications
You must be signed in to change notification settings - Fork 78
feat(api): Migrate to VTEX Intelligent Search V1 API #3385
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
Merged
+1,679
−623
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
337 changes: 132 additions & 205 deletions
337
packages/api/src/platforms/vtex/clients/search/index.ts
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or 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 hidden or 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 |
|---|---|---|
|
|
@@ -26,6 +26,7 @@ import { | |
| import type { CategoryTree } from '../clients/commerce/types/CategoryTree' | ||
| import type { ProfileAddress } from '../clients/commerce/types/Profile' | ||
| import type { SearchArgs } from '../clients/search' | ||
| import type { ProductSearchResult } from '../clients/search/types/ProductSearchResult' | ||
| import type { GraphqlContext } from '../index' | ||
| import { extractRuleForAuthorization } from '../utils/commercialAuth' | ||
| import { mutateChannelContext, mutateLocaleContext } from '../utils/contex' | ||
|
|
@@ -44,6 +45,16 @@ import { SORT_MAP } from '../utils/sort' | |
| import { FACET_CROSS_SELLING_MAP } from './../utils/facets' | ||
| import { StoreCollection } from './collection' | ||
|
|
||
| const INVALID_SKU_ID_ERROR = 'Invalid SkuId' | ||
| const SLUG_MISMATCH_ERROR = | ||
| 'Slug was set but the fetched sku does not satisfy the slug condition.' | ||
|
|
||
| const shouldFallbackToProductRoute = (error: unknown) => | ||
| isNotFoundError(error) || | ||
| (error instanceof Error && | ||
| (error.message === INVALID_SKU_ID_ERROR || | ||
| error.message.startsWith(SLUG_MISMATCH_ERROR))) | ||
|
|
||
| export const Query = { | ||
| product: async ( | ||
| _: unknown, | ||
|
|
@@ -73,7 +84,7 @@ export const Query = { | |
| const skuId = id ?? slug?.split('-').pop() ?? '' | ||
|
|
||
| if (!isValidSkuId(skuId)) { | ||
| throw new Error('Invalid SkuId') | ||
| throw new Error(INVALID_SKU_ID_ERROR) | ||
| } | ||
|
|
||
| const sku = await skuLoader.load(skuId) | ||
|
|
@@ -97,6 +108,10 @@ export const Query = { | |
|
|
||
| return sku | ||
| } catch (err) { | ||
| if (!shouldFallbackToProductRoute(err)) { | ||
| throw err | ||
| } | ||
|
|
||
| if (slug == null) { | ||
| throw new BadRequestError('Missing slug or id') | ||
| } | ||
|
|
@@ -107,15 +122,12 @@ export const Query = { | |
| throw new NotFoundError(`No product found for slug ${slug}`) | ||
| } | ||
|
|
||
| const { | ||
| products: [product], | ||
| } = await search.products({ | ||
| page: 0, | ||
| count: 1, | ||
| query: `product:${route.id}`, | ||
| // Manually disabling this flag to prevent regionalization issues | ||
| hideUnavailableItems: false, | ||
| }) | ||
| const product = await search | ||
| .fetchProduct({ | ||
| field: 'id', | ||
| value: String(route.id), | ||
| }) | ||
| .catch(() => null) | ||
|
|
||
| if (!product) { | ||
| throw new NotFoundError(`No product found for id ${route.id}`) | ||
|
|
@@ -162,39 +174,60 @@ export const Query = { | |
| mutateLocaleContext(ctx, locale) | ||
| } | ||
|
|
||
| let query = term | ||
| const after = maybeAfter ? Number(maybeAfter) : 0 | ||
| const searchArgs: Omit<SearchArgs, 'type'> = { | ||
| page: Math.ceil(after / first) || 0, | ||
| count: first, | ||
| query: term ?? undefined, | ||
| sort: SORT_MAP[sort ?? 'score_desc'] ?? SORT_MAP.score_desc, | ||
| selectedFacets: selectedFacets?.flatMap(transformSelectedFacet) ?? [], | ||
| sponsoredCount: sponsoredCount ?? undefined, | ||
| } | ||
|
|
||
| /** | ||
| * In case we are using crossSelling, we need to modify the search | ||
| * we will be performing on our search engine. The idea in here | ||
| * is to use the cross selling API for fetching the productIds our | ||
| * search will return for us. | ||
| * Doing this two request workflow makes it possible to have cross | ||
| * selling with Search features, like pagination, internationalization | ||
| * etc | ||
| * In case we are using crossSelling, we fetch product IDs from the | ||
| * cross selling API and then hydrate them using the PDP endpoint | ||
| * via productsByIdentifier. | ||
| */ | ||
| if (crossSelling) { | ||
| const products = await ctx.clients.commerce.catalog.products.crossselling( | ||
| { | ||
| const crossSellingProducts = | ||
| await ctx.clients.commerce.catalog.products.crossselling({ | ||
| type: FACET_CROSS_SELLING_MAP[crossSelling.key], | ||
| productId: crossSelling.value, | ||
| } | ||
| ) | ||
| }) | ||
|
|
||
| query = `product:${products | ||
| const productIds = crossSellingProducts | ||
| .map((x) => x.productId) | ||
| .slice(0, first) | ||
| .join(';')}` | ||
| } | ||
|
|
||
| const after = maybeAfter ? Number(maybeAfter) : 0 | ||
| const searchArgs: Omit<SearchArgs, 'type'> = { | ||
| page: Math.ceil(after / first) || 0, | ||
| count: first, | ||
| query: query ?? undefined, | ||
| sort: SORT_MAP[sort ?? 'score_desc'], | ||
| selectedFacets: selectedFacets?.flatMap(transformSelectedFacet) ?? [], | ||
| sponsoredCount: sponsoredCount ?? undefined, | ||
| const productSearchPromise: Promise<ProductSearchResult> = | ||
| ctx.clients.search | ||
| .productsByIdentifier({ field: 'id', values: productIds }) | ||
| .then((products) => ({ | ||
| products, | ||
| recordsFiltered: products.length, | ||
| pagination: { | ||
| count: products.length, | ||
| current: { index: 0, proxyURL: '' }, | ||
| before: [], | ||
| after: [], | ||
| perPage: first, | ||
| next: { index: 0, proxyURL: '' }, | ||
| previous: { index: 0, proxyURL: '' }, | ||
| first: { index: 0, proxyURL: '' }, | ||
| last: { index: 0, proxyURL: '' }, | ||
| }, | ||
| sampling: false, | ||
| options: { sorts: [], counts: [] }, | ||
| translated: false, | ||
| locale: '', | ||
| query: '', | ||
| operator: 'and', | ||
| fuzzy: '0', | ||
| searchId: '', | ||
| })) | ||
|
|
||
| return { searchArgs, productSearchPromise } | ||
| } | ||
|
|
||
| const productSearchPromise = ctx.clients.search.products(searchArgs) | ||
|
|
@@ -250,14 +283,12 @@ export const Query = { | |
| return [] | ||
| } | ||
|
|
||
| const query = `id:${productIds.join(';')}` | ||
| const products = await search.products({ | ||
| page: 0, | ||
| count: productIds.length, | ||
| query, | ||
| const products = await search.productsByIdentifier({ | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same as before. Should we hit this directly without a dataloader controlling the batch size and caching repeated keys requests? |
||
| field: 'id', | ||
| values: productIds, | ||
| }) | ||
|
|
||
| return products.products | ||
| return products | ||
| .flatMap((product) => | ||
| product.items.map((sku) => enhanceSku(sku, product)) | ||
| ) | ||
|
|
||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Here we are hiting the productsByIdentifier endpoint directly without any dataloader controlling how many requests it should batch as it is done in the getSKULoader. Shouldn't it have some logic to control max request fired simultaneously?