diff --git a/framework/saleor/api/utils/fetch-graphql-api.ts b/framework/saleor/api/utils/fetch-graphql-api.ts index 324b1061c..465eacba7 100644 --- a/framework/saleor/api/utils/fetch-graphql-api.ts +++ b/framework/saleor/api/utils/fetch-graphql-api.ts @@ -11,11 +11,13 @@ const fetchGraphqlApi: GraphQLFetcher = async (query: string, { variables } = {} const config = getConfig() const token = getToken() - const res = await fetch(API_URL || '', { + const res = await fetch(API_URL!, { ...fetchOptions, method: 'POST', headers: { - Authorization: `Bearer ${token}`, + ...(token && { + Authorization: `Bearer ${token}`, + }), ...fetchOptions?.headers, 'Content-Type': 'application/json', }, diff --git a/framework/saleor/cart/use-remove-item.tsx b/framework/saleor/cart/use-remove-item.tsx index ce2ea491a..35cafaba0 100644 --- a/framework/saleor/cart/use-remove-item.tsx +++ b/framework/saleor/cart/use-remove-item.tsx @@ -27,7 +27,7 @@ export const handler = { lineId: itemId, }, }) - return checkoutToCart(data.checkoutLinesUpdate) + return checkoutToCart(data.checkoutLineDelete) }, useHook: ({ fetch }: MutationHookContext) => diff --git a/framework/saleor/utils/checkout-to-cart.ts b/framework/saleor/utils/checkout-to-cart.ts index 917fa8d0b..638ca815e 100644 --- a/framework/saleor/utils/checkout-to-cart.ts +++ b/framework/saleor/utils/checkout-to-cart.ts @@ -1,7 +1,7 @@ import { Cart } from '../types' import { CommerceError } from '@commerce/utils/errors' -import { CheckoutLinesAdd, CheckoutLinesUpdate, CheckoutCreate, CheckoutError, Checkout, Maybe } from '../schema' +import { CheckoutLinesAdd, CheckoutLinesUpdate, CheckoutCreate, CheckoutError, Checkout, Maybe, CheckoutLineDelete } from '../schema' import { normalizeCart } from './normalize' import throwUserErrors from './throw-user-errors' @@ -11,7 +11,7 @@ export type CheckoutQuery = { errors?: Array } -export type CheckoutPayload = CheckoutLinesAdd | CheckoutLinesUpdate | CheckoutCreate | CheckoutQuery +export type CheckoutPayload = CheckoutLinesAdd | CheckoutLinesUpdate | CheckoutCreate | CheckoutQuery | CheckoutLineDelete const checkoutToCart = (checkoutPayload?: Maybe): Cart => { if (!checkoutPayload) { diff --git a/framework/saleor/utils/fragments/checkout-details.ts b/framework/saleor/utils/fragments/checkout-details.ts index 1a5c44295..ccbe6a7ec 100644 --- a/framework/saleor/utils/fragments/checkout-details.ts +++ b/framework/saleor/utils/fragments/checkout-details.ts @@ -1,48 +1,48 @@ -export const CheckoutDetails = ` - id - token - created - - totalPrice { - currency - gross { - amount - } - } - subtotalPrice { - currency - gross { - amount - } - } - - lines { +export const CheckoutDetails = /* GraphQL */ ` + fragment CheckoutDetails on Checkout { id - variant { - id - name - sku - product { - slug - } - media { - url - } - pricing { - price { - gross { - amount - } - } - - } - } - quantity + token + created totalPrice { currency gross { amount } } + subtotalPrice { + currency + gross { + amount + } + } + + lines { + id + variant { + id + name + sku + product { + slug + } + media { + url + } + pricing { + price { + gross { + amount + } + } + } + } + quantity + totalPrice { + currency + gross { + amount + } + } + } } ` diff --git a/framework/saleor/utils/mutations/checkout-create.ts b/framework/saleor/utils/mutations/checkout-create.ts index 155b715fd..3cfe9d480 100644 --- a/framework/saleor/utils/mutations/checkout-create.ts +++ b/framework/saleor/utils/mutations/checkout-create.ts @@ -2,19 +2,16 @@ import * as fragment from '../fragments' export const CheckoutCreate = /* GraphQL */ ` mutation CheckoutCreate { - checkoutCreate(input: { - email: "customer@example.com", - lines: [], - channel: "default-channel" - }) { + checkoutCreate(input: { email: "customer@example.com", lines: [], channel: "default-channel" }) { errors { code field message } - checkout { - ${fragment.CheckoutDetails} + checkout { + ...CheckoutDetails } } } + ${fragment.CheckoutDetails} ` diff --git a/framework/saleor/utils/mutations/checkout-line-add.ts b/framework/saleor/utils/mutations/checkout-line-add.ts index d44fe5867..e7b8e0a27 100644 --- a/framework/saleor/utils/mutations/checkout-line-add.ts +++ b/framework/saleor/utils/mutations/checkout-line-add.ts @@ -9,8 +9,9 @@ export const CheckoutLineAdd = /* GraphQL */ ` message } checkout { - ${fragment.CheckoutDetails} + ...CheckoutDetails } } } + ${fragment.CheckoutDetails} ` diff --git a/framework/saleor/utils/mutations/checkout-line-remove.ts b/framework/saleor/utils/mutations/checkout-line-remove.ts index dea9379cd..191ef54aa 100644 --- a/framework/saleor/utils/mutations/checkout-line-remove.ts +++ b/framework/saleor/utils/mutations/checkout-line-remove.ts @@ -2,18 +2,16 @@ import * as fragment from '../fragments' export const CheckoutLineDelete = /* GraphQL */ ` mutation CheckoutLineDelete($checkoutId: ID!, $lineId: ID!) { - checkoutLineDelete( - checkoutId: $checkoutId - lineId: $lineId - ) { + checkoutLineDelete(checkoutId: $checkoutId, lineId: $lineId) { errors { code field message } checkout { - ${fragment.CheckoutDetails} + ...CheckoutDetails } } } + ${fragment.CheckoutDetails} ` diff --git a/framework/saleor/utils/mutations/checkout-line-update.ts b/framework/saleor/utils/mutations/checkout-line-update.ts index 0cb351dcc..50019d6ef 100644 --- a/framework/saleor/utils/mutations/checkout-line-update.ts +++ b/framework/saleor/utils/mutations/checkout-line-update.ts @@ -9,8 +9,9 @@ export const CheckoutLineUpdate = /* GraphQL */ ` message } checkout { - ${fragment.CheckoutDetails} + ...CheckoutDetails } } } + ${fragment.CheckoutDetails} ` diff --git a/framework/saleor/utils/normalize.ts b/framework/saleor/utils/normalize.ts index 494424d41..91188a65a 100644 --- a/framework/saleor/utils/normalize.ts +++ b/framework/saleor/utils/normalize.ts @@ -73,7 +73,10 @@ export function normalizeProduct(productNode: SaleorProduct): Product { description: description ? JSON.parse(description)?.blocks[0]?.data.text : '', path: `/${slug}`, slug: slug?.replace(/^\/+|\/+$/g, ''), - price: (pricing?.priceRange?.start?.net && money(pricing.priceRange.start.net)) || { value: 0, currencyCode: 'USD' }, + price: (pricing?.priceRange?.start?.net && money(pricing.priceRange.start.net)) || { + value: 0, + currencyCode: 'USD', + }, // TODO: Check nextjs-commerce bug if no images are added for a product images: media?.length ? media : [{ url: placeholderImg }], variants: variants && variants.length > 0 ? normalizeProductVariants(variants as ProductVariant[]) : [], diff --git a/framework/saleor/utils/queries/product-one-by-slug.ts b/framework/saleor/utils/queries/product-one-by-slug.ts index d1880c49b..e9169f05e 100644 --- a/framework/saleor/utils/queries/product-one-by-slug.ts +++ b/framework/saleor/utils/queries/product-one-by-slug.ts @@ -34,7 +34,7 @@ export const ProductOneBySlug = /* GraphQL */ ` } } } - images { + media { url alt }