From 36303dddd574c29141d3c28e3350a74de722ada0 Mon Sep 17 00:00:00 2001 From: Michael Bromley Date: Wed, 10 Mar 2021 21:28:46 +0100 Subject: [PATCH] Vendure provider: split out gql operations, remove unused files --- framework/vendure/auth/use-login.tsx | 16 +--- framework/vendure/auth/use-logout.tsx | 9 +-- framework/vendure/auth/use-signup.tsx | 16 +--- framework/vendure/cart/use-add-item.tsx | 16 +--- framework/vendure/cart/use-cart.tsx | 11 +-- framework/vendure/cart/use-remove-item.tsx | 16 +--- framework/vendure/cart/use-update-item.tsx | 16 +--- framework/vendure/common/get-site-info.ts | 25 +----- framework/vendure/customer/get-customer-id.ts | 34 -------- .../vendure/customer/get-customer-wishlist.ts | 79 ++----------------- framework/vendure/customer/use-customer.tsx | 12 +-- .../fragments/cart-fragment.ts} | 0 .../fragments/search-result-fragment.ts} | 0 framework/vendure/lib/immutability.ts | 18 ----- .../mutations/add-item-to-order-mutation.ts | 15 ++++ .../mutations/adjust-order-line-mutation.ts | 15 ++++ .../vendure/lib/mutations/log-in-mutation.ts | 14 ++++ .../vendure/lib/mutations/log-out-mutation.ts | 7 ++ .../mutations/remove-order-line-mutation.ts | 15 ++++ .../vendure/lib/mutations/sign-up-mutation.ts | 14 ++++ framework/vendure/lib/normalize.ts | 68 ---------------- .../lib/queries/active-customer-query.ts | 10 +++ .../queries/get-all-product-paths-query.ts | 9 +++ .../lib/queries/get-all-products-query.ts | 12 +++ .../vendure/lib/queries/get-cart-query.ts | 10 +++ .../lib/queries/get-collections-query.ts | 21 +++++ .../vendure/lib/queries/get-product-query.ts | 41 ++++++++++ framework/vendure/lib/queries/search-query.ts | 13 +++ .../vendure/product/get-all-product-paths.ts | 13 +-- framework/vendure/product/get-all-products.ts | 13 +-- framework/vendure/product/get-product.ts | 45 +---------- framework/vendure/product/use-search.tsx | 14 +--- 32 files changed, 217 insertions(+), 400 deletions(-) delete mode 100644 framework/vendure/customer/get-customer-id.ts rename framework/vendure/{api/fragments/cart.ts => lib/fragments/cart-fragment.ts} (100%) rename framework/vendure/{api/fragments/search-result.ts => lib/fragments/search-result-fragment.ts} (100%) delete mode 100644 framework/vendure/lib/immutability.ts create mode 100644 framework/vendure/lib/mutations/add-item-to-order-mutation.ts create mode 100644 framework/vendure/lib/mutations/adjust-order-line-mutation.ts create mode 100644 framework/vendure/lib/mutations/log-in-mutation.ts create mode 100644 framework/vendure/lib/mutations/log-out-mutation.ts create mode 100644 framework/vendure/lib/mutations/remove-order-line-mutation.ts create mode 100644 framework/vendure/lib/mutations/sign-up-mutation.ts create mode 100644 framework/vendure/lib/queries/active-customer-query.ts create mode 100644 framework/vendure/lib/queries/get-all-product-paths-query.ts create mode 100644 framework/vendure/lib/queries/get-all-products-query.ts create mode 100644 framework/vendure/lib/queries/get-cart-query.ts create mode 100644 framework/vendure/lib/queries/get-collections-query.ts create mode 100644 framework/vendure/lib/queries/get-product-query.ts create mode 100644 framework/vendure/lib/queries/search-query.ts diff --git a/framework/vendure/auth/use-login.tsx b/framework/vendure/auth/use-login.tsx index d3e365519..f0fc85cbc 100644 --- a/framework/vendure/auth/use-login.tsx +++ b/framework/vendure/auth/use-login.tsx @@ -4,21 +4,7 @@ import useLogin, { UseLogin } from '@commerce/auth/use-login' import { CommerceError, ValidationError } from '@commerce/utils/errors' import useCustomer from '../customer/use-customer' import { LoginMutation, LoginMutationVariables } from '../schema' - -export const loginMutation = /* GraphQL */ ` - mutation login($username: String!, $password: String!) { - login(username: $username, password: $password) { - __typename - ... on CurrentUser { - id - } - ... on ErrorResult { - errorCode - message - } - } - } -` +import { loginMutation } from '../lib/mutations/log-in-mutation' export default useLogin as UseLogin diff --git a/framework/vendure/auth/use-logout.tsx b/framework/vendure/auth/use-logout.tsx index 3b053155b..93ba665b8 100644 --- a/framework/vendure/auth/use-logout.tsx +++ b/framework/vendure/auth/use-logout.tsx @@ -3,14 +3,7 @@ import { MutationHook } from '@commerce/utils/types' import useLogout, { UseLogout } from '@commerce/auth/use-logout' import useCustomer from '../customer/use-customer' import { LogoutMutation } from '../schema' - -export const logoutMutation = /* GraphQL */ ` - mutation logout { - logout { - success - } - } -` +import { logoutMutation } from '../lib/mutations/log-out-mutation' export default useLogout as UseLogout diff --git a/framework/vendure/auth/use-signup.tsx b/framework/vendure/auth/use-signup.tsx index dcd18ee5d..b9b461250 100644 --- a/framework/vendure/auth/use-signup.tsx +++ b/framework/vendure/auth/use-signup.tsx @@ -8,21 +8,7 @@ import { SignupMutation, SignupMutationVariables, } from '../schema' - -export const signupMutation = /* GraphQL */ ` - mutation signup($input: RegisterCustomerInput!) { - registerCustomerAccount(input: $input) { - __typename - ... on Success { - success - } - ... on ErrorResult { - errorCode - message - } - } - } -` +import { signupMutation } from '../lib/mutations/sign-up-mutation' export default useSignup as UseSignup diff --git a/framework/vendure/cart/use-add-item.tsx b/framework/vendure/cart/use-add-item.tsx index 780ef03ea..42c5e5a63 100644 --- a/framework/vendure/cart/use-add-item.tsx +++ b/framework/vendure/cart/use-add-item.tsx @@ -4,23 +4,9 @@ import { CommerceError } from '@commerce/utils/errors' import { MutationHook } from '@commerce/utils/types' import { useCallback } from 'react' import useCart from './use-cart' -import { cartFragment } from '../api/fragments/cart' import { AddItemToOrderMutation } from '../schema' import { normalizeCart } from '../lib/normalize' - -export const addItemToOrderMutation = /* GraphQL */ ` - mutation addItemToOrder($variantId: ID!, $quantity: Int!) { - addItemToOrder(productVariantId: $variantId, quantity: $quantity) { - __typename - ...Cart - ... on ErrorResult { - errorCode - message - } - } - } - ${cartFragment} -` +import { addItemToOrderMutation } from '../lib/mutations/add-item-to-order-mutation' export default useAddItem as UseAddItem diff --git a/framework/vendure/cart/use-cart.tsx b/framework/vendure/cart/use-cart.tsx index 5464be042..ee9975f87 100644 --- a/framework/vendure/cart/use-cart.tsx +++ b/framework/vendure/cart/use-cart.tsx @@ -1,19 +1,10 @@ import { Cart } from '@commerce/types' import { SWRHook } from '@commerce/utils/types' import useCart, { FetchCartInput, UseCart } from '@commerce/cart/use-cart' -import { cartFragment } from '../api/fragments/cart' import { ActiveOrderQuery, CartFragment } from '../schema' import { normalizeCart } from '../lib/normalize' import { useMemo } from 'react' - -export const getCartQuery = /* GraphQL */ ` - query activeOrder { - activeOrder { - ...Cart - } - } - ${cartFragment} -` +import { getCartQuery } from '../lib/queries/get-cart-query' export type CartResult = { activeOrder?: CartFragment diff --git a/framework/vendure/cart/use-remove-item.tsx b/framework/vendure/cart/use-remove-item.tsx index 151d05174..f23f04d00 100644 --- a/framework/vendure/cart/use-remove-item.tsx +++ b/framework/vendure/cart/use-remove-item.tsx @@ -3,27 +3,13 @@ import { HookFetcherContext, MutationHookContext } from '@commerce/utils/types' import useRemoveItem, { UseRemoveItem } from '@commerce/cart/use-remove-item' import { CommerceError } from '@commerce/utils/errors' import useCart from './use-cart' -import { cartFragment } from '../api/fragments/cart' import { RemoveOrderLineMutation, RemoveOrderLineMutationVariables, } from '../schema' import { Cart, LineItem, RemoveCartItemBody } from '@commerce/types' import { normalizeCart } from '../lib/normalize' - -export const removeOrderLineMutation = /* GraphQL */ ` - mutation removeOrderLine($orderLineId: ID!) { - removeOrderLine(orderLineId: $orderLineId) { - __typename - ...Cart - ... on ErrorResult { - errorCode - message - } - } - } - ${cartFragment} -` +import { removeOrderLineMutation } from '../lib/mutations/remove-order-line-mutation' export default useRemoveItem as UseRemoveItem diff --git a/framework/vendure/cart/use-update-item.tsx b/framework/vendure/cart/use-update-item.tsx index 215e18980..c87022d35 100644 --- a/framework/vendure/cart/use-update-item.tsx +++ b/framework/vendure/cart/use-update-item.tsx @@ -13,22 +13,8 @@ import { AdjustOrderLineMutation, AdjustOrderLineMutationVariables, } from '../schema' -import { cartFragment } from '../api/fragments/cart' import { normalizeCart } from '../lib/normalize' - -export const adjustOrderLineMutation = /* GraphQL */ ` - mutation adjustOrderLine($orderLineId: ID!, $quantity: Int!) { - adjustOrderLine(orderLineId: $orderLineId, quantity: $quantity) { - __typename - ...Cart - ... on ErrorResult { - errorCode - message - } - } - } - ${cartFragment} -` +import { adjustOrderLineMutation } from '../lib/mutations/adjust-order-line-mutation' export default useUpdateItem as UseUpdateItem diff --git a/framework/vendure/common/get-site-info.ts b/framework/vendure/common/get-site-info.ts index ba7df175b..a56157310 100644 --- a/framework/vendure/common/get-site-info.ts +++ b/framework/vendure/common/get-site-info.ts @@ -1,28 +1,7 @@ -import { VendureConfig, getConfig } from '../api' +import { getConfig, VendureConfig } from '../api' import { GetCollectionsQuery } from '../schema' import { arrayToTree } from '../lib/array-to-tree' - -export const getCollectionsQuery = /* GraphQL */ ` - query getCollections { - collections { - items { - id - name - description - slug - productVariants { - totalItems - } - parent { - id - } - children { - id - } - } - } - } -` +import { getCollectionsQuery } from '../lib/queries/get-collections-query' async function getSiteInfo({ query = getCollectionsQuery, diff --git a/framework/vendure/customer/get-customer-id.ts b/framework/vendure/customer/get-customer-id.ts deleted file mode 100644 index 9fb5f6eea..000000000 --- a/framework/vendure/customer/get-customer-id.ts +++ /dev/null @@ -1,34 +0,0 @@ -import { GetCustomerIdQuery } from '../schema' -import { VendureConfig, getConfig } from '../api' - -export const getCustomerIdQuery = /* */ ` - query getCustomerId { - customer { - entityId - } - } -` - -async function getCustomerId({ - customerToken, - config, -}: { - customerToken: string - config?: VendureConfig -}): Promise { - config = getConfig(config) - - const { data } = await config.fetch( - getCustomerIdQuery, - undefined, - { - headers: { - cookie: `${config.customerCookie}=${customerToken}`, - }, - } - ) - - return data?.customer?.entityId -} - -export default getCustomerId diff --git a/framework/vendure/customer/get-customer-wishlist.ts b/framework/vendure/customer/get-customer-wishlist.ts index ccacf87b9..81ec96956 100644 --- a/framework/vendure/customer/get-customer-wishlist.ts +++ b/framework/vendure/customer/get-customer-wishlist.ts @@ -1,41 +1,4 @@ -import type { RecursivePartial, RecursiveRequired } from '../api/utils/types' -import { definitions } from '../api/definitions/wishlist' -import { VendureConfig, getConfig } from '../api' -import getAllProducts, { ProductEdge } from '../product/get-all-products' - -export type Wishlist = Omit & { - items?: WishlistItem[] -} - -export type WishlistItem = NonNullable< - definitions['wishlist_Full']['items'] ->[0] & { - product?: ProductEdge['node'] -} - -export type GetCustomerWishlistResult< - T extends { wishlist?: any } = { wishlist?: Wishlist } -> = T - -export type GetCustomerWishlistVariables = { - customerId: number -} - -async function getCustomerWishlist(opts: { - variables: GetCustomerWishlistVariables - config?: VendureConfig - includeProducts?: boolean -}): Promise - -async function getCustomerWishlist< - T extends { wishlist?: any }, - V = any ->(opts: { - url: string - variables: V - config?: VendureConfig - includeProducts?: boolean -}): Promise> +import { getConfig, VendureConfig } from '../api' async function getCustomerWishlist({ config, @@ -43,45 +6,13 @@ async function getCustomerWishlist({ includeProducts, }: { url?: string - variables: GetCustomerWishlistVariables + variables: any config?: VendureConfig includeProducts?: boolean -}): Promise { +}): Promise { + // Not implemented as Vendure does not ship with wishlist functionality at present config = getConfig(config) - - const { data = [] } = await config.storeApiFetch< - RecursivePartial<{ data: Wishlist[] }> - >(`/v3/wishlists?customer_id=${variables.customerId}`) - const wishlist = data[0] - - if (includeProducts && wishlist?.items?.length) { - const entityIds = wishlist.items - ?.map((item) => item?.product_id) - .filter((id): id is number => !!id) - - if (entityIds?.length) { - const graphqlData = await getAllProducts({ - variables: { first: 100, entityIds }, - config, - }) - // Put the products in an object that we can use to get them by id - const productsById = graphqlData.products.reduce<{ - [k: number]: ProductEdge - }>((prods, p) => { - prods[p.node.entityId] = p - return prods - }, {}) - // Populate the wishlist items with the graphql products - wishlist.items.forEach((item) => { - const product = item && productsById[item.product_id!] - if (item && product) { - item.product = product.node - } - }) - } - } - - return { wishlist: wishlist as RecursiveRequired } + return { wishlist: {} } } export default getCustomerWishlist diff --git a/framework/vendure/customer/use-customer.tsx b/framework/vendure/customer/use-customer.tsx index 74275abd1..4de821253 100644 --- a/framework/vendure/customer/use-customer.tsx +++ b/framework/vendure/customer/use-customer.tsx @@ -2,17 +2,7 @@ import { SWRHook } from '@commerce/utils/types' import useCustomer, { UseCustomer } from '@commerce/customer/use-customer' import { Customer } from '@commerce/types' import { ActiveCustomerQuery } from '../schema' - -export const activeCustomerQuery = /* GraphQL */ ` - query activeCustomer { - activeCustomer { - id - firstName - lastName - emailAddress - } - } -` +import { activeCustomerQuery } from '../lib/queries/active-customer-query' export default useCustomer as UseCustomer diff --git a/framework/vendure/api/fragments/cart.ts b/framework/vendure/lib/fragments/cart-fragment.ts similarity index 100% rename from framework/vendure/api/fragments/cart.ts rename to framework/vendure/lib/fragments/cart-fragment.ts diff --git a/framework/vendure/api/fragments/search-result.ts b/framework/vendure/lib/fragments/search-result-fragment.ts similarity index 100% rename from framework/vendure/api/fragments/search-result.ts rename to framework/vendure/lib/fragments/search-result-fragment.ts diff --git a/framework/vendure/lib/immutability.ts b/framework/vendure/lib/immutability.ts deleted file mode 100644 index dc0177f8a..000000000 --- a/framework/vendure/lib/immutability.ts +++ /dev/null @@ -1,18 +0,0 @@ - -import update, { Context } from 'immutability-helper'; - -const c = new Context(); - -c.extend('$auto', function(value, object) { - return object ? - c.update(object, value): - c.update({}, value); -}); - -c.extend('$autoArray', function(value, object) { - return object ? - c.update(object, value): - c.update([], value); -}); - -export default c.update \ No newline at end of file diff --git a/framework/vendure/lib/mutations/add-item-to-order-mutation.ts b/framework/vendure/lib/mutations/add-item-to-order-mutation.ts new file mode 100644 index 000000000..5ed41d972 --- /dev/null +++ b/framework/vendure/lib/mutations/add-item-to-order-mutation.ts @@ -0,0 +1,15 @@ +import { cartFragment } from '../fragments/cart-fragment' + +export const addItemToOrderMutation = /* GraphQL */ ` + mutation addItemToOrder($variantId: ID!, $quantity: Int!) { + addItemToOrder(productVariantId: $variantId, quantity: $quantity) { + __typename + ...Cart + ... on ErrorResult { + errorCode + message + } + } + } + ${cartFragment} +` diff --git a/framework/vendure/lib/mutations/adjust-order-line-mutation.ts b/framework/vendure/lib/mutations/adjust-order-line-mutation.ts new file mode 100644 index 000000000..ce9864b12 --- /dev/null +++ b/framework/vendure/lib/mutations/adjust-order-line-mutation.ts @@ -0,0 +1,15 @@ +import { cartFragment } from '../fragments/cart-fragment' + +export const adjustOrderLineMutation = /* GraphQL */ ` + mutation adjustOrderLine($orderLineId: ID!, $quantity: Int!) { + adjustOrderLine(orderLineId: $orderLineId, quantity: $quantity) { + __typename + ...Cart + ... on ErrorResult { + errorCode + message + } + } + } + ${cartFragment} +` diff --git a/framework/vendure/lib/mutations/log-in-mutation.ts b/framework/vendure/lib/mutations/log-in-mutation.ts new file mode 100644 index 000000000..75e2ddba9 --- /dev/null +++ b/framework/vendure/lib/mutations/log-in-mutation.ts @@ -0,0 +1,14 @@ +export const loginMutation = /* GraphQL */ ` + mutation login($username: String!, $password: String!) { + login(username: $username, password: $password) { + __typename + ... on CurrentUser { + id + } + ... on ErrorResult { + errorCode + message + } + } + } +` diff --git a/framework/vendure/lib/mutations/log-out-mutation.ts b/framework/vendure/lib/mutations/log-out-mutation.ts new file mode 100644 index 000000000..6f222c5c8 --- /dev/null +++ b/framework/vendure/lib/mutations/log-out-mutation.ts @@ -0,0 +1,7 @@ +export const logoutMutation = /* GraphQL */ ` + mutation logout { + logout { + success + } + } +` diff --git a/framework/vendure/lib/mutations/remove-order-line-mutation.ts b/framework/vendure/lib/mutations/remove-order-line-mutation.ts new file mode 100644 index 000000000..47a89039e --- /dev/null +++ b/framework/vendure/lib/mutations/remove-order-line-mutation.ts @@ -0,0 +1,15 @@ +import { cartFragment } from '../fragments/cart-fragment' + +export const removeOrderLineMutation = /* GraphQL */ ` + mutation removeOrderLine($orderLineId: ID!) { + removeOrderLine(orderLineId: $orderLineId) { + __typename + ...Cart + ... on ErrorResult { + errorCode + message + } + } + } + ${cartFragment} +` diff --git a/framework/vendure/lib/mutations/sign-up-mutation.ts b/framework/vendure/lib/mutations/sign-up-mutation.ts new file mode 100644 index 000000000..410e395c2 --- /dev/null +++ b/framework/vendure/lib/mutations/sign-up-mutation.ts @@ -0,0 +1,14 @@ +export const signupMutation = /* GraphQL */ ` + mutation signup($input: RegisterCustomerInput!) { + registerCustomerAccount(input: $input) { + __typename + ... on Success { + success + } + ... on ErrorResult { + errorCode + message + } + } + } +` diff --git a/framework/vendure/lib/normalize.ts b/framework/vendure/lib/normalize.ts index 0be24461c..7bd30da80 100644 --- a/framework/vendure/lib/normalize.ts +++ b/framework/vendure/lib/normalize.ts @@ -1,74 +1,6 @@ import { Cart, Product } from '@commerce/types' -import update from '../lib/immutability' import { CartFragment, SearchResultFragment } from '../schema' -function normalizeProductOption(productOption: any) { - const { - node: { - entityId, - values: { edges }, - ...rest - }, - } = productOption - - return { - id: entityId, - values: edges?.map(({ node }: any) => node), - ...rest, - } -} - -export function normalizeProduct(productNode: any): Product { - const { - entityId: id, - productOptions, - prices, - path, - id: _, - options: _0, - } = productNode - - return update(productNode, { - id: { $set: String(id) }, - images: { - $apply: ({ edges }: any) => - edges?.map(({ node: { urlOriginal, altText, ...rest } }: any) => ({ - url: urlOriginal, - alt: altText, - ...rest, - })), - }, - variants: { - $apply: ({ edges }: any) => - edges?.map(({ node: { entityId, productOptions, ...rest } }: any) => ({ - id: entityId, - options: productOptions?.edges - ? productOptions.edges.map(normalizeProductOption) - : [], - ...rest, - })), - }, - options: { - $set: productOptions.edges - ? productOptions?.edges.map(normalizeProductOption) - : [], - }, - brand: { - $apply: (brand: any) => (brand?.entityId ? brand?.entityId : null), - }, - slug: { - $set: path?.replace(/^\/+|\/+$/g, ''), - }, - price: { - $set: { - value: prices?.price.value, - currencyCode: prices?.price.currencyCode, - }, - }, - $unset: ['entityId'], - }) -} - export function normalizeSearchResult(item: SearchResultFragment): Product { return { id: item.productId, diff --git a/framework/vendure/lib/queries/active-customer-query.ts b/framework/vendure/lib/queries/active-customer-query.ts new file mode 100644 index 000000000..65b280743 --- /dev/null +++ b/framework/vendure/lib/queries/active-customer-query.ts @@ -0,0 +1,10 @@ +export const activeCustomerQuery = /* GraphQL */ ` + query activeCustomer { + activeCustomer { + id + firstName + lastName + emailAddress + } + } +` diff --git a/framework/vendure/lib/queries/get-all-product-paths-query.ts b/framework/vendure/lib/queries/get-all-product-paths-query.ts new file mode 100644 index 000000000..29922dca2 --- /dev/null +++ b/framework/vendure/lib/queries/get-all-product-paths-query.ts @@ -0,0 +1,9 @@ +export const getAllProductPathsQuery = /* GraphQL */ ` + query getAllProductPaths($first: Int = 100) { + products(options: { take: $first }) { + items { + slug + } + } + } +` diff --git a/framework/vendure/lib/queries/get-all-products-query.ts b/framework/vendure/lib/queries/get-all-products-query.ts new file mode 100644 index 000000000..1b44b2017 --- /dev/null +++ b/framework/vendure/lib/queries/get-all-products-query.ts @@ -0,0 +1,12 @@ +import { searchResultFragment } from '../fragments/search-result-fragment' + +export const getAllProductsQuery = /* GraphQL */ ` + query getAllProducts($input: SearchInput!) { + search(input: $input) { + items { + ...SearchResult + } + } + } + ${searchResultFragment} +` diff --git a/framework/vendure/lib/queries/get-cart-query.ts b/framework/vendure/lib/queries/get-cart-query.ts new file mode 100644 index 000000000..7faac355c --- /dev/null +++ b/framework/vendure/lib/queries/get-cart-query.ts @@ -0,0 +1,10 @@ +import { cartFragment } from '../fragments/cart-fragment' + +export const getCartQuery = /* GraphQL */ ` + query activeOrder { + activeOrder { + ...Cart + } + } + ${cartFragment} +` diff --git a/framework/vendure/lib/queries/get-collections-query.ts b/framework/vendure/lib/queries/get-collections-query.ts new file mode 100644 index 000000000..ed0919652 --- /dev/null +++ b/framework/vendure/lib/queries/get-collections-query.ts @@ -0,0 +1,21 @@ +export const getCollectionsQuery = /* GraphQL */ ` + query getCollections { + collections { + items { + id + name + description + slug + productVariants { + totalItems + } + parent { + id + } + children { + id + } + } + } + } +` diff --git a/framework/vendure/lib/queries/get-product-query.ts b/framework/vendure/lib/queries/get-product-query.ts new file mode 100644 index 000000000..b2c502da9 --- /dev/null +++ b/framework/vendure/lib/queries/get-product-query.ts @@ -0,0 +1,41 @@ +export const getProductQuery = /* GraphQL */ ` + query getProduct($slug: String!) { + product(slug: $slug) { + id + name + slug + description + assets { + id + preview + name + } + variants { + id + priceWithTax + currencyCode + options { + id + name + code + groupId + group { + id + options { + name + } + } + } + } + optionGroups { + id + code + name + options { + id + name + } + } + } + } +` diff --git a/framework/vendure/lib/queries/search-query.ts b/framework/vendure/lib/queries/search-query.ts new file mode 100644 index 000000000..f95c43703 --- /dev/null +++ b/framework/vendure/lib/queries/search-query.ts @@ -0,0 +1,13 @@ +import { searchResultFragment } from '../fragments/search-result-fragment' + +export const searchQuery = /* GraphQL */ ` + query search($input: SearchInput!) { + search(input: $input) { + items { + ...SearchResult + } + totalItems + } + } + ${searchResultFragment} +` diff --git a/framework/vendure/product/get-all-product-paths.ts b/framework/vendure/product/get-all-product-paths.ts index 251f52174..dfbaff1b8 100644 --- a/framework/vendure/product/get-all-product-paths.ts +++ b/framework/vendure/product/get-all-product-paths.ts @@ -2,17 +2,8 @@ import type { GetAllProductPathsQuery, GetAllProductPathsQueryVariables, } from '../schema' -import { VendureConfig, getConfig } from '../api' - -export const getAllProductPathsQuery = /* GraphQL */ ` - query getAllProductPaths($first: Int = 100) { - products(options: { take: $first }) { - items { - slug - } - } - } -` +import { getConfig, VendureConfig } from '../api' +import { getAllProductPathsQuery } from '../lib/queries/get-all-product-paths-query' export type GetAllProductPathsResult = { products: Array<{ node: { path: string } }> diff --git a/framework/vendure/product/get-all-products.ts b/framework/vendure/product/get-all-products.ts index 447dd4d2f..b292e0246 100644 --- a/framework/vendure/product/get-all-products.ts +++ b/framework/vendure/product/get-all-products.ts @@ -1,19 +1,8 @@ import { Product } from '@commerce/types' import { getConfig, VendureConfig } from '../api' -import { searchResultFragment } from '../api/fragments/search-result' import { GetAllProductsQuery } from '../schema' import { normalizeSearchResult } from '../lib/normalize' - -export const getAllProductsQuery = /* GraphQL */ ` - query getAllProducts($input: SearchInput!) { - search(input: $input) { - items { - ...SearchResult - } - } - } - ${searchResultFragment} -` +import { getAllProductsQuery } from '../lib/queries/get-all-products-query' export type ProductVariables = { first?: number } diff --git a/framework/vendure/product/get-product.ts b/framework/vendure/product/get-product.ts index 61be2c3f6..735164ec1 100644 --- a/framework/vendure/product/get-product.ts +++ b/framework/vendure/product/get-product.ts @@ -1,48 +1,7 @@ import { Product } from '@commerce/types' import { getConfig, VendureConfig } from '../api' -import { GetProductQuery } from '@framework/schema' - -export const getProductQuery = /* GraphQL */ ` - query getProduct($slug: String!) { - product(slug: $slug) { - id - name - slug - description - assets { - id - preview - name - } - variants { - id - priceWithTax - currencyCode - options { - id - name - code - groupId - group { - id - options { - name - } - } - } - } - optionGroups { - id - code - name - options { - id - name - } - } - } - } -` +import { GetProductQuery } from '../schema' +import { getProductQuery } from '../lib/queries/get-product-query' async function getProduct({ query = getProductQuery, diff --git a/framework/vendure/product/use-search.tsx b/framework/vendure/product/use-search.tsx index 3c285e26c..00d9db36c 100644 --- a/framework/vendure/product/use-search.tsx +++ b/framework/vendure/product/use-search.tsx @@ -2,20 +2,8 @@ import { SWRHook } from '@commerce/utils/types' import useSearch, { UseSearch } from '@commerce/product/use-search' import { Product } from '@commerce/types' import { SearchQuery, SearchQueryVariables } from '../schema' -import { searchResultFragment } from '../api/fragments/search-result' import { normalizeSearchResult } from '../lib/normalize' - -export const searchQuery = /* GraphQL */ ` - query search($input: SearchInput!) { - search(input: $input) { - items { - ...SearchResult - } - totalItems - } - } - ${searchResultFragment} -` +import { searchQuery } from '../lib/queries/search-query' export default useSearch as UseSearch