Skip to content

Commit

Permalink
Merge pull request #27 from vtex/fix/slug
Browse files Browse the repository at this point in the history
Use slug to identify item in search-graphql query
  • Loading branch information
Marcos Kawakami authored Oct 10, 2019
2 parents 918a08f + 8e6c2fb commit 44ab6a9
Show file tree
Hide file tree
Showing 9 changed files with 55 additions and 23 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Changed

- `search-graphql`'s `product` query receives the product slug instead of its id when the request comes from a GoCommerce account.

## [0.14.0] - 2019-10-10

### Changed
Expand Down
1 change: 0 additions & 1 deletion node/clients/searchGraphQL/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ export class SearchGraphQL extends AppGraphQLClient {
query: string = productQuery
) => this.graphql.query<T, ProductArgs>({
query,
useGet: true,
variables,
}, {
metric: 'get-product',
Expand Down
7 changes: 4 additions & 3 deletions node/clients/searchGraphQL/productQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ export interface ProductResponse {
}

export interface ProductArgs {
identifier: ProductUniqueIdentifier
identifier?: ProductUniqueIdentifier
slug?: string
}

interface ProductUniqueIdentifier {
Expand All @@ -22,8 +23,8 @@ interface ProductUniqueIdentifier {
}

export const query = `
query Product($identifier: ProductUniqueIdentifier) {
product(identifier: $identifier) {
query Product($identifier: ProductUniqueIdentifier, $slug: String) {
product(identifier: $identifier, slug: $slug) {
productName
items {
itemId
Expand Down
2 changes: 1 addition & 1 deletion node/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"@types/lodash": "^4.14.138",
"@types/node": "^10.12.17",
"@types/ramda": "types/npm-ramda#dist",
"@vtex/api": "^3.8.1",
"@vtex/api": "^3.55.2",
"@vtex/test-tools": "^1.2.0",
"@vtex/tsconfig": "^0.2.0",
"tslint": "^5.12.0",
Expand Down
3 changes: 2 additions & 1 deletion node/resolvers/coupon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@ export const mutations = {
insertCoupon: async (_: any, args: any, ctx: Context) => {
const {
clients: { checkout, searchGraphQL },
vtex: { orderFormId },
vtex: { orderFormId, platform },
} = ctx
const newOrderForm = await checkout.insertCoupon(orderFormId!, args.text)

return getNewOrderForm({
checkout,
newOrderForm,
platform,
searchGraphQL,
})
},
Expand Down
37 changes: 28 additions & 9 deletions node/resolvers/items.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,30 @@ import { SearchGraphQL } from '../clients/searchGraphQL'
import { fixImageUrl } from '../utils/image'
import { getNewOrderForm } from './orderForm'

const GOCOMMERCE = 'gocommerce'

const getProductInfo = async (
platform: string,
item: OrderFormItem,
searchGraphQL: SearchGraphQL
) => {
let response

if (platform === GOCOMMERCE) {
const slug = item.detailUrl.split('/')[1]
response = await searchGraphQL.product({ slug })
} else {
response = await searchGraphQL.product({
identifier: {
field: 'id',
value: item.productId,
},
})
}

return response.data!.product
}

const getVariations = (skuId: string, skuList: any[]) => {
const matchedSku = skuList.find((sku: any) => sku.itemId === skuId)
if (!matchedSku) {
Expand All @@ -16,18 +40,12 @@ const getVariations = (skuId: string, skuList: any[]) => {
}

export const adjustItems = (
platform: string,
items: OrderFormItem[],
searchGraphQL: SearchGraphQL
) =>
map(items, async (item: OrderFormItem) => {
const response = await searchGraphQL.product({
identifier: {
field: 'id',
value: item.productId,
},
})

const { product } = response.data!
const product = await getProductInfo(platform, item, searchGraphQL)

return {
...item,
Expand All @@ -45,7 +63,7 @@ export const mutations = {
): Promise<OrderForm> => {
const {
clients: { checkout, searchGraphQL },
vtex: { orderFormId },
vtex: { orderFormId, platform },
} = ctx

if (orderItems.some((item: OrderFormItemInput) => !item.index)) {
Expand All @@ -71,6 +89,7 @@ export const mutations = {
return getNewOrderForm({
checkout,
newOrderForm,
platform,
searchGraphQL,
})
},
Expand Down
6 changes: 5 additions & 1 deletion node/resolvers/orderForm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@ import { getShippingInfo } from './shipping/utils/shipping'
export const getNewOrderForm = async ({
checkout,
newOrderForm,
platform,
searchGraphQL,
}: {
checkout: Checkout
newOrderForm: CheckoutOrderForm
platform: string
searchGraphQL: SearchGraphQL
}) => {
const { orderFormId, messages } = newOrderForm
Expand All @@ -22,7 +24,7 @@ export const getNewOrderForm = async ({
}

return {
items: await adjustItems(newOrderForm.items, searchGraphQL),
items: await adjustItems(platform, newOrderForm.items, searchGraphQL),
marketingData: newOrderForm.marketingData,
messages: newMessages,
shipping: getShippingInfo(newOrderForm),
Expand All @@ -35,13 +37,15 @@ export const queries = {
orderForm: async (_: any, __: any, ctx: Context): Promise<OrderForm> => {
const {
clients: { checkout, searchGraphQL },
vtex: { platform },
} = ctx

const newOrderForm = await checkout.orderForm()

return getNewOrderForm({
checkout,
newOrderForm,
platform,
searchGraphQL,
})
},
Expand Down
6 changes: 4 additions & 2 deletions node/resolvers/shipping/mutations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export const estimateShippingMutation = async (
) => {
const {
clients: { checkout, shipping, searchGraphQL },
vtex: { orderFormId },
vtex: { orderFormId, platform },
} = ctx

const orderForm = await checkout.orderForm()
Expand All @@ -24,6 +24,7 @@ export const estimateShippingMutation = async (
return getNewOrderForm({
checkout,
newOrderForm,
platform,
searchGraphQL,
})
}
Expand All @@ -35,7 +36,7 @@ export const selectDeliveryOptionMutation = async (
) => {
const {
clients: { checkout, shipping, searchGraphQL },
vtex: { orderFormId },
vtex: { orderFormId, platform },
} = ctx

const orderForm = await checkout.orderForm()
Expand All @@ -52,6 +53,7 @@ export const selectDeliveryOptionMutation = async (
return getNewOrderForm({
checkout,
newOrderForm,
platform,
searchGraphQL,
})
}
12 changes: 7 additions & 5 deletions node/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1324,20 +1324,22 @@
resolved "https://registry.yarnpkg.com/@types/zen-observable/-/zen-observable-0.8.0.tgz#8b63ab7f1aa5321248aad5ac890a485656dcea4d"
integrity sha512-te5lMAWii1uEJ4FwLjzdlbw3+n0FZNOvFXHxQDKeT0dilh7HOzdMzV2TrJVUzq8ep7J4Na8OUYPRLSQkJHAlrg==

"@vtex/api@^3.8.1":
version "3.31.0"
resolved "https://registry.yarnpkg.com/@vtex/api/-/api-3.31.0.tgz#a06b9daa6e998e25917aa15b1e508d059e794a34"
integrity sha512-aBJkfX6ZdlcjmyeWuyBw+EArczLJiQteCu5X7zb2tUP5uhPV40Do3E4SdbJwRNjkUkyTPE729GI0NBfZsnjBnw==
"@vtex/api@^3.55.2":
version "3.55.2"
resolved "https://registry.yarnpkg.com/@vtex/api/-/api-3.55.2.tgz#086bfdc95d5da268a64f0a4df3e4879589e03e81"
integrity sha512-i0LaoSHJdGOwmlFDQ3X1RvY0mqQIFATH91VesLT20avgE6m/lQLvnSwVrX+zfI2SIsCpcMCHnUaJ+R8MIB/S/Q==
dependencies:
"@types/koa" "^2.0.48"
"@types/koa-compose" "^3.2.3"
"@wry/equality" "^0.1.9"
apollo-datasource "^0.3.1"
apollo-server-core "^2.4.8"
apollo-server-errors "^2.2.1"
archiver "^3.0.0"
axios "^0.18.0"
axios-retry "^3.1.2"
bluebird "^3.5.4"
chalk "^2.4.2"
co-body "^6.0.0"
cookie "^0.3.1"
dataloader "^1.4.0"
Expand Down Expand Up @@ -1412,7 +1414,7 @@
"@types/node" ">=6"
tslib "^1.9.3"

"@wry/equality@^0.1.2":
"@wry/equality@^0.1.2", "@wry/equality@^0.1.9":
version "0.1.9"
resolved "https://registry.yarnpkg.com/@wry/equality/-/equality-0.1.9.tgz#b13e18b7a8053c6858aa6c85b54911fb31e3a909"
integrity sha512-mB6ceGjpMGz1ZTza8HYnrPGos2mC6So4NhS1PtZ8s4Qt0K7fBiIGhpSxUbQmhwcSWE3no+bYxmI2OL6KuXYmoQ==
Expand Down

0 comments on commit 44ab6a9

Please sign in to comment.