saleor: refactor GraphQL queries

This commit is contained in:
Zaiste 2021-05-20 12:03:14 +02:00
parent 5f41b6a057
commit 1959647846
No known key found for this signature in database
GPG Key ID: 15DF7EBC7F2FFE35
10 changed files with 62 additions and 60 deletions

View File

@ -11,11 +11,13 @@ const fetchGraphqlApi: GraphQLFetcher = async (query: string, { variables } = {}
const config = getConfig() const config = getConfig()
const token = getToken() const token = getToken()
const res = await fetch(API_URL || '', { const res = await fetch(API_URL!, {
...fetchOptions, ...fetchOptions,
method: 'POST', method: 'POST',
headers: { headers: {
Authorization: `Bearer ${token}`, ...(token && {
Authorization: `Bearer ${token}`,
}),
...fetchOptions?.headers, ...fetchOptions?.headers,
'Content-Type': 'application/json', 'Content-Type': 'application/json',
}, },

View File

@ -27,7 +27,7 @@ export const handler = {
lineId: itemId, lineId: itemId,
}, },
}) })
return checkoutToCart(data.checkoutLinesUpdate) return checkoutToCart(data.checkoutLineDelete)
}, },
useHook: useHook:
({ fetch }: MutationHookContext<Cart | null, RemoveCartItemBody>) => ({ fetch }: MutationHookContext<Cart | null, RemoveCartItemBody>) =>

View File

@ -1,7 +1,7 @@
import { Cart } from '../types' import { Cart } from '../types'
import { CommerceError } from '@commerce/utils/errors' 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 { normalizeCart } from './normalize'
import throwUserErrors from './throw-user-errors' import throwUserErrors from './throw-user-errors'
@ -11,7 +11,7 @@ export type CheckoutQuery = {
errors?: Array<CheckoutError> errors?: Array<CheckoutError>
} }
export type CheckoutPayload = CheckoutLinesAdd | CheckoutLinesUpdate | CheckoutCreate | CheckoutQuery export type CheckoutPayload = CheckoutLinesAdd | CheckoutLinesUpdate | CheckoutCreate | CheckoutQuery | CheckoutLineDelete
const checkoutToCart = (checkoutPayload?: Maybe<CheckoutPayload>): Cart => { const checkoutToCart = (checkoutPayload?: Maybe<CheckoutPayload>): Cart => {
if (!checkoutPayload) { if (!checkoutPayload) {

View File

@ -1,48 +1,48 @@
export const CheckoutDetails = ` export const CheckoutDetails = /* GraphQL */ `
id fragment CheckoutDetails on Checkout {
token
created
totalPrice {
currency
gross {
amount
}
}
subtotalPrice {
currency
gross {
amount
}
}
lines {
id id
variant { token
id created
name
sku
product {
slug
}
media {
url
}
pricing {
price {
gross {
amount
}
}
}
}
quantity
totalPrice { totalPrice {
currency currency
gross { gross {
amount amount
} }
} }
subtotalPrice {
currency
gross {
amount
}
}
lines {
id
variant {
id
name
sku
product {
slug
}
media {
url
}
pricing {
price {
gross {
amount
}
}
}
}
quantity
totalPrice {
currency
gross {
amount
}
}
}
} }
` `

View File

@ -2,19 +2,16 @@ import * as fragment from '../fragments'
export const CheckoutCreate = /* GraphQL */ ` export const CheckoutCreate = /* GraphQL */ `
mutation CheckoutCreate { mutation CheckoutCreate {
checkoutCreate(input: { checkoutCreate(input: { email: "customer@example.com", lines: [], channel: "default-channel" }) {
email: "customer@example.com",
lines: [],
channel: "default-channel"
}) {
errors { errors {
code code
field field
message message
} }
checkout { checkout {
${fragment.CheckoutDetails} ...CheckoutDetails
} }
} }
} }
${fragment.CheckoutDetails}
` `

View File

@ -9,8 +9,9 @@ export const CheckoutLineAdd = /* GraphQL */ `
message message
} }
checkout { checkout {
${fragment.CheckoutDetails} ...CheckoutDetails
} }
} }
} }
${fragment.CheckoutDetails}
` `

View File

@ -2,18 +2,16 @@ import * as fragment from '../fragments'
export const CheckoutLineDelete = /* GraphQL */ ` export const CheckoutLineDelete = /* GraphQL */ `
mutation CheckoutLineDelete($checkoutId: ID!, $lineId: ID!) { mutation CheckoutLineDelete($checkoutId: ID!, $lineId: ID!) {
checkoutLineDelete( checkoutLineDelete(checkoutId: $checkoutId, lineId: $lineId) {
checkoutId: $checkoutId
lineId: $lineId
) {
errors { errors {
code code
field field
message message
} }
checkout { checkout {
${fragment.CheckoutDetails} ...CheckoutDetails
} }
} }
} }
${fragment.CheckoutDetails}
` `

View File

@ -9,8 +9,9 @@ export const CheckoutLineUpdate = /* GraphQL */ `
message message
} }
checkout { checkout {
${fragment.CheckoutDetails} ...CheckoutDetails
} }
} }
} }
${fragment.CheckoutDetails}
` `

View File

@ -73,7 +73,10 @@ export function normalizeProduct(productNode: SaleorProduct): Product {
description: description ? JSON.parse(description)?.blocks[0]?.data.text : '', description: description ? JSON.parse(description)?.blocks[0]?.data.text : '',
path: `/${slug}`, path: `/${slug}`,
slug: slug?.replace(/^\/+|\/+$/g, ''), 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 // TODO: Check nextjs-commerce bug if no images are added for a product
images: media?.length ? media : [{ url: placeholderImg }], images: media?.length ? media : [{ url: placeholderImg }],
variants: variants && variants.length > 0 ? normalizeProductVariants(variants as ProductVariant[]) : [], variants: variants && variants.length > 0 ? normalizeProductVariants(variants as ProductVariant[]) : [],

View File

@ -34,7 +34,7 @@ export const ProductOneBySlug = /* GraphQL */ `
} }
} }
} }
images { media {
url url
alt alt
} }