diff --git a/CHANGELOG.md b/CHANGELOG.md index d2c89080..f4196675 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,9 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0. ## [Unreleased] +### Added +- `linkText` resolver. + ## [1.63.0] - 2022-10-04 ### Changed diff --git a/manifest.json b/manifest.json index 3b6acf4f..157a5f77 100644 --- a/manifest.json +++ b/manifest.json @@ -1,7 +1,7 @@ { "vendor": "vtex", "name": "search-resolver", - "version": "1.63.0", + "version": "1.63.1-beta.0", "title": "GraphQL resolver for the VTEX store APIs", "description": "GraphQL resolvers for the VTEX API for the catalog and orders.", "credentialType": "absolute", diff --git a/node/resolvers/search/product.ts b/node/resolvers/search/product.ts index eacea655..35f518d4 100644 --- a/node/resolvers/search/product.ts +++ b/node/resolvers/search/product.ts @@ -3,6 +3,7 @@ import { compose, last, omit, pathOr, split, flatten } from 'ramda' import { addContextToTranslatableString, formatTranslatableProp, + shouldTranslateToBinding, shouldTranslateToUserLocale, } from '../../utils/i18n' import { getBenefits } from '../benefits' @@ -10,6 +11,15 @@ import { buildCategoryMap, logDegradedSearchError } from './utils' type DynamicKey = Record +const urlToSlug = (slug: string | undefined) => { + if (!slug) { + return slug + } + const erasedSlash = slug.replace(/^\//g, '') //removing starting / char + const finalSlug = erasedSlash.replace(/(\/p)$/g, '') //remove ending /p chars + return finalSlug +} + const objToNameValue = ( keyName: string, valueName: string, @@ -248,6 +258,27 @@ export const resolvers = { 'productId' ), + linkText: async ({ productId, linkText, origin }: SearchProduct, _: unknown, ctx: Context) => { + const { clients: { rewriter }, vtex: { binding } } = ctx + + if (origin === 'intelligent-search' || !shouldTranslateToBinding(ctx)) { + return linkText + } + + try { + const route = await rewriter.getRoute(productId, 'product', binding!.id!) + return urlToSlug(route) ?? linkText + } catch (e) { + logDegradedSearchError(ctx.vtex.logger, { + service: 'Rewriter getRoute', + error: `Rewriter getRoute query returned an error for productId ${productId}. Linktext may be incorrect.`, + errorStack: e, + }) + } + + return linkText + }, + specificationGroups: async (product: SearchProduct, _: unknown, ctx: Context) => { if (product.origin === 'intelligent-search') { return product.specificationGroups