From 49bd38fd829497298b2860e88e611905810b4aa9 Mon Sep 17 00:00:00 2001 From: Loan Laux Date: Wed, 31 Mar 2021 17:43:42 +0400 Subject: [PATCH] replace customer query with viewer Signed-off-by: Loan Laux --- .../customer/use-customer.tsx | 7 +++-- .../utils/queries/get-customer-id-query.ts | 4 +-- .../utils/queries/get-customer-query.ts | 16 ------------ .../reactioncommerce/utils/queries/index.ts | 2 +- .../utils/queries/viewer-query.ts | 26 +++++++++++++++++++ 5 files changed, 32 insertions(+), 23 deletions(-) delete mode 100644 framework/reactioncommerce/utils/queries/get-customer-query.ts create mode 100644 framework/reactioncommerce/utils/queries/viewer-query.ts diff --git a/framework/reactioncommerce/customer/use-customer.tsx b/framework/reactioncommerce/customer/use-customer.tsx index 137f0da74..113dd1a9d 100644 --- a/framework/reactioncommerce/customer/use-customer.tsx +++ b/framework/reactioncommerce/customer/use-customer.tsx @@ -1,20 +1,19 @@ import useCustomer, { UseCustomer } from '@commerce/customer/use-customer' import { Customer } from '@commerce/types' import { SWRHook } from '@commerce/utils/types' -import { getCustomerQuery, getCustomerToken } from '../utils' +import { viewerQuery } from '../utils' export default useCustomer as UseCustomer export const handler: SWRHook = { fetchOptions: { - query: getCustomerQuery, + query: viewerQuery, }, async fetcher({ options, fetch }) { const data = await fetch({ ...options, - variables: { customerAccessToken: getCustomerToken() }, }) - return data.customer ?? null + return data.viewer ?? null }, useHook: ({ useData }) => (input) => { return useData({ diff --git a/framework/reactioncommerce/utils/queries/get-customer-id-query.ts b/framework/reactioncommerce/utils/queries/get-customer-id-query.ts index 076ceb10b..51fef14e7 100644 --- a/framework/reactioncommerce/utils/queries/get-customer-id-query.ts +++ b/framework/reactioncommerce/utils/queries/get-customer-id-query.ts @@ -1,8 +1,8 @@ -export const getCustomerQuery = /* GraphQL */ ` +export const viewerQuery = /* GraphQL */ ` query getCustomerId($customerAccessToken: String!) { customer(customerAccessToken: $customerAccessToken) { id } } ` -export default getCustomerQuery +export default viewerQuery diff --git a/framework/reactioncommerce/utils/queries/get-customer-query.ts b/framework/reactioncommerce/utils/queries/get-customer-query.ts deleted file mode 100644 index 87e37e68d..000000000 --- a/framework/reactioncommerce/utils/queries/get-customer-query.ts +++ /dev/null @@ -1,16 +0,0 @@ -export const getCustomerQuery = /* GraphQL */ ` - query getCustomer($customerAccessToken: String!) { - customer(customerAccessToken: $customerAccessToken) { - id - firstName - lastName - displayName - email - phone - tags - acceptsMarketing - createdAt - } - } -` -export default getCustomerQuery diff --git a/framework/reactioncommerce/utils/queries/index.ts b/framework/reactioncommerce/utils/queries/index.ts index 327a77183..857648471 100644 --- a/framework/reactioncommerce/utils/queries/index.ts +++ b/framework/reactioncommerce/utils/queries/index.ts @@ -7,4 +7,4 @@ export { default as getCollectionProductsQuery } from './get-collection-products 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 { default as viewerQuery } from './viewer-query' diff --git a/framework/reactioncommerce/utils/queries/viewer-query.ts b/framework/reactioncommerce/utils/queries/viewer-query.ts new file mode 100644 index 000000000..fa08ba0ef --- /dev/null +++ b/framework/reactioncommerce/utils/queries/viewer-query.ts @@ -0,0 +1,26 @@ +export const viewerQuery = /* GraphQL */ ` + query viewer { + viewer { + _id + addressBook { + edges { + node { + _id + isBillingDefault + isShippingDefault + phone + } + } + } + emailRecords { + address + } + firstName + lastName + name + primaryEmailAddress + createdAt + } + } +` +export default viewerQuery