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

View File

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

View File

@ -1,5 +1,3 @@
import { Product } from '@commerce/types'
import {
Product as ShopifyProduct,
Checkout,
@ -11,7 +9,7 @@ import {
ProductOption,
} from '../schema'
import type { Cart, LineItem } from '../types'
import type { Cart, LineItem, Product } from '../types'
const money = ({ amount, currencyCode }: MoneyV2) => {
return {
@ -86,6 +84,7 @@ export function normalizeProduct(productNode: ShopifyProduct): Product {
handle,
priceRange,
options,
totalInventory,
...rest
} = productNode
@ -99,6 +98,7 @@ export function normalizeProduct(productNode: ShopifyProduct): Product {
price: money(priceRange?.minVariantPrice),
images: normalizeProductImages(images),
variants: variants ? normalizeProductVariants(variants) : [],
totalInventory,
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

View File

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

View File

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