commerce/framework/saleor/product/get-product.ts
2021-06-09 17:02:10 +02:00

31 lines
657 B
TypeScript

import { GraphQLFetcherResult } from '@commerce/api'
import { getConfig, SaleorConfig } from '../api'
import { normalizeProduct, getProductQuery } from '../utils'
type Variables = {
slug: string
}
type ReturnType = {
product: any
}
const getProduct = async (options: {
variables: Variables
config: SaleorConfig
preview?: boolean
}): Promise<ReturnType> => {
let { config, variables } = options ?? {}
config = getConfig(config)
const { data }: GraphQLFetcherResult = await config.fetch(getProductQuery, {
variables,
})
return {
product: data?.product ? normalizeProduct(data.product) : null,
}
}
export default getProduct