refactor: move query into get-product-query

This commit is contained in:
Tan Le
2021-09-30 15:52:18 +07:00
parent a4c23a2181
commit cf2260869a
2 changed files with 19 additions and 18 deletions

View File

@@ -39,3 +39,19 @@ export const getProductQuery = /* GraphQL */ `
} }
} }
` `
export const getProductDetailQuery = /* GraphQL */ `
query GetProductDetail($slug: String! = "hand-trowel") {
product(slug: $slug) {
name
description
variants {
price
priceWithTax
}
assets {
preview
name
}
}
}
`

View File

@@ -1,30 +1,15 @@
import { GetProductQuery } from '@framework/schema' import { GetProductQuery } from '@framework/schema'
import { gql } from 'graphql-request' import { getProductDetailQuery } from '@framework/utils/queries/get-product-query';
import gglFetcher from 'src/utils/gglFetcher' import gglFetcher from 'src/utils/gglFetcher'
import useSWR from 'swr' import useSWR from 'swr'
const query = gql`
query GetProductDetail($slug: String! = "hand-trowel") {
product(slug: $slug) {
name
description
variants {
price
priceWithTax
}
assets {
preview
name
}
}
}
`
interface ProductDetail { interface ProductDetail {
slug: string slug: string
} }
const useProductDetail = () => { const useProductDetail = () => {
const { data, ...rest } = useSWR<GetProductQuery>([query],gglFetcher) const { data, ...rest } = useSWR<GetProductQuery>([getProductDetailQuery],gglFetcher)
return { productDetail: data?.product, ...rest } return { productDetail: data?.product, ...rest }
} }