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

View File

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

View File

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

View File

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

View File

@ -7,7 +7,7 @@ import useCommerceCart, {
import { Cart } from '../types' import { Cart } from '../types'
import { SWRHook } from '@commerce/utils/types' import { SWRHook } from '@commerce/utils/types'
import { checkoutCreate, checkoutToCart, getCheckoutId } from '../utils' 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> export default useCommerceCart as UseCart<typeof handler>

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -1,6 +1,6 @@
import { checkoutDetailsFragment } from '../queries/get-checkout-query' import { checkoutDetailsFragment } from '../queries/get-checkout-query'
const checkoutCreateMutation = /* GraphQL */ ` export const checkoutCreate = /* GraphQL */ `
mutation createCheckout { mutation createCheckout {
checkoutCreate(input: { checkoutCreate(input: {
email: "customer@example.com", 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' import { checkoutDetailsFragment } from '../queries/get-checkout-query'
const checkoutLineItemAddMutation = /* GraphQL */ ` export const checkoutLineAdd = /* GraphQL */ `
mutation checkoutLineItemAdd($checkoutId: ID!, $lineItems: [CheckoutLineInput!]!) { mutation checkoutLineItemAdd($checkoutId: ID!, $lineItems: [CheckoutLineInput!]!) {
checkoutLinesAdd(checkoutId: $checkoutId, lines: $lineItems) { checkoutLinesAdd(checkoutId: $checkoutId, lines: $lineItems) {
errors { errors {
@ -14,4 +14,3 @@ const checkoutLineItemAddMutation = /* GraphQL */ `
} }
} }
` `
export default checkoutLineItemAddMutation

View File

@ -1,6 +1,6 @@
import { checkoutDetailsFragment } from '../queries/get-checkout-query' import { checkoutDetailsFragment } from '../queries/get-checkout-query'
const checkoutLineItemRemoveMutation = /* GraphQL */ ` export const checkoutLineDelete = /* GraphQL */ `
mutation checkoutLineItemRemove($checkoutId: ID!, $lineId: ID!) { mutation checkoutLineItemRemove($checkoutId: ID!, $lineId: ID!) {
checkoutLineDelete( checkoutLineDelete(
checkoutId: $checkoutId 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' import { checkoutDetailsFragment } from '../queries/get-checkout-query'
const checkoutLineItemUpdateMutation = /* GraphQL */ ` export const checkoutLineUpdate = /* GraphQL */ `
mutation checkoutLineItemUpdate($checkoutId: ID!, $lineItems: [CheckoutLineInput!]!) { mutation checkoutLineItemUpdate($checkoutId: ID!, $lineItems: [CheckoutLineInput!]!) {
checkoutLinesUpdate(checkoutId: $checkoutId, lines: $lineItems) { checkoutLinesUpdate(checkoutId: $checkoutId, lines: $lineItems) {
errors { 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!) { mutation tokenCreate($email: String!, $password: String!) {
tokenCreate(email: $email, password: $password) { tokenCreate(email: $email, password: $password) {
token 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 { mutation customerAccessTokenDelete {
tokensDeactivateAll { tokensDeactivateAll {
errors { 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!) { mutation customerCreate($input: AccountRegisterInput!) {
accountRegister(input: $input) { accountRegister(input: $input) {
errors { 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 { accountRegister } from './customer-create'
export { default as checkoutCreateMutation } from './checkout-create' export { checkoutCreate } from './checkout-create'
export { default as checkoutLineItemAddMutation } from './checkout-line-item-add' export { checkoutLineAdd } from './checkout-line-item-add'
export { default as checkoutLineItemUpdateMutation } from './checkout-line-item-update' export { checkoutLineUpdate } from './checkout-line-item-update'
export { default as checkoutLineItemRemoveMutation } from './checkout-line-item-remove' export { checkoutLineDelete } from './checkout-line-item-remove'
export { default as customerAccessTokenCreateMutation } from './customer-access-token-create' export { sessionCreate } from './customer-access-token-create'
export { default as customerAccessTokenDeleteMutation } from './customer-access-token-delete' 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") { query getSiteCollections($first: Int!, $channel: String = "default-channel") {
collections(first: $first, channel: $channel) { collections(first: $first, channel: $channel) {
edges { 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) { query getAllProductVendors($first: Int = 250, $cursor: String) {
products(first: $first, after: $cursor) { products(first: $first, after: $cursor) {
pageInfo { 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( query getAllProductPaths(
$first: Int = 100 $first: Int = 100
$cursor: String $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( query getAllProducts(
$first: Int = 100 $first: Int = 100
$filter: ProductFilterInput $filter: ProductFilterInput
@ -40,4 +40,3 @@ const getAllProductsQuery = /* GraphQL */ `
} }
${productConnection} ${productConnection}
` `
export default getAllProductsQuery

View File

@ -47,7 +47,7 @@ export const checkoutDetailsFragment = `
} }
` `
const getCheckoutQuery = /* GraphQL */ ` export const getCheckoutQuery = /* GraphQL */ `
query($checkoutId: UUID!) { query($checkoutId: UUID!) {
checkout(token: $checkoutId) { checkout(token: $checkoutId) {
... on Checkout { ... 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' import { productConnection } from './get-all-products-query'
const getCollectionProductsQuery = /* GraphQL */ ` export const getCollectionProductsQuery = /* GraphQL */ `
query getProductsFromCollection( query getProductsFromCollection(
$categoryId: ID! $categoryId: ID!
$first: Int = 250 $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!) { query getCustomerId($customerAccessToken: String!) {
customer(customerAccessToken: $customerAccessToken) { customer(customerAccessToken: $customerAccessToken) {
id id
} }
} }
` `
export default getCustomerQuery

View File

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

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") { query getProductBySlug($slug: String!, $channel: String = "default-channel") {
product(slug: $slug, channel: $channel) { product(slug: $slug, channel: $channel) {
id 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 { getSiteCollectionsQuery } from './get-all-collections-query'
export { default as getProductQuery } from './get-product-query' export { getProductQuery } from './get-product-query'
export { default as getAllProductsQuery } from './get-all-products-query' export { getAllProductsQuery } from './get-all-products-query'
export { default as getAllProductsPathtsQuery } from './get-all-products-paths-query' export { getAllProductsPathsQuery } from './get-all-products-paths-query'
export { default as getAllProductVendors } from './get-all-product-vendors-query' export { getAllProductVendors } from './get-all-product-vendors-query'
export { default as getCollectionProductsQuery } from './get-collection-products-query' export { getCollectionProductsQuery } from './get-collection-products-query'
export { default as getCheckoutQuery } from './get-checkout-query' export { getCheckoutQuery } from './get-checkout-query'
export { default as getAllPagesQuery } from './get-all-pages-query' export { getAllPagesQuery } from './get-all-pages-query'
export { default as getPageQuery } from './get-page-query' export { getPageQuery } from './get-page-query'
export { default as getCustomerQuery } from './get-customer-query' export { getCustomerQuery } from './get-customer-query'
export { getCustomerIdQuery } from './get-customer-id-query'