Add support for totalInvetory

This commit is contained in:
cond0r 2021-03-27 00:15:08 +02:00
parent 41eb6954e0
commit 81ac0dd1d4
5 changed files with 11 additions and 12 deletions

View File

@ -1,5 +1,5 @@
import * as Core from '@commerce/types' import * as Core from '@commerce/types'
import { CheckoutLineItem } from './schema' import { CheckoutLineItem, Maybe, Scalars } from './schema'
export type ShopifyCheckout = { export type ShopifyCheckout = {
id: string id: string
@ -18,6 +18,10 @@ export interface LineItem extends Core.LineItem {
* Cart mutations * Cart mutations
*/ */
export type Product = Core.Product & {
totalInventory?: Maybe<Scalars['Int']>
}
export type OptionSelections = { export type OptionSelections = {
option_id: number option_id: number
option_value: number | string option_value: number | string

View File

@ -27,15 +27,8 @@ export type CheckoutPayload =
| CheckoutQuery | CheckoutQuery
const checkoutToCart = (checkoutPayload?: Maybe<CheckoutPayload>): Cart => { const checkoutToCart = (checkoutPayload?: Maybe<CheckoutPayload>): Cart => {
if (!checkoutPayload) {
throw new CommerceError({
message: 'Missing checkout payload from response',
})
}
const checkout = checkoutPayload?.checkout
throwUserErrors(checkoutPayload?.checkoutUserErrors) throwUserErrors(checkoutPayload?.checkoutUserErrors)
const checkout = checkoutPayload?.checkout
if (!checkout) { if (!checkout) {
throw new CommerceError({ throw new CommerceError({
message: 'Missing checkout object from response', message: 'Missing checkout object from response',

View File

@ -1,5 +1,3 @@
import { Product } from '@commerce/types'
import { import {
Product as ShopifyProduct, Product as ShopifyProduct,
Checkout, Checkout,
@ -11,7 +9,7 @@ import {
ProductOption, ProductOption,
} from '../schema' } from '../schema'
import type { Cart, LineItem } from '../types' import type { Cart, LineItem, Product } from '../types'
const money = ({ amount, currencyCode }: MoneyV2) => { const money = ({ amount, currencyCode }: MoneyV2) => {
return { return {
@ -86,6 +84,7 @@ export function normalizeProduct(productNode: ShopifyProduct): Product {
handle, handle,
priceRange, priceRange,
options, options,
totalInventory,
...rest ...rest
} = productNode } = productNode
@ -99,6 +98,7 @@ export function normalizeProduct(productNode: ShopifyProduct): Product {
price: money(priceRange?.minVariantPrice), price: money(priceRange?.minVariantPrice),
images: normalizeProductImages(images), images: normalizeProductImages(images),
variants: variants ? normalizeProductVariants(variants) : [], variants: variants ? normalizeProductVariants(variants) : [],
totalInventory,
options: options options: options
? options ? options
.filter((o) => o.name !== 'Title') // By default Shopify adds a 'Title' name when there's only one option. We don't need it. https://community.shopify.com/c/Shopify-APIs-SDKs/Adding-new-product-variant-is-automatically-adding-quot-Default/td-p/358095 .filter((o) => o.name !== 'Title') // By default Shopify adds a 'Title' name when there's only one option. We don't need it. https://community.shopify.com/c/Shopify-APIs-SDKs/Adding-new-product-variant-is-automatically-adding-quot-Default/td-p/358095

View File

@ -10,6 +10,7 @@ edges {
vendor vendor
handle handle
description description
totalInventory
priceRange { priceRange {
minVariantPrice { minVariantPrice {
amount amount

View File

@ -8,6 +8,7 @@ const getProductQuery = /* GraphQL */ `
vendor vendor
description description
descriptionHtml descriptionHtml
totalInventory
options { options {
id id
name name