From c92e7658145051cb371a595a75937a8b540c1e4d Mon Sep 17 00:00:00 2001 From: Luis Alvarez Date: Sun, 18 Oct 2020 17:52:07 -0500 Subject: [PATCH] Fetch products separately --- .../api/operations/get-all-products.ts | 80 +++++++------------ lib/bigcommerce/schema.d.ts | 74 +++-------------- pages/index.tsx | 3 + 3 files changed, 45 insertions(+), 112 deletions(-) diff --git a/lib/bigcommerce/api/operations/get-all-products.ts b/lib/bigcommerce/api/operations/get-all-products.ts index e83eb7464..796c10585 100644 --- a/lib/bigcommerce/api/operations/get-all-products.ts +++ b/lib/bigcommerce/api/operations/get-all-products.ts @@ -19,27 +19,22 @@ export const getAllProductsQuery = /* GraphQL */ ` $imgLargeHeight: Int $imgXLWidth: Int = 1280 $imgXLHeight: Int + $products: Boolean = false $featuredProducts: Boolean = false - $featuredProducts__first: Int = 10 $bestSellingProducts: Boolean = false - $bestSellingProducts__first: Int = 10 $newestProducts: Boolean = false - $newestProducts__first: Int = 10 ) { site { - products(first: $first, entityIds: $entityIds) { + products(first: $first, entityIds: $entityIds) @include(if: $products) { ...productConnnection } - featuredProducts(first: $featuredProducts__first) - @include(if: $featuredProducts) { + featuredProducts(first: $first) @include(if: $featuredProducts) { ...productConnnection } - bestSellingProducts(first: $bestSellingProducts__first) - @include(if: $bestSellingProducts) { + bestSellingProducts(first: $first) @include(if: $bestSellingProducts) { ...productConnnection } - newestProducts(first: $newestProducts__first) - @include(if: $newestProducts) { + newestProducts(first: $first) @include(if: $newestProducts) { ...productConnnection } } @@ -55,20 +50,17 @@ export type Product = NonNullable< export type Products = Product[] export type GetAllProductsResult< - T extends Record = { - products: Products - featuredProducts: Products - bestSellingProducts: Products - newestProducts: Products - } + T extends Record = { products: Products } > = T -export type ProductVariables = { - featured?: boolean | { first?: number } - bestSelling?: boolean | { first?: number } - newest?: boolean | { first?: number } -} & Images & - Omit +export type ProductTypes = + | 'products' + | 'featuredProducts' + | 'bestSellingProducts' + | 'newestProducts' + +export type ProductVariables = { field?: ProductTypes } & Images & + Omit async function getAllProducts(opts?: { variables?: ProductVariables @@ -86,7 +78,7 @@ async function getAllProducts< async function getAllProducts({ query = getAllProductsQuery, - variables: { featured, bestSelling, newest, ...vars } = {}, + variables: { field = 'products', ...vars } = {}, config, }: { query?: string @@ -94,28 +86,25 @@ async function getAllProducts({ config?: BigcommerceConfig } = {}): Promise { config = getConfig(config) + const variables: GetAllProductsQueryVariables = { ...config.imageVariables, ...vars, } - if (bestSelling) { - variables.bestSellingProducts = true - if (typeof bestSelling === 'object' && bestSelling.first) { - variables.bestSellingProducts__first = bestSelling.first - } - } - if (featured) { - variables.featuredProducts = true - if (typeof featured === 'object' && featured.first) { - variables.featuredProducts__first = featured.first - } - } - if (newest) { - variables.newestProducts = true - if (typeof newest === 'object' && newest.first) { - variables.newestProducts__first = newest.first - } + switch (field) { + case 'products': + variables.products = true + break + case 'featuredProducts': + variables.featuredProducts = true + break + case 'bestSellingProducts': + variables.bestSellingProducts = true + break + case 'newestProducts': + variables.newestProducts = true + break } // RecursivePartial forces the method to check for every prop in the data, which is @@ -124,17 +113,10 @@ async function getAllProducts({ query, { variables } ) - const products = data.site?.products?.edges - - type P = RecursiveRequired + const products = data.site?.[field]?.edges return { - products: filterEdges(products as P), - featuredProducts: filterEdges(data.site?.featuredProducts?.edges as P), - bestSellingProducts: filterEdges( - data.site?.bestSellingProducts?.edges as P - ), - newestProducts: filterEdges(data.site?.newestProducts?.edges as P), + products: filterEdges(products as RecursiveRequired), } } diff --git a/lib/bigcommerce/schema.d.ts b/lib/bigcommerce/schema.d.ts index 4d13e92d3..e9141337b 100644 --- a/lib/bigcommerce/schema.d.ts +++ b/lib/bigcommerce/schema.d.ts @@ -1832,76 +1832,24 @@ export type GetAllProductsQueryVariables = Exact<{ imgLargeHeight?: Maybe imgXLWidth?: Maybe imgXLHeight?: Maybe + products?: Maybe featuredProducts?: Maybe - featuredProducts__first?: Maybe bestSellingProducts?: Maybe - bestSellingProducts__first?: Maybe newestProducts?: Maybe - newestProducts__first?: Maybe }> export type GetAllProductsQuery = { __typename?: 'Query' } & { site: { __typename?: 'Site' } & { - products: { __typename?: 'ProductConnection' } & { - pageInfo: { __typename?: 'PageInfo' } & Pick< - PageInfo, - 'startCursor' | 'endCursor' - > - edges?: Maybe< - Array< - Maybe< - { __typename?: 'ProductEdge' } & Pick & { - node: { __typename?: 'Product' } & ProductInfoFragment - } - > - > - > - } - featuredProducts: { __typename?: 'ProductConnection' } & { - pageInfo: { __typename?: 'PageInfo' } & Pick< - PageInfo, - 'startCursor' | 'endCursor' - > - edges?: Maybe< - Array< - Maybe< - { __typename?: 'ProductEdge' } & Pick & { - node: { __typename?: 'Product' } & ProductInfoFragment - } - > - > - > - } - bestSellingProducts: { __typename?: 'ProductConnection' } & { - pageInfo: { __typename?: 'PageInfo' } & Pick< - PageInfo, - 'startCursor' | 'endCursor' - > - edges?: Maybe< - Array< - Maybe< - { __typename?: 'ProductEdge' } & Pick & { - node: { __typename?: 'Product' } & ProductInfoFragment - } - > - > - > - } - newestProducts: { __typename?: 'ProductConnection' } & { - pageInfo: { __typename?: 'PageInfo' } & Pick< - PageInfo, - 'startCursor' | 'endCursor' - > - edges?: Maybe< - Array< - Maybe< - { __typename?: 'ProductEdge' } & Pick & { - node: { __typename?: 'Product' } & ProductInfoFragment - } - > - > - > - } + products: { __typename?: 'ProductConnection' } & ProductConnnectionFragment + featuredProducts: { + __typename?: 'ProductConnection' + } & ProductConnnectionFragment + bestSellingProducts: { + __typename?: 'ProductConnection' + } & ProductConnnectionFragment + newestProducts: { + __typename?: 'ProductConnection' + } & ProductConnnectionFragment } } diff --git a/pages/index.tsx b/pages/index.tsx index 04b3e9ef5..d0b0be8c7 100644 --- a/pages/index.tsx +++ b/pages/index.tsx @@ -9,6 +9,9 @@ import getAllPages from '@lib/bigcommerce/api/operations/get-all-pages' export async function getStaticProps({ preview }: GetStaticPropsContext) { const { pages } = await getAllPages() const { products } = await getAllProducts() + // const { products: featuredProducts } = await getAllProducts({ + // variables: { field: 'featuredProducts' }, + // }) const { categories, brands } = await getSiteInfo() return {