Skip to content

Commit

Permalink
Merge pull request #439 from vtex-apps/feature/add-linkText-resolver
Browse files Browse the repository at this point in the history
add linkText resolver
  • Loading branch information
hiagolcm authored Jan 2, 2023
2 parents 01667c3 + 4b3d812 commit 2b6bfc2
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 1 deletion.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion manifest.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
31 changes: 31 additions & 0 deletions node/resolvers/search/product.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,23 @@ import { compose, last, omit, pathOr, split, flatten } from 'ramda'
import {
addContextToTranslatableString,
formatTranslatableProp,
shouldTranslateToBinding,
shouldTranslateToUserLocale,
} from '../../utils/i18n'
import { getBenefits } from '../benefits'
import { buildCategoryMap, logDegradedSearchError } from './utils'

type DynamicKey<T> = Record<string, T>

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,
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 2b6bfc2

Please sign in to comment.