From c6df70c34c90dde6bfc7e7297ccf8665ebf43643 Mon Sep 17 00:00:00 2001 From: tedraykov Date: Sun, 23 May 2021 19:15:01 +0300 Subject: [PATCH] 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. --- framework/commerce/types.ts | 16 +++---- .../api/cart/handlers/get-cart.ts | 7 ++- framework/reactioncommerce/codegen.json | 5 ++- framework/reactioncommerce/utils/normalize.ts | 44 +++++++++---------- 4 files changed, 37 insertions(+), 35 deletions(-) diff --git a/framework/commerce/types.ts b/framework/commerce/types.ts index 86361fd9f..7bf44e774 100644 --- a/framework/commerce/types.ts +++ b/framework/commerce/types.ts @@ -63,6 +63,7 @@ export type ProductVariant = { // The variant's depth. If a depth was not explicitly specified on the // variant, this will be the product's depth. depth?: Measurement + options: ProductOption[] } // Shopping cart, a.k.a Checkout @@ -109,6 +110,10 @@ export type CartItemBody = { variantId: string productId?: string quantity?: number + pricing?: { + amount: number, + currencyCode: string, + } } // Body used by the `getCart` operation handler @@ -167,18 +172,18 @@ export interface Product extends Entity { slug?: string path?: string images: ProductImage[] - variants: ProductVariant2[] + variants: ProductVariant[] price: ProductPrice options: ProductOption[] sku?: string } -interface ProductOption extends Entity { +export interface ProductOption extends Entity { displayName: string values: ProductOptionValues[] } -interface ProductOptionValues { +export interface ProductOptionValues { label: string hexColors?: string[] } @@ -188,11 +193,6 @@ interface ProductImage { alt?: string } -interface ProductVariant2 { - id: string | number - options: ProductOption[] -} - interface ProductPrice { value: number currencyCode: 'USD' | 'ARS' | string | undefined diff --git a/framework/reactioncommerce/api/cart/handlers/get-cart.ts b/framework/reactioncommerce/api/cart/handlers/get-cart.ts index a6f2e58e7..f322bb669 100644 --- a/framework/reactioncommerce/api/cart/handlers/get-cart.ts +++ b/framework/reactioncommerce/api/cart/handlers/get-cart.ts @@ -1,6 +1,5 @@ -import type { Cart } from '../../../types' 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 getCartCookie from '@framework/api/utils/get-cart-cookie' import reconcileCarts from '@framework/api/utils/reconcile-carts' @@ -9,7 +8,7 @@ import { REACTION_ANONYMOUS_CART_TOKEN_COOKIE, REACTION_CART_ID_COOKIE, REACTION_CUSTOMER_TOKEN_COOKIE, -} from '@framework/const.ts' +} from '@framework/const' import { normalizeCart } from '@framework/utils' // Return current cart info @@ -42,7 +41,7 @@ const getCart: CartHandlers['getCart'] = async ({ req, res, config }) => { } else if (cartId && anonymousCartToken) { const { data: { cart: rawAnonymousCart }, - } = await config.fetch(getAnomymousCartQuery, { + } = await config.fetch(getAnonymousCartQuery, { variables: { cartId, cartToken: anonymousCartToken, diff --git a/framework/reactioncommerce/codegen.json b/framework/reactioncommerce/codegen.json index 56167f954..5679e67fd 100644 --- a/framework/reactioncommerce/codegen.json +++ b/framework/reactioncommerce/codegen.json @@ -6,7 +6,10 @@ }, "generates": { "./framework/reactioncommerce/schema.d.ts": { - "plugins": ["typescript", "typescript-operations"] + "plugins": ["typescript", "typescript-operations"], + "config": { + "avoidOptionals": true + } }, "./framework/reactioncommerce/schema.graphql": { "plugins": ["schema-ast"] diff --git a/framework/reactioncommerce/utils/normalize.ts b/framework/reactioncommerce/utils/normalize.ts index c97eb6185..982444f08 100644 --- a/framework/reactioncommerce/utils/normalize.ts +++ b/framework/reactioncommerce/utils/normalize.ts @@ -8,15 +8,15 @@ import { CatalogItemProduct, CatalogProduct, ImageInfo, - Maybe, + CartItem, } from '../schema' import type {Cart, LineItem} from '../types' -const normalizeProductImages = (images: Maybe[], name: string) => +const normalizeProductImages = (images: ImageInfo[], name: string) => images.map((image) => ({ url: image?.URLs?.original || image?.URLs?.medium || '', - alt: name, + alt: name })) const normalizeProductOption = (variant: CatalogProductVariant) => { @@ -38,7 +38,8 @@ function colorizeProductOptionValue(value: ProductOptionValues, displayName: str return value; } -const normalizeProductVariants = (variants: CatalogProductVariant[]): ProductVariant[] => { +const normalizeProductVariants = (variants: Array): ProductVariant[] => { + console.log(variants); return variants.reduce((productVariants: ProductVariant[], variant: CatalogProductVariant) => { if (variantHasOptions(variant)) { @@ -141,14 +142,14 @@ export function normalizeProduct(productNode: CatalogItemProduct): Product { slug: slug?.replace(/^\/+|\/+$/g, '') ?? '', path: slug ?? '', sku: sku ?? '', - images: media?.length ? normalizeProductImages(media, title ?? '') : [], + images: media?.length ? normalizeProductImages(media, title ?? '') : [], vendor: product.vendor, price: { value: pricing[0]?.minPrice ?? 0, currencyCode: pricing[0]?.currency.code, }, - variants: variants !== null ? normalizeProductVariants(variants) : [], - options: variants !== null ? groupProductOptionsByAttributeLabel(variants) : [] + variants: !!variants ? normalizeProductVariants(variants) : [], + options: !!variants ? groupProductOptionsByAttributeLabel(variants) : [] } } @@ -172,20 +173,18 @@ export function normalizeCart(cart: ReactionCart): Cart { function normalizeLineItem(cartItem: CartItemEdge): LineItem { const { - node: { - _id, - compareAtPrice, - imageURLs, - title, - productConfiguration, - priceWhenAdded, - optionTitle, - variantTitle, - quantity, - } - } = cartItem + _id, + compareAtPrice, + imageURLs, + title, + productConfiguration, + priceWhenAdded, + optionTitle, + variantTitle, + quantity + } = cartItem.node - console.log('imageURLs', imageURLs) + console.log('imageURLs', cartItem) return { id: _id, variantId: String(productConfiguration?.productVariantId), @@ -197,11 +196,12 @@ function normalizeLineItem(cartItem: CartItemEdge): LineItem { sku: String(productConfiguration?.productVariantId), name: String(optionTitle || variantTitle), image: { - url: imageURLs?.thumbnail ?? '/product-img-placeholder.svg', + url: imageURLs?.thumbnail ?? '/product-img-placeholder.svg', }, requiresShipping: true, price: priceWhenAdded?.amount, - listPrice: compareAtPrice?.amount, + listPrice: compareAtPrice?.amount ?? 0, + options: [] }, path: '', discounts: [],