mirror of
https://github.com/vercel/commerce.git
synced 2025-05-19 07:56:59 +00:00
Graphql codegen change
When generating graphql types, the generation of optional types is redundant because the value is wrapped in Maybe type anyway. Removing the redundancy simplifies the type checking whenever generated types are used.
This commit is contained in:
parent
fb112385e6
commit
c6df70c34c
@ -63,6 +63,7 @@ export type ProductVariant = {
|
|||||||
// The variant's depth. If a depth was not explicitly specified on the
|
// The variant's depth. If a depth was not explicitly specified on the
|
||||||
// variant, this will be the product's depth.
|
// variant, this will be the product's depth.
|
||||||
depth?: Measurement
|
depth?: Measurement
|
||||||
|
options: ProductOption[]
|
||||||
}
|
}
|
||||||
|
|
||||||
// Shopping cart, a.k.a Checkout
|
// Shopping cart, a.k.a Checkout
|
||||||
@ -109,6 +110,10 @@ export type CartItemBody = {
|
|||||||
variantId: string
|
variantId: string
|
||||||
productId?: string
|
productId?: string
|
||||||
quantity?: number
|
quantity?: number
|
||||||
|
pricing?: {
|
||||||
|
amount: number,
|
||||||
|
currencyCode: string,
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Body used by the `getCart` operation handler
|
// Body used by the `getCart` operation handler
|
||||||
@ -167,18 +172,18 @@ export interface Product extends Entity {
|
|||||||
slug?: string
|
slug?: string
|
||||||
path?: string
|
path?: string
|
||||||
images: ProductImage[]
|
images: ProductImage[]
|
||||||
variants: ProductVariant2[]
|
variants: ProductVariant[]
|
||||||
price: ProductPrice
|
price: ProductPrice
|
||||||
options: ProductOption[]
|
options: ProductOption[]
|
||||||
sku?: string
|
sku?: string
|
||||||
}
|
}
|
||||||
|
|
||||||
interface ProductOption extends Entity {
|
export interface ProductOption extends Entity {
|
||||||
displayName: string
|
displayName: string
|
||||||
values: ProductOptionValues[]
|
values: ProductOptionValues[]
|
||||||
}
|
}
|
||||||
|
|
||||||
interface ProductOptionValues {
|
export interface ProductOptionValues {
|
||||||
label: string
|
label: string
|
||||||
hexColors?: string[]
|
hexColors?: string[]
|
||||||
}
|
}
|
||||||
@ -188,11 +193,6 @@ interface ProductImage {
|
|||||||
alt?: string
|
alt?: string
|
||||||
}
|
}
|
||||||
|
|
||||||
interface ProductVariant2 {
|
|
||||||
id: string | number
|
|
||||||
options: ProductOption[]
|
|
||||||
}
|
|
||||||
|
|
||||||
interface ProductPrice {
|
interface ProductPrice {
|
||||||
value: number
|
value: number
|
||||||
currencyCode: 'USD' | 'ARS' | string | undefined
|
currencyCode: 'USD' | 'ARS' | string | undefined
|
||||||
|
@ -1,6 +1,5 @@
|
|||||||
import type { Cart } from '../../../types'
|
|
||||||
import type { CartHandlers } from '../'
|
import type { CartHandlers } from '../'
|
||||||
import getAnomymousCartQuery from '@framework/utils/queries/get-anonymous-cart'
|
import getAnonymousCartQuery from '@framework/utils/queries/get-anonymous-cart'
|
||||||
import accountCartByAccountIdQuery from '@framework/utils/queries/account-cart-by-account-id'
|
import accountCartByAccountIdQuery from '@framework/utils/queries/account-cart-by-account-id'
|
||||||
import getCartCookie from '@framework/api/utils/get-cart-cookie'
|
import getCartCookie from '@framework/api/utils/get-cart-cookie'
|
||||||
import reconcileCarts from '@framework/api/utils/reconcile-carts'
|
import reconcileCarts from '@framework/api/utils/reconcile-carts'
|
||||||
@ -9,7 +8,7 @@ import {
|
|||||||
REACTION_ANONYMOUS_CART_TOKEN_COOKIE,
|
REACTION_ANONYMOUS_CART_TOKEN_COOKIE,
|
||||||
REACTION_CART_ID_COOKIE,
|
REACTION_CART_ID_COOKIE,
|
||||||
REACTION_CUSTOMER_TOKEN_COOKIE,
|
REACTION_CUSTOMER_TOKEN_COOKIE,
|
||||||
} from '@framework/const.ts'
|
} from '@framework/const'
|
||||||
import { normalizeCart } from '@framework/utils'
|
import { normalizeCart } from '@framework/utils'
|
||||||
|
|
||||||
// Return current cart info
|
// Return current cart info
|
||||||
@ -42,7 +41,7 @@ const getCart: CartHandlers['getCart'] = async ({ req, res, config }) => {
|
|||||||
} else if (cartId && anonymousCartToken) {
|
} else if (cartId && anonymousCartToken) {
|
||||||
const {
|
const {
|
||||||
data: { cart: rawAnonymousCart },
|
data: { cart: rawAnonymousCart },
|
||||||
} = await config.fetch(getAnomymousCartQuery, {
|
} = await config.fetch(getAnonymousCartQuery, {
|
||||||
variables: {
|
variables: {
|
||||||
cartId,
|
cartId,
|
||||||
cartToken: anonymousCartToken,
|
cartToken: anonymousCartToken,
|
||||||
|
@ -6,7 +6,10 @@
|
|||||||
},
|
},
|
||||||
"generates": {
|
"generates": {
|
||||||
"./framework/reactioncommerce/schema.d.ts": {
|
"./framework/reactioncommerce/schema.d.ts": {
|
||||||
"plugins": ["typescript", "typescript-operations"]
|
"plugins": ["typescript", "typescript-operations"],
|
||||||
|
"config": {
|
||||||
|
"avoidOptionals": true
|
||||||
|
}
|
||||||
},
|
},
|
||||||
"./framework/reactioncommerce/schema.graphql": {
|
"./framework/reactioncommerce/schema.graphql": {
|
||||||
"plugins": ["schema-ast"]
|
"plugins": ["schema-ast"]
|
||||||
|
@ -8,15 +8,15 @@ import {
|
|||||||
CatalogItemProduct,
|
CatalogItemProduct,
|
||||||
CatalogProduct,
|
CatalogProduct,
|
||||||
ImageInfo,
|
ImageInfo,
|
||||||
Maybe,
|
CartItem,
|
||||||
} from '../schema'
|
} from '../schema'
|
||||||
|
|
||||||
import type {Cart, LineItem} from '../types'
|
import type {Cart, LineItem} from '../types'
|
||||||
|
|
||||||
const normalizeProductImages = (images: Maybe<ImageInfo>[], name: string) =>
|
const normalizeProductImages = (images: ImageInfo[], name: string) =>
|
||||||
images.map((image) => ({
|
images.map((image) => ({
|
||||||
url: image?.URLs?.original || image?.URLs?.medium || '',
|
url: image?.URLs?.original || image?.URLs?.medium || '',
|
||||||
alt: name,
|
alt: name
|
||||||
}))
|
}))
|
||||||
|
|
||||||
const normalizeProductOption = (variant: CatalogProductVariant) => {
|
const normalizeProductOption = (variant: CatalogProductVariant) => {
|
||||||
@ -38,7 +38,8 @@ function colorizeProductOptionValue(value: ProductOptionValues, displayName: str
|
|||||||
return value;
|
return value;
|
||||||
}
|
}
|
||||||
|
|
||||||
const normalizeProductVariants = (variants: CatalogProductVariant[]): ProductVariant[] => {
|
const normalizeProductVariants = (variants: Array<CatalogProductVariant>): ProductVariant[] => {
|
||||||
|
console.log(variants);
|
||||||
return variants.reduce((productVariants: ProductVariant[], variant: CatalogProductVariant) => {
|
return variants.reduce((productVariants: ProductVariant[], variant: CatalogProductVariant) => {
|
||||||
|
|
||||||
if (variantHasOptions(variant)) {
|
if (variantHasOptions(variant)) {
|
||||||
@ -141,14 +142,14 @@ export function normalizeProduct(productNode: CatalogItemProduct): Product {
|
|||||||
slug: slug?.replace(/^\/+|\/+$/g, '') ?? '',
|
slug: slug?.replace(/^\/+|\/+$/g, '') ?? '',
|
||||||
path: slug ?? '',
|
path: slug ?? '',
|
||||||
sku: sku ?? '',
|
sku: sku ?? '',
|
||||||
images: media?.length ? normalizeProductImages(media, title ?? '') : [],
|
images: media?.length ? normalizeProductImages(<ImageInfo[]>media, title ?? '') : [],
|
||||||
vendor: product.vendor,
|
vendor: product.vendor,
|
||||||
price: {
|
price: {
|
||||||
value: pricing[0]?.minPrice ?? 0,
|
value: pricing[0]?.minPrice ?? 0,
|
||||||
currencyCode: pricing[0]?.currency.code,
|
currencyCode: pricing[0]?.currency.code,
|
||||||
},
|
},
|
||||||
variants: variants !== null ? normalizeProductVariants(<CatalogProductVariant[]>variants) : [],
|
variants: !!variants ? normalizeProductVariants(<CatalogProductVariant[]>variants) : [],
|
||||||
options: variants !== null ? groupProductOptionsByAttributeLabel(<CatalogProductVariant[]>variants) : []
|
options: !!variants ? groupProductOptionsByAttributeLabel(<CatalogProductVariant[]>variants) : []
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -172,7 +173,6 @@ export function normalizeCart(cart: ReactionCart): Cart {
|
|||||||
|
|
||||||
function normalizeLineItem(cartItem: CartItemEdge): LineItem {
|
function normalizeLineItem(cartItem: CartItemEdge): LineItem {
|
||||||
const {
|
const {
|
||||||
node: {
|
|
||||||
_id,
|
_id,
|
||||||
compareAtPrice,
|
compareAtPrice,
|
||||||
imageURLs,
|
imageURLs,
|
||||||
@ -181,11 +181,10 @@ function normalizeLineItem(cartItem: CartItemEdge): LineItem {
|
|||||||
priceWhenAdded,
|
priceWhenAdded,
|
||||||
optionTitle,
|
optionTitle,
|
||||||
variantTitle,
|
variantTitle,
|
||||||
quantity,
|
quantity
|
||||||
}
|
} = <CartItem>cartItem.node
|
||||||
} = cartItem
|
|
||||||
|
|
||||||
console.log('imageURLs', imageURLs)
|
console.log('imageURLs', cartItem)
|
||||||
return {
|
return {
|
||||||
id: _id,
|
id: _id,
|
||||||
variantId: String(productConfiguration?.productVariantId),
|
variantId: String(productConfiguration?.productVariantId),
|
||||||
@ -201,7 +200,8 @@ function normalizeLineItem(cartItem: CartItemEdge): LineItem {
|
|||||||
},
|
},
|
||||||
requiresShipping: true,
|
requiresShipping: true,
|
||||||
price: priceWhenAdded?.amount,
|
price: priceWhenAdded?.amount,
|
||||||
listPrice: compareAtPrice?.amount,
|
listPrice: compareAtPrice?.amount ?? 0,
|
||||||
|
options: []
|
||||||
},
|
},
|
||||||
path: '',
|
path: '',
|
||||||
discounts: [],
|
discounts: [],
|
||||||
|
Loading…
x
Reference in New Issue
Block a user