saleor: unify GraphQL naming approach

This commit is contained in:
Zaiste 2021-05-14 15:57:45 +02:00
parent df3d85f86e
commit f530704efe
No known key found for this signature in database
GPG Key ID: 15DF7EBC7F2FFE35
50 changed files with 217 additions and 216 deletions

View File

@ -15,7 +15,7 @@ export default useLogin as UseLogin<typeof handler>
export const handler: MutationHook<null, {}, MutationTokenCreateArgs> = {
fetchOptions: {
query: mutation.sessionCreate,
query: mutation.SessionCreate,
},
async fetcher({ input: { email, password }, options, fetch }) {
if (!(email && password)) {

View File

@ -9,7 +9,7 @@ export default useLogout as UseLogout<typeof handler>
export const handler: MutationHook<null> = {
fetchOptions: {
query: mutation.sessionDestroy,
query: mutation.SessionDestroy,
},
async fetcher({ options, fetch }) {
await fetch({

View File

@ -21,7 +21,7 @@ export const handler: MutationHook<
AccountRegisterInput
> = {
fetchOptions: {
query: mutation.accountRegister
query: mutation.AccountCreate
},
async fetcher({
input: { email, password },

View File

@ -17,7 +17,7 @@ import { Mutation, MutationCheckoutLinesAddArgs } from '../schema'
export default useAddItem as UseAddItem<typeof handler>
export const handler: MutationHook<Cart, {}, CartItemBody> = {
fetchOptions: { query: mutation.checkoutLineAdd },
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'
import * as query from '../utils/queries'
export default useCommerceCart as UseCart<typeof handler>
@ -18,7 +18,7 @@ export const handler: SWRHook<
{ isEmpty?: boolean }
> = {
fetchOptions: {
query: getCheckoutQuery,
query: query.CheckoutOne,
},
async fetcher({ input: { cartId: checkoutId }, options, fetch }) {
let checkout

View File

@ -29,7 +29,7 @@ export type RemoveItemInput<T = any> = T extends LineItem
export default useRemoveItem as UseRemoveItem<typeof handler>
export const handler = {
fetchOptions: { query: mutation.checkoutLineDelete },
fetchOptions: { query: mutation.CheckoutLineDelete },
async fetcher({
input: { itemId },
options,

View File

@ -26,7 +26,7 @@ export type UpdateItemInput<T = any> = T extends LineItem
export default useUpdateItem as UseUpdateItem<typeof handler>
export const handler = {
fetchOptions: { query: mutation.checkoutLineUpdate },
fetchOptions: { query: mutation.CheckoutLineUpdate },
async fetcher({
input: { itemId, item },
options,

View File

@ -1,6 +1,6 @@
import { getConfig, SaleorConfig } from '../api'
import { PageCountableEdge } from '../schema'
import { getAllPagesQuery } from '../utils/queries'
import * as query from '../utils/queries'
type Variables = {
first?: number
@ -26,7 +26,7 @@ const getAllPages = async (options?: {
let { config, variables = { first: 100 } } = options ?? {}
config = getConfig(config)
const { locale } = config
const { data } = await config.fetch(getAllPagesQuery, { variables })
const { data } = await config.fetch(query.PageMany, { variables })
const pages = data.pages?.edges?.map(
({ node: { title: name, slug, ...node } }: PageCountableEdge) => ({

View File

@ -1,7 +1,8 @@
import { getConfig, SaleorConfig } from '../api'
import { getPageQuery } from '../utils/queries'
import { Page } from './get-all-pages'
import * as query from '../utils/queries'
type Variables = {
id: string
}
@ -18,9 +19,7 @@ const getPage = async (options: {
config = getConfig(config)
const { locale } = config
const { data } = await config.fetch(getPageQuery, {
variables,
})
const { data } = await config.fetch(query.PageOne, { variables })
const page = data.page
return {

View File

@ -1,5 +1,5 @@
import { getConfig, SaleorConfig } from '../api'
import { getCustomerIdQuery } from '../utils/queries'
import * as query from '../utils/queries'
import Cookies from 'js-cookie'
async function getCustomerId({
@ -11,7 +11,7 @@ async function getCustomerId({
}): Promise<number | undefined> {
config = getConfig(config)
const { data } = await config.fetch(getCustomerIdQuery, {
const { data } = await config.fetch(query.CustomerOne, {
variables: {
customerAccesToken:
customerAccesToken || Cookies.get(config.customerCookie),

View File

@ -1,13 +1,14 @@
import useCustomer, { UseCustomer } from '@commerce/customer/use-customer'
import { Customer } from '@commerce/types'
import { SWRHook } from '@commerce/utils/types'
import { getCustomerQuery } from '../utils'
import * as query from '../utils/queries'
export default useCustomer as UseCustomer<typeof handler>
export const handler: SWRHook<Customer | null> = {
fetchOptions: {
query: getCustomerQuery,
query: query.CustomerCurrent,
},
async fetcher({ options, fetch }) {
const data = await fetch<any | null>({

View File

@ -1,6 +1,6 @@
import { CollectionCountableEdge } from '../schema'
import { getConfig, SaleorConfig } from '../api'
import { getSiteCollectionsQuery } from '../utils/queries'
import * as query 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(getSiteCollectionsQuery, { variables })
const { data } = await config.fetch(query.CollectionMany, { variables })
const edges = data.collections?.edges ?? []
const categories = edges.map(

View File

@ -1,9 +1,11 @@
import { GraphQLFetcherResult } from '@commerce/api'
import { Product } from '@commerce/types'
import { getConfig, SaleorConfig } from '../api'
import { ProductCountableEdge } from '../schema'
import { getAllProductsQuery } from '../utils/queries'
import { normalizeProduct } from '../utils/normalize'
import { Product } from '@commerce/types'
import * as query from '../utils/queries'
type Variables = {
first?: number
@ -22,10 +24,7 @@ const getAllProducts = async (options: {
let { config, variables = { first: 100 } } = options ?? {}
config = getConfig(config)
const { data }: GraphQLFetcherResult = await config.fetch(
getAllProductsQuery,
{ variables }
)
const { data }: GraphQLFetcherResult = await config.fetch(query.ProductMany, { variables })
const products =
data.products?.edges?.map(({ node: p }: ProductCountableEdge) =>

View File

@ -1,6 +1,7 @@
import { GraphQLFetcherResult } from '@commerce/api'
import { getConfig, SaleorConfig } from '../api'
import { normalizeProduct, getProductQuery } from '../utils'
import { normalizeProduct } from '../utils'
import * as query from '../utils/queries';
type Variables = {
slug: string
@ -18,9 +19,7 @@ const getProduct = async (options: {
let { config, variables } = options ?? {}
config = getConfig(config)
const { data }: GraphQLFetcherResult = await config.fetch(getProductQuery, {
variables,
})
const { data }: GraphQLFetcherResult = await config.fetch(query.ProductOneBySlug, { variables })
return {
product: data?.product ? normalizeProduct(data.product) : null,

View File

@ -1,15 +1,14 @@
import { SWRHook } from '@commerce/utils/types'
import { Product } from '@commerce/types'
import useSearch, { UseSearch } from '@commerce/product/use-search'
import { ProductCountableEdge } from '../schema'
import {
getAllProductsQuery,
getCollectionProductsQuery,
getSearchVariables,
normalizeProduct,
} from '../utils'
import { Product } from '@commerce/types'
import * as query from '../utils/queries'
export default useSearch as UseSearch<typeof handler>
@ -31,13 +30,13 @@ export const handler: SWRHook<
SearchProductsInput
> = {
fetchOptions: {
query: getAllProductsQuery,
query: query.ProductMany
},
async fetcher({ input, options, fetch }) {
const { categoryId, brandId } = input
const data = await fetch({
query: categoryId ? getCollectionProductsQuery : options.query,
query: categoryId ? query.CollectionOne : options.query,
method: options?.method,
variables: getSearchVariables(input),
})

View File

@ -6,7 +6,7 @@ export const checkoutAttach = async (
{ variables, headers }: any,
): Promise<CheckoutCustomerAttach> => {
const data = await fetch({
query: mutation.AttachCheckout,
query: mutation.CheckoutAttach,
variables,
headers
})

View File

@ -7,7 +7,7 @@ import { CHECKOUT_ID_COOKIE } from '@framework/const'
export const checkoutCreate = async (
fetch: any
): Promise<CheckoutCreate> => {
const data = await fetch({ query: mutation.checkoutCreate })
const data = await fetch({ query: mutation.CheckoutCreate })
const checkout = data.checkoutCreate?.checkout
const checkoutId = checkout?.id
const checkoutToken = checkout?.token

View File

@ -1,4 +1,4 @@
export const checkoutDetailsFragment = `
export const CheckoutDetails = `
id
token
created
@ -45,14 +45,4 @@ export const checkoutDetailsFragment = `
}
}
}
`
export const getCheckoutQuery = /* GraphQL */ `
query($checkoutId: UUID!) {
checkout(token: $checkoutId) {
... on Checkout {
${checkoutDetailsFragment}
}
}
}
`
`

View File

@ -0,0 +1,2 @@
export { ProductConnection } from './product';
export { CheckoutDetails } from './checkout-details';

View File

@ -0,0 +1,29 @@
export const ProductConnection = /* GraphQL */ `
fragment ProductConnection on ProductCountableConnection {
pageInfo {
hasNextPage
hasPreviousPage
}
edges {
node {
id
name
description
slug
pricing {
priceRange {
start {
net {
amount
}
}
}
}
media {
url
alt
}
}
}
}
`

View File

@ -1,6 +1,6 @@
import { SaleorConfig } from '../api'
import { CollectionCountableEdge } from '../schema'
import { getSiteCollectionsQuery } from './queries/'
import * as query from './queries';
export type Category = {
entityId: string
@ -9,7 +9,7 @@ export type Category = {
}
const getCategories = async (config: SaleorConfig): Promise<Category[]> => {
const { data } = await config.fetch(getSiteCollectionsQuery, {
const { data } = await config.fetch(query.CollectionMany, {
variables: {
first: 100,
},

View File

@ -7,7 +7,7 @@ export const getSearchVariables = ({
categoryId,
sort,
}: SearchProductsInput) => {
const sortBy = { direction: 'ASC', ...getSortVariables(sort, !!categoryId), channel: "default-channel"};
const sortBy = { field: "NAME", direction: 'ASC', ...getSortVariables(sort, !!categoryId), channel: "default-channel"};
return {
categoryId,
filter: { search },

View File

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

View File

@ -26,7 +26,7 @@ export const handleAutomaticLogin = async (
Mutation,
MutationTokenCreateArgs
>({
query: mutation.sessionCreate,
query: mutation.SessionCreate,
variables: { ...input },
})
handleLogin(tokenCreate!)

View File

@ -1,5 +1,5 @@
export const accountRegister = /* GraphQL */ `
mutation customerCreate($input: AccountRegisterInput!) {
export const AccountCreate = /* GraphQL */ `
mutation AccountCreate($input: AccountRegisterInput!) {
accountRegister(input: $input) {
errors {
code

View File

@ -1,5 +1,5 @@
export const AttachCheckout = /* GraphQl */ `
mutation AttachCheckout($checkoutId: ID!) {
export const CheckoutAttach = /* GraphQl */ `
mutation CheckoutAttach($checkoutId: ID!) {
checkoutCustomerAttach(checkoutId: $checkoutId) {
errors {
message

View File

@ -1,7 +1,7 @@
import { checkoutDetailsFragment } from '../queries/get-checkout-query'
import * as fragment from '../fragments'
export const checkoutCreate = /* GraphQL */ `
mutation createCheckout {
export const CheckoutCreate = /* GraphQL */ `
mutation CheckoutCreate {
checkoutCreate(input: {
email: "customer@example.com",
lines: [],
@ -13,7 +13,7 @@ export const checkoutCreate = /* GraphQL */ `
message
}
checkout {
${checkoutDetailsFragment}
${fragment.CheckoutDetails}
}
}
}

View File

@ -0,0 +1,16 @@
import * as fragment from '../fragments'
export const CheckoutLineAdd = /* GraphQL */ `
mutation CheckoutLineAdd($checkoutId: ID!, $lineItems: [CheckoutLineInput!]!) {
checkoutLinesAdd(checkoutId: $checkoutId, lines: $lineItems) {
errors {
code
field
message
}
checkout {
${fragment.CheckoutDetails}
}
}
}
`

View File

@ -1,16 +0,0 @@
import { checkoutDetailsFragment } from '../queries/get-checkout-query'
export const checkoutLineAdd = /* GraphQL */ `
mutation checkoutLineItemAdd($checkoutId: ID!, $lineItems: [CheckoutLineInput!]!) {
checkoutLinesAdd(checkoutId: $checkoutId, lines: $lineItems) {
errors {
code
field
message
}
checkout {
${checkoutDetailsFragment}
}
}
}
`

View File

@ -1,19 +0,0 @@
import { checkoutDetailsFragment } from '../queries/get-checkout-query'
export const checkoutLineDelete = /* GraphQL */ `
mutation checkoutLineItemRemove($checkoutId: ID!, $lineId: ID!) {
checkoutLineDelete(
checkoutId: $checkoutId
lineId: $lineId
) {
errors {
code
field
message
}
checkout {
${checkoutDetailsFragment}
}
}
}
`

View File

@ -1,16 +0,0 @@
import { checkoutDetailsFragment } from '../queries/get-checkout-query'
export const checkoutLineUpdate = /* GraphQL */ `
mutation checkoutLineItemUpdate($checkoutId: ID!, $lineItems: [CheckoutLineInput!]!) {
checkoutLinesUpdate(checkoutId: $checkoutId, lines: $lineItems) {
errors {
code
field
message
}
checkout {
${checkoutDetailsFragment}
}
}
}
`

View File

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

View File

@ -0,0 +1,16 @@
import * as fragment from '../fragments';
export const CheckoutLineUpdate = /* GraphQL */ `
mutation CheckoutLineUpdate($checkoutId: ID!, $lineItems: [CheckoutLineInput!]!) {
checkoutLinesUpdate(checkoutId: $checkoutId, lines: $lineItems) {
errors {
code
field
message
}
checkout {
${fragment.CheckoutDetails}
}
}
}
`

View File

@ -1,8 +1,8 @@
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'
export { AttachCheckout } from './attach-checkout'
export { AccountCreate } from './account-create'
export { CheckoutCreate } from './checkout-create'
export { CheckoutLineAdd } from './checkout-line-add'
export { CheckoutLineUpdate } from './checkout-line-update'
export { CheckoutLineDelete } from './checkout-line-remove'
export { SessionCreate } from './session-create'
export { SessionDestroy } from './session-destroy'
export { CheckoutAttach } from './checkout-attach'

View File

@ -1,5 +1,5 @@
export const sessionCreate = /* GraphQL */ `
mutation tokenCreate($email: String!, $password: String!) {
export const SessionCreate = /* GraphQL */ `
mutation SessionCreate($email: String!, $password: String!) {
tokenCreate(email: $email, password: $password) {
token
refreshToken

View File

@ -1,5 +1,5 @@
export const sessionDestroy = /* GraphQL */ `
mutation customerAccessTokenDelete {
export const SessionDestroy = /* GraphQL */ `
mutation SessionDestroy {
tokensDeactivateAll {
errors {
field

View File

@ -0,0 +1,11 @@
import * as fragment from '../fragments';
export const CheckoutOne = /* GraphQL */ `
query CheckoutOne($checkoutId: UUID!) {
checkout(token: $checkoutId) {
... on Checkout {
${fragment.CheckoutDetails}
}
}
}
`

View File

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

View File

@ -1,6 +1,6 @@
import { productConnection } from './get-all-products-query'
import * as fragment from '../fragments'
export const getCollectionProductsQuery = /* GraphQL */ `
export const CollectionOne = /* GraphQL */ `
query getProductsFromCollection(
$categoryId: ID!
$first: Int = 100
@ -9,9 +9,9 @@ export const getCollectionProductsQuery = /* GraphQL */ `
collection(id: $categoryId, channel: $channel) {
id
products(first: $first) {
...productConnection
...ProductConnection
}
}
}
${productConnection}
${fragment.ProductConnection}
`

View File

@ -0,0 +1,11 @@
export const CustomerCurrent = /* GraphQL */ `
query CustomerCurrent {
me {
id
email
firstName
lastName
dateJoined
}
}
`

View File

@ -0,0 +1,7 @@
export const CustomerOne = /* GraphQL */ `
query CustomerOne($customerAccessToken: String!) {
customer(customerAccessToken: $customerAccessToken) {
id
}
}
`

View File

@ -1,43 +0,0 @@
export const productConnection = /* GraphQL */ `
fragment productConnection on ProductCountableConnection {
pageInfo {
hasNextPage
hasPreviousPage
}
edges {
node {
id
name
description
slug
pricing {
priceRange {
start {
net {
amount
}
}
}
}
media {
url
alt
}
}
}
}
`
export const getAllProductsQuery = /* GraphQL */ `
query getAllProducts(
$first: Int = 100
$filter: ProductFilterInput
$sortBy: ProductOrder
$channel: String = "default-channel"
) {
products(first: $first, channel: $channel, filter: $filter, sortBy: $sortBy) {
...productConnection
}
}
${productConnection}
`

View File

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

View File

@ -1,12 +0,0 @@
export const getCustomerQuery = /* GraphQL */ `
query getCustomer {
me {
id
email
firstName
lastName
dateJoined
}
}
`
export default getCustomerQuery

View File

@ -1,9 +0,0 @@
export const getPageQuery = /* GraphQL */ `
query($id: ID!) {
page(id: $id) {
id
title
slug
}
}
`

View File

@ -1,11 +1,14 @@
export { getSiteCollectionsQuery } from './get-all-collections-query'
export { getProductQuery } from './get-product-query'
export { getAllProductsQuery } from './get-all-products-query'
export { CollectionMany } from './collection-many'
export { ProductOneBySlug } from './product-one-by-slug'
export { ProductMany } from './product-many'
export { CollectionOne } from './collection-one'
export { CheckoutOne } from './checkout-one'
export { PageMany } from './page-many'
export { PageOne } from './page-one'
export { CustomerCurrent } from './customer-current'
// getCustomerIdQuery
export { CustomerOne } from './customer-one'
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'

View File

@ -1,5 +1,5 @@
export const getAllPagesQuery = /* GraphQL */ `
query getAllPages($first: Int = 100) {
export const PageMany = /* GraphQL */ `
query PageMany($first: Int = 100) {
pages(first: $first) {
edges {
node {

View File

@ -0,0 +1,9 @@
export const PageOne = /* GraphQL */ `
query PageOne($id: ID!) {
page(id: $id) {
id
title
slug
}
}
`

View File

@ -0,0 +1,15 @@
import * as fragment from '../fragments';
export const ProductMany = /* GraphQL */ `
query ProductMany(
$first: Int = 100
$filter: ProductFilterInput
$sortBy: ProductOrder
$channel: String = "default-channel"
) {
products(first: $first, channel: $channel, filter: $filter, sortBy: $sortBy) {
...ProductConnection
}
}
${fragment.ProductConnection}
`

View File

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