From 4c43278e67a2d5b59a57e8d9290dad05d41a7878 Mon Sep 17 00:00:00 2001 From: Luis Alvarez Date: Sat, 10 Oct 2020 14:19:05 -0500 Subject: [PATCH] Added brands --- .../api/operations/get-all-product-paths.ts | 9 ++-- .../api/operations/get-all-products.ts | 9 ++-- .../api/operations/get-site-info.ts | 41 ++++++++++++++++++- lib/bigcommerce/api/utils/filter-edges.ts | 5 +++ lib/bigcommerce/schema.d.ts | 31 ++++++++++++++ pages/index.tsx | 15 ++++++- pages/product/[slug].tsx | 2 +- 7 files changed, 101 insertions(+), 11 deletions(-) create mode 100644 lib/bigcommerce/api/utils/filter-edges.ts diff --git a/lib/bigcommerce/api/operations/get-all-product-paths.ts b/lib/bigcommerce/api/operations/get-all-product-paths.ts index 7a9f49b23..278fb78fe 100644 --- a/lib/bigcommerce/api/operations/get-all-product-paths.ts +++ b/lib/bigcommerce/api/operations/get-all-product-paths.ts @@ -1,5 +1,6 @@ import type { GetAllProductPathsQuery } from 'lib/bigcommerce/schema' import type { RecursivePartial, RecursiveRequired } from '../utils/types' +import filterEdges from '../utils/filter-edges' import { BigcommerceConfig, getConfig } from '..' export const getAllProductPathsQuery = /* GraphQL */ ` @@ -16,10 +17,12 @@ export const getAllProductPathsQuery = /* GraphQL */ ` } ` -export type ProductPaths = NonNullable< - GetAllProductPathsQuery['site']['products']['edges'] +export type ProductPath = NonNullable< + NonNullable[0] > +export type ProductPaths = ProductPath[] + export type GetAllProductPathsResult< T extends { products: any[] } = { products: ProductPaths } > = T @@ -52,7 +55,7 @@ async function getAllProductPaths({ const products = data.site?.products?.edges return { - products: (products as RecursiveRequired) ?? [], + products: filterEdges(products as RecursiveRequired), } } diff --git a/lib/bigcommerce/api/operations/get-all-products.ts b/lib/bigcommerce/api/operations/get-all-products.ts index db7dd406a..b584d0dd9 100644 --- a/lib/bigcommerce/api/operations/get-all-products.ts +++ b/lib/bigcommerce/api/operations/get-all-products.ts @@ -3,6 +3,7 @@ import type { GetAllProductsQueryVariables, } from 'lib/bigcommerce/schema' import type { RecursivePartial, RecursiveRequired } from '../utils/types' +import filterEdges from '../utils/filter-edges' import { productInfoFragment } from '../fragments/product' import { BigcommerceConfig, getConfig, Images, ProductImageVariables } from '..' @@ -37,10 +38,12 @@ export const getAllProductsQuery = /* GraphQL */ ` ${productInfoFragment} ` -export type Products = NonNullable< - GetAllProductsQuery['site']['products']['edges'] +export type Product = NonNullable< + NonNullable[0] > +export type Products = Product[] + export type GetAllProductsResult< T extends { products: any[] } = { products: Products } > = T @@ -82,7 +85,7 @@ async function getAllProducts({ const products = data.site?.products?.edges return { - products: (products as RecursiveRequired) ?? [], + products: filterEdges(products as RecursiveRequired), } } diff --git a/lib/bigcommerce/api/operations/get-site-info.ts b/lib/bigcommerce/api/operations/get-site-info.ts index 85929e9a0..a992ba980 100644 --- a/lib/bigcommerce/api/operations/get-site-info.ts +++ b/lib/bigcommerce/api/operations/get-site-info.ts @@ -3,6 +3,7 @@ import type { GetSiteInfoQueryVariables, } from 'lib/bigcommerce/schema' import type { RecursivePartial, RecursiveRequired } from '../utils/types' +import filterEdges from '../utils/filter-edges' import { BigcommerceConfig, getConfig } from '..' import { categoryTreeItemFragment } from '../fragments/category-tree' @@ -19,6 +20,28 @@ export const getSiteInfoQuery = /* GraphQL */ ` } } } + brands { + pageInfo { + startCursor + endCursor + } + edges { + cursor + node { + entityId + name + defaultImage { + urlOriginal + altText + } + pageTitle + metaDesc + metaKeywords + searchKeywords + path + } + } + } } } ${categoryTreeItemFragment} @@ -28,8 +51,17 @@ export type CategoriesTree = NonNullable< GetSiteInfoQuery['site']['categoryTree'] > +export type BrandEdge = NonNullable< + NonNullable[0] +> + +export type Brands = BrandEdge[] + export type GetSiteInfoResult< - T extends { categories: any[] } = { categories: CategoriesTree } + T extends { categories: any[]; brands: any[] } = { + categories: CategoriesTree + brands: Brands + } > = T async function getSiteInfo(opts?: { @@ -37,7 +69,10 @@ async function getSiteInfo(opts?: { config?: BigcommerceConfig }): Promise -async function getSiteInfo(opts: { +async function getSiteInfo< + T extends { categories: any[]; brands: any[] }, + V = any +>(opts: { query: string variables?: V config?: BigcommerceConfig @@ -59,9 +94,11 @@ async function getSiteInfo({ variables, }) const categories = data.site?.categoryTree + const brands = data.site?.brands?.edges return { categories: (categories as RecursiveRequired) ?? [], + brands: filterEdges(brands as RecursiveRequired), } } diff --git a/lib/bigcommerce/api/utils/filter-edges.ts b/lib/bigcommerce/api/utils/filter-edges.ts new file mode 100644 index 000000000..09cd20640 --- /dev/null +++ b/lib/bigcommerce/api/utils/filter-edges.ts @@ -0,0 +1,5 @@ +export default function filterEdges( + edges: (T | null | undefined)[] | null | undefined +) { + return edges?.filter((edge): edge is T => !!edge) ?? [] +} diff --git a/lib/bigcommerce/schema.d.ts b/lib/bigcommerce/schema.d.ts index 5741f32cd..0bf3d42f0 100644 --- a/lib/bigcommerce/schema.d.ts +++ b/lib/bigcommerce/schema.d.ts @@ -1830,5 +1830,36 @@ export type GetSiteInfoQuery = { __typename?: 'Query' } & { > } & CategoryTreeItemFragment > + brands: { __typename?: 'BrandConnection' } & { + pageInfo: { __typename?: 'PageInfo' } & Pick< + PageInfo, + 'startCursor' | 'endCursor' + > + edges?: Maybe< + Array< + Maybe< + { __typename?: 'BrandEdge' } & Pick & { + node: { __typename?: 'Brand' } & Pick< + Brand, + | 'entityId' + | 'name' + | 'pageTitle' + | 'metaDesc' + | 'metaKeywords' + | 'searchKeywords' + | 'path' + > & { + defaultImage?: Maybe< + { __typename?: 'Image' } & Pick< + Image, + 'urlOriginal' | 'altText' + > + > + } + } + > + > + > + } } } diff --git a/pages/index.tsx b/pages/index.tsx index 4eb0b0706..7f619be02 100644 --- a/pages/index.tsx +++ b/pages/index.tsx @@ -7,16 +7,17 @@ import getSiteInfo from '@lib/bigcommerce/api/operations/get-site-info' export async function getStaticProps({ preview }: GetStaticPropsContext) { const { products } = await getAllProducts() - const { categories } = await getSiteInfo() + const { categories, brands } = await getSiteInfo() return { - props: { products, categories }, + props: { products, categories, brands }, } } export default function Home({ products, categories, + brands, }: InferGetStaticPropsType) { return ( <> @@ -53,6 +54,16 @@ export default function Home({ ))} +
    +
  • +

    All Designers

    +
  • + {brands.flatMap(({ node }) => ( +
  • + {node.name} +
  • + ))} +
`/product${product!.node.path}`), + paths: products.map((product) => `/product${product.node.path}`), fallback: false, } }