Skip to content

Commit 2e032d2

Browse files
committed
refactor: replaces ratingCallback function with a direct call to commerce client api
1 parent 165333d commit 2e032d2

File tree

2 files changed

+10
-11
lines changed

2 files changed

+10
-11
lines changed

packages/api/src/platforms/vtex/resolvers/query.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -182,10 +182,7 @@ export const Query = {
182182

183183
const productSearchPromise = ctx.clients.search.products(searchArgs)
184184

185-
const ratingCallback = async (productId: string) =>
186-
ctx.clients.commerce.rating(productId)
187-
188-
return { searchArgs, productSearchPromise, ratingCallback }
185+
return { searchArgs, productSearchPromise }
189186
},
190187
allProducts: async (
191188
_: unknown,

packages/api/src/platforms/vtex/resolvers/searchResult.ts

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,10 @@ import type { SearchArgs } from '../clients/search'
44
import type { Facet } from '../clients/search/types/FacetSearchResult'
55
import type { ProductSearchResult } from '../clients/search/types/ProductSearchResult'
66
import { pickBestSku } from '../utils/sku'
7-
import type { ProductRating } from '../clients/commerce/types/ProductRating'
87

98
export type Root = {
109
searchArgs: Omit<SearchArgs, 'type'>
1110
productSearchPromise: Promise<ProductSearchResult>
12-
ratingCallback: (productId: string) => Promise<ProductRating>
1311
}
1412

1513
const isRootFacet = (facet: Facet, isDepartment: boolean, isBrand: boolean) =>
@@ -22,10 +20,10 @@ const isRootFacet = (facet: Facet, isDepartment: boolean, isBrand: boolean) =>
2220
export const StoreSearchResult: Record<string, Resolver<Root>> = {
2321
suggestions: async (root, _, ctx) => {
2422
const {
25-
clients: { search },
23+
clients: { search, commerce },
2624
} = ctx
2725

28-
const { searchArgs, productSearchPromise, ratingCallback } = root
26+
const { searchArgs, productSearchPromise } = root
2927

3028
// If there's no search query, suggest the most popular searches.
3129
if (!searchArgs.query) {
@@ -57,7 +55,7 @@ export const StoreSearchResult: Record<string, Resolver<Root>> = {
5755
.filter((sku) => !!sku)
5856
.map(async (sku) => ({
5957
...sku,
60-
rating: await ratingCallback(sku.itemId),
58+
rating: await commerce.rating(sku.itemId),
6159
}))
6260
)
6361

@@ -68,7 +66,11 @@ export const StoreSearchResult: Record<string, Resolver<Root>> = {
6866
products: skus,
6967
}
7068
},
71-
products: async ({ productSearchPromise, ratingCallback }) => {
69+
products: async ({ productSearchPromise }, _, ctx) => {
70+
const {
71+
clients: { commerce },
72+
} = ctx
73+
7274
const productSearchResult = await productSearchPromise
7375

7476
const skus = productSearchResult.products
@@ -83,7 +85,7 @@ export const StoreSearchResult: Record<string, Resolver<Root>> = {
8385

8486
const edges = await Promise.all(
8587
skus.map(async (sku, index) => ({
86-
node: { ...sku, rating: await ratingCallback(sku.itemId) },
88+
node: { ...sku, rating: await commerce.rating(sku.itemId) },
8789
cursor: index.toString(),
8890
}))
8991
)

0 commit comments

Comments
 (0)