saleor: simplify the naming for GraphQL statements

This commit is contained in:
Zaiste 2021-05-12 12:07:26 +02:00
parent 42ae126b7b
commit 2fe2c71fc3
No known key found for this signature in database
GPG Key ID: 15DF7EBC7F2FFE35
35 changed files with 76 additions and 110 deletions

View File

@ -1,8 +1,9 @@
import { useCallback } from 'react'
import type { MutationHook } from '@commerce/utils/types'
import { CommerceError } from '@commerce/utils/errors'
import useCustomer from '../customer/use-customer'
import tokenCreateMutation from '../utils/mutations/customer-access-token-create'
import * as mutation from '../utils/mutations'
import {
Mutation,
MutationTokenCreateArgs,
@ -14,7 +15,7 @@ export default useLogin as UseLogin<typeof handler>
export const handler: MutationHook<null, {}, MutationTokenCreateArgs> = {
fetchOptions: {
query: tokenCreateMutation,
query: mutation.sessionCreate,
},
async fetcher({ input: { email, password }, options, fetch }) {
if (!(email && password)) {

View File

@ -2,14 +2,14 @@ import { useCallback } from 'react'
import type { MutationHook } from '@commerce/utils/types'
import useLogout, { UseLogout } from '@commerce/auth/use-logout'
import useCustomer from '../customer/use-customer'
import customerAccessTokenDeleteMutation from '../utils/mutations/customer-access-token-delete'
import * as mutation from '../utils/mutations'
import { setToken } from '../utils/customer-token'
export default useLogout as UseLogout<typeof handler>
export const handler: MutationHook<null> = {
fetchOptions: {
query: customerAccessTokenDeleteMutation,
query: mutation.sessionDestroy,
},
async fetcher({ options, fetch }) {
await fetch({

View File

@ -9,7 +9,7 @@ import {
MutationAccountRegisterArgs
} from '../schema'
import { customerCreateMutation } from '../utils/mutations'
import * as mutation from '../utils/mutations'
import { handleAutomaticLogin, throwUserErrors } from '../utils'
export default useSignup as UseSignup<typeof handler>
@ -21,7 +21,7 @@ export const handler: MutationHook<
AccountRegisterInput
> = {
fetchOptions: {
query: customerCreateMutation,
query: mutation.accountRegister
},
async fetcher({
input: { email, password },

View File

@ -3,20 +3,21 @@ import type { MutationHook } from '@commerce/utils/types'
import { CommerceError } from '@commerce/utils/errors'
import useAddItem, { UseAddItem } from '@commerce/cart/use-add-item'
import useCart from './use-cart'
import * as mutation from '../utils/mutations'
import {
checkoutLineItemAddMutation,
getCheckoutId,
checkoutToCart,
} from '../utils'
import { Cart, CartItemBody } from '../types'
import { Mutation, MutationCheckoutLinesAddArgs } from '../schema'
export default useAddItem as UseAddItem<typeof handler>
export const handler: MutationHook<Cart, {}, CartItemBody> = {
fetchOptions: {
query: checkoutLineItemAddMutation,
},
fetchOptions: { query: mutation.checkoutLineAdd },
async fetcher({ input: item, options, fetch }) {
if (
item.quantity &&

View File

@ -7,7 +7,7 @@ import useCommerceCart, {
import { Cart } from '../types'
import { SWRHook } from '@commerce/utils/types'
import { checkoutCreate, checkoutToCart, getCheckoutId } from '../utils'
import getCheckoutQuery from '../utils/queries/get-checkout-query'
import { getCheckoutQuery } from '../utils/queries'
export default useCommerceCart as UseCart<typeof handler>

View File

@ -10,8 +10,8 @@ import useRemoveItem, {
UseRemoveItem,
} from '@commerce/cart/use-remove-item'
import useCart from './use-cart'
import * as mutation from '../utils/mutations'
import {
checkoutLineItemRemoveMutation,
getCheckoutId,
checkoutToCart,
} from '../utils'
@ -29,9 +29,7 @@ export type RemoveItemInput<T = any> = T extends LineItem
export default useRemoveItem as UseRemoveItem<typeof handler>
export const handler = {
fetchOptions: {
query: checkoutLineItemRemoveMutation,
},
fetchOptions: { query: mutation.checkoutLineDelete },
async fetcher({
input: { itemId },
options,

View File

@ -14,9 +14,11 @@ import useCart from './use-cart'
import { handler as removeItemHandler } from './use-remove-item'
import type { Cart, LineItem, UpdateCartItemBody } from '../types'
import { checkoutToCart } from '../utils'
import { getCheckoutId, checkoutLineItemUpdateMutation } from '../utils'
import { getCheckoutId } from '../utils'
import { Mutation, MutationCheckoutLinesUpdateArgs } from '../schema'
import * as mutation from '../utils/mutations'
export type UpdateItemInput<T = any> = T extends LineItem
? Partial<UpdateItemInputBase<LineItem>>
: UpdateItemInputBase<LineItem>
@ -24,9 +26,7 @@ export type UpdateItemInput<T = any> = T extends LineItem
export default useUpdateItem as UseUpdateItem<typeof handler>
export const handler = {
fetchOptions: {
query: checkoutLineItemUpdateMutation,
},
fetchOptions: { query: mutation.checkoutLineUpdate },
async fetcher({
input: { itemId, item },
options,

View File

@ -1,5 +1,5 @@
import { getConfig, SaleorConfig } from '../api'
import getPageQuery from '../utils/queries/get-page-query'
import { getPageQuery } from '../utils/queries'
import { Page } from './get-all-pages'
type Variables = {

View File

@ -1,5 +1,5 @@
import { getConfig, SaleorConfig } from '../api'
import getCustomerIdQuery from '../utils/queries/get-customer-id-query'
import { getCustomerIdQuery } from '../utils/queries'
import Cookies from 'js-cookie'
async function getCustomerId({

View File

@ -1,6 +1,6 @@
import { CollectionCountableEdge } from '../schema'
import { getConfig, SaleorConfig } from '../api'
import getAllCollectionsQuery from '../utils/queries/get-all-collections-query'
import { getSiteCollectionsQuery } from '../utils/queries'
const getAllCollections = async (options?: {
variables?: any
@ -10,7 +10,7 @@ const getAllCollections = async (options?: {
let { config, variables = { first: 100 } } = options ?? {}
config = getConfig(config)
const { data } = await config.fetch(getAllCollectionsQuery, { variables })
const { data } = await config.fetch(getSiteCollectionsQuery, { variables })
const edges = data.collections?.edges ?? []
const categories = edges.map(

View File

@ -1,7 +1,7 @@
import { getConfig, SaleorConfig } from '../api'
import fetchAllProducts from '../api/utils/fetch-all-products'
import { ProductCountableEdge } from '../schema'
import getAllProductsPathsQuery from '../utils/queries/get-all-products-paths-query'
import { getAllProductsPathsQuery } from '../utils/queries'
type ProductPath = {
path: string

View File

@ -1,16 +1,13 @@
import Cookies from 'js-cookie'
import checkoutCreateMutation from './mutations/checkout-create'
import * as mutation from './mutations'
import { CheckoutCreate } from '../schema'
import { CHECKOUT_ID_COOKIE } from '@framework/const'
export const checkoutCreate = async (
fetch: any
): Promise<CheckoutCreate> => {
const data = await fetch({
query: checkoutCreateMutation,
})
const data = await fetch({ query: mutation.checkoutCreate })
const checkout = data.checkoutCreate?.checkout
const checkoutId = checkout?.id
const checkoutToken = checkout?.token

View File

@ -1,6 +1,6 @@
import { SaleorConfig } from '../api'
import fetchAllProducts from '../api/utils/fetch-all-products'
import getAllProductVendors from './queries/get-all-product-vendors-query'
import { getAllProductVendors } from './queries'
export type Brand = {
entityId: string

View File

@ -1,7 +1,7 @@
import { FetcherOptions } from '@commerce/utils/types'
import { CreateToken, Mutation, MutationTokenCreateArgs } from '../schema'
import { setToken, setCSRFToken } from './customer-token'
import { customerAccessTokenCreateMutation } from './mutations'
import * as mutation from './mutations'
import throwUserErrors from './throw-user-errors'
const handleLogin = (data: CreateToken) => {
@ -26,7 +26,7 @@ export const handleAutomaticLogin = async (
Mutation,
MutationTokenCreateArgs
>({
query: customerAccessTokenCreateMutation,
query: mutation.sessionCreate,
variables: { ...input },
})
handleLogin(tokenCreate!)

View File

@ -1,18 +1,12 @@
const associateCustomerWithCheckoutMutation = /* GraphQl */ `
mutation associateCustomerWithCheckout($checkoutId: ID!, $customerAccessToken: String!) {
checkoutCustomerAssociateV2(checkoutId: $checkoutId, customerAccessToken: $customerAccessToken) {
checkout {
id
}
checkoutUserErrors {
code
field
export const checkoutCustomerAttach = /* GraphQl */ `
mutation associateCustomerWithCheckout($checkoutId: ID!) {
checkoutCustomerAttach(checkoutId: $checkoutId) {
errors {
message
}
customer {
checkout {
id
}
}
}
`
export default associateCustomerWithCheckoutMutation

View File

@ -1,6 +1,6 @@
import { checkoutDetailsFragment } from '../queries/get-checkout-query'
const checkoutCreateMutation = /* GraphQL */ `
export const checkoutCreate = /* GraphQL */ `
mutation createCheckout {
checkoutCreate(input: {
email: "customer@example.com",
@ -18,4 +18,3 @@ const checkoutCreateMutation = /* GraphQL */ `
}
}
`
export default checkoutCreateMutation

View File

@ -1,6 +1,6 @@
import { checkoutDetailsFragment } from '../queries/get-checkout-query'
const checkoutLineItemAddMutation = /* GraphQL */ `
export const checkoutLineAdd = /* GraphQL */ `
mutation checkoutLineItemAdd($checkoutId: ID!, $lineItems: [CheckoutLineInput!]!) {
checkoutLinesAdd(checkoutId: $checkoutId, lines: $lineItems) {
errors {
@ -14,4 +14,3 @@ const checkoutLineItemAddMutation = /* GraphQL */ `
}
}
`
export default checkoutLineItemAddMutation

View File

@ -1,6 +1,6 @@
import { checkoutDetailsFragment } from '../queries/get-checkout-query'
const checkoutLineItemRemoveMutation = /* GraphQL */ `
export const checkoutLineDelete = /* GraphQL */ `
mutation checkoutLineItemRemove($checkoutId: ID!, $lineId: ID!) {
checkoutLineDelete(
checkoutId: $checkoutId
@ -17,4 +17,3 @@ const checkoutLineItemRemoveMutation = /* GraphQL */ `
}
}
`
export default checkoutLineItemRemoveMutation

View File

@ -1,6 +1,6 @@
import { checkoutDetailsFragment } from '../queries/get-checkout-query'
const checkoutLineItemUpdateMutation = /* GraphQL */ `
export const checkoutLineUpdate = /* GraphQL */ `
mutation checkoutLineItemUpdate($checkoutId: ID!, $lineItems: [CheckoutLineInput!]!) {
checkoutLinesUpdate(checkoutId: $checkoutId, lines: $lineItems) {
errors {
@ -14,4 +14,3 @@ const checkoutLineItemUpdateMutation = /* GraphQL */ `
}
}
`
export default checkoutLineItemUpdateMutation

View File

@ -1,4 +1,4 @@
const tokenCreateMutation = /* GraphQL */ `
export const sessionCreate = /* GraphQL */ `
mutation tokenCreate($email: String!, $password: String!) {
tokenCreate(email: $email, password: $password) {
token
@ -12,4 +12,3 @@ const tokenCreateMutation = /* GraphQL */ `
}
}
`
export default tokenCreateMutation;

View File

@ -1,4 +1,4 @@
const customerAccessTokenDeleteMutation = /* GraphQL */ `
export const sessionDestroy = /* GraphQL */ `
mutation customerAccessTokenDelete {
tokensDeactivateAll {
errors {
@ -8,5 +8,3 @@ const customerAccessTokenDeleteMutation = /* GraphQL */ `
}
}
`
export default customerAccessTokenDeleteMutation

View File

@ -1,4 +1,4 @@
const customerCreateMutation = /* GraphQL */ `
export const accountRegister = /* GraphQL */ `
mutation customerCreate($input: AccountRegisterInput!) {
accountRegister(input: $input) {
errors {
@ -13,4 +13,3 @@ const customerCreateMutation = /* GraphQL */ `
}
}
`
export default customerCreateMutation

View File

@ -1,7 +1,7 @@
export { default as customerCreateMutation } from './customer-create'
export { default as checkoutCreateMutation } from './checkout-create'
export { default as checkoutLineItemAddMutation } from './checkout-line-item-add'
export { default as checkoutLineItemUpdateMutation } from './checkout-line-item-update'
export { default as checkoutLineItemRemoveMutation } from './checkout-line-item-remove'
export { default as customerAccessTokenCreateMutation } from './customer-access-token-create'
export { default as customerAccessTokenDeleteMutation } from './customer-access-token-delete'
export { accountRegister } from './customer-create'
export { checkoutCreate } from './checkout-create'
export { checkoutLineAdd } from './checkout-line-item-add'
export { checkoutLineUpdate } from './checkout-line-item-update'
export { checkoutLineDelete } from './checkout-line-item-remove'
export { sessionCreate } from './customer-access-token-create'
export { sessionDestroy } from './customer-access-token-delete'

View File

@ -1,4 +1,4 @@
const getSiteCollectionsQuery = /* GraphQL */ `
export const getSiteCollectionsQuery = /* GraphQL */ `
query getSiteCollections($first: Int!, $channel: String = "default-channel") {
collections(first: $first, channel: $channel) {
edges {
@ -11,4 +11,3 @@ const getSiteCollectionsQuery = /* GraphQL */ `
}
}
`
export default getSiteCollectionsQuery

View File

@ -10,5 +10,4 @@ export const getAllPagesQuery = /* GraphQL */ `
}
}
}
`
export default getAllPagesQuery
`

View File

@ -1,4 +1,4 @@
const getAllProductVendors = /* GraphQL */ `
export const getAllProductVendors = /* GraphQL */ `
query getAllProductVendors($first: Int = 250, $cursor: String) {
products(first: $first, after: $cursor) {
pageInfo {
@ -13,5 +13,4 @@ const getAllProductVendors = /* GraphQL */ `
}
}
}
`
export default getAllProductVendors
`

View File

@ -1,4 +1,4 @@
const getAllProductsPathsQuery = /* GraphQL */ `
export const getAllProductsPathsQuery = /* GraphQL */ `
query getAllProductPaths(
$first: Int = 100
$cursor: String
@ -18,4 +18,3 @@ const getAllProductsPathsQuery = /* GraphQL */ `
}
}
`
export default getAllProductsPathsQuery

View File

@ -28,7 +28,7 @@ export const productConnection = /* GraphQL */ `
}
`
const getAllProductsQuery = /* GraphQL */ `
export const getAllProductsQuery = /* GraphQL */ `
query getAllProducts(
$first: Int = 100
$filter: ProductFilterInput
@ -40,4 +40,3 @@ const getAllProductsQuery = /* GraphQL */ `
}
${productConnection}
`
export default getAllProductsQuery

View File

@ -47,7 +47,7 @@ export const checkoutDetailsFragment = `
}
`
const getCheckoutQuery = /* GraphQL */ `
export const getCheckoutQuery = /* GraphQL */ `
query($checkoutId: UUID!) {
checkout(token: $checkoutId) {
... on Checkout {
@ -56,4 +56,3 @@ const getCheckoutQuery = /* GraphQL */ `
}
}
`
export default getCheckoutQuery

View File

@ -1,6 +1,6 @@
import { productConnection } from './get-all-products-query'
const getCollectionProductsQuery = /* GraphQL */ `
export const getCollectionProductsQuery = /* GraphQL */ `
query getProductsFromCollection(
$categoryId: ID!
$first: Int = 250
@ -21,4 +21,3 @@ const getCollectionProductsQuery = /* GraphQL */ `
}
}
`
export default getCollectionProductsQuery

View File

@ -1,8 +1,7 @@
export const getCustomerQuery = /* GraphQL */ `
export const getCustomerIdQuery = /* GraphQL */ `
query getCustomerId($customerAccessToken: String!) {
customer(customerAccessToken: $customerAccessToken) {
id
}
}
`
export default getCustomerQuery

View File

@ -1,19 +1,11 @@
// FIXME move to `mutations/` @zaiste
export const getCustomerQuery = /* GraphQL */ `
mutation getCustomer($customerAccessToken: String!) {
tokenRefresh(csrfToken: $customerAccessToken) {
token
user {
id
email
firstName
lastName
dateJoined
}
errors {
code
message
}
query getCustomer {
me {
id
email
firstName
lastName
dateJoined
}
}
`

View File

@ -7,4 +7,3 @@ export const getPageQuery = /* GraphQL */ `
}
}
`
export default getPageQuery

View File

@ -1,4 +1,4 @@
const getProductQuery = /* GraphQL */ `
export const getProductQuery = /* GraphQL */ `
query getProductBySlug($slug: String!, $channel: String = "default-channel") {
product(slug: $slug, channel: $channel) {
id
@ -42,4 +42,3 @@ const getProductQuery = /* GraphQL */ `
}
`
export default getProductQuery

View File

@ -1,10 +1,11 @@
export { default as getSiteCollectionsQuery } from './get-all-collections-query'
export { default as getProductQuery } from './get-product-query'
export { default as getAllProductsQuery } from './get-all-products-query'
export { default as getAllProductsPathtsQuery } from './get-all-products-paths-query'
export { default as getAllProductVendors } from './get-all-product-vendors-query'
export { default as getCollectionProductsQuery } from './get-collection-products-query'
export { default as getCheckoutQuery } from './get-checkout-query'
export { default as getAllPagesQuery } from './get-all-pages-query'
export { default as getPageQuery } from './get-page-query'
export { default as getCustomerQuery } from './get-customer-query'
export { getSiteCollectionsQuery } from './get-all-collections-query'
export { getProductQuery } from './get-product-query'
export { getAllProductsQuery } from './get-all-products-query'
export { getAllProductsPathsQuery } from './get-all-products-paths-query'
export { getAllProductVendors } from './get-all-product-vendors-query'
export { getCollectionProductsQuery } from './get-collection-products-query'
export { getCheckoutQuery } from './get-checkout-query'
export { getAllPagesQuery } from './get-all-pages-query'
export { getPageQuery } from './get-page-query'
export { getCustomerQuery } from './get-customer-query'
export { getCustomerIdQuery } from './get-customer-id-query'