From 1fac8e187d1630a52552eccf0414660187be42ce Mon Sep 17 00:00:00 2001 From: cond0r Date: Thu, 29 Apr 2021 21:58:46 +0300 Subject: [PATCH] Uptate customer fetch types --- framework/shopify/customer/get-customer-id.ts | 9 ++++++--- framework/shopify/customer/use-customer.tsx | 14 +++++++++----- framework/shopify/product/get-all-collections.ts | 12 +++++++----- 3 files changed, 22 insertions(+), 13 deletions(-) diff --git a/framework/shopify/customer/get-customer-id.ts b/framework/shopify/customer/get-customer-id.ts index ca096645a..4f38f1c72 100644 --- a/framework/shopify/customer/get-customer-id.ts +++ b/framework/shopify/customer/get-customer-id.ts @@ -1,6 +1,7 @@ import { getConfig, ShopifyConfig } from '../api' import getCustomerIdQuery from '../utils/queries/get-customer-id-query' import Cookies from 'js-cookie' +import { GetCustomerIdQuery } from '../schema' async function getCustomerId({ customerToken: customerAccesToken, @@ -8,17 +9,19 @@ async function getCustomerId({ }: { customerToken: string config?: ShopifyConfig -}): Promise { +}): Promise { config = getConfig(config) - const { data } = await config.fetch(getCustomerIdQuery, { + const { + data: { customer }, + } = await config.fetch(getCustomerIdQuery, { variables: { customerAccesToken: customerAccesToken || Cookies.get(config.customerCookie), }, }) - return data.customer?.id + return customer?.id } export default getCustomerId diff --git a/framework/shopify/customer/use-customer.tsx b/framework/shopify/customer/use-customer.tsx index 137f0da74..7b600838e 100644 --- a/framework/shopify/customer/use-customer.tsx +++ b/framework/shopify/customer/use-customer.tsx @@ -10,11 +10,15 @@ export const handler: SWRHook = { query: getCustomerQuery, }, async fetcher({ options, fetch }) { - const data = await fetch({ - ...options, - variables: { customerAccessToken: getCustomerToken() }, - }) - return data.customer ?? null + const customerAccessToken = getCustomerToken() + if (customerAccessToken) { + const data = await fetch({ + ...options, + variables: { customerAccessToken: getCustomerToken() }, + }) + return data.customer + } + return null }, useHook: ({ useData }) => (input) => { return useData({ diff --git a/framework/shopify/product/get-all-collections.ts b/framework/shopify/product/get-all-collections.ts index 15c4bc51a..3369f9e51 100644 --- a/framework/shopify/product/get-all-collections.ts +++ b/framework/shopify/product/get-all-collections.ts @@ -1,4 +1,4 @@ -import { CollectionEdge } from '../schema' +import { GetSiteCollectionsQuery } from '../schema' import { getConfig, ShopifyConfig } from '../api' import getAllCollectionsQuery from '../utils/queries/get-all-collections-query' @@ -10,11 +10,13 @@ const getAllCollections = async (options?: { let { config, variables = { first: 250 } } = options ?? {} config = getConfig(config) - const { data } = await config.fetch(getAllCollectionsQuery, { variables }) - const edges = data.collections?.edges ?? [] + const { data } = await config.fetch( + getAllCollectionsQuery, + { variables } + ) - const categories = edges.map( - ({ node: { id: entityId, title: name, handle } }: CollectionEdge) => ({ + const categories = data.collections.edges.map( + ({ node: { id: entityId, title: name, handle } }) => ({ entityId, name, path: `/${handle}`,