diff --git a/codegen.json b/codegen.json index dfd125f82..c2f13e69b 100644 --- a/codegen.json +++ b/codegen.json @@ -8,7 +8,7 @@ }, "documents": [ { - "./lib/bigcommerce/api/operations/**/*.ts": { + "./lib/bigcommerce/api/{operations,fragments}/**/*.ts": { "noRequire": true } } diff --git a/lib/bigcommerce/api/fragments/product.ts b/lib/bigcommerce/api/fragments/product.ts new file mode 100644 index 000000000..48c0df36d --- /dev/null +++ b/lib/bigcommerce/api/fragments/product.ts @@ -0,0 +1,66 @@ +export const responsiveImageFragment = /* GraphQL */ ` + fragment responsiveImage on Image { + urlSmall: url(width: $imgSmallWidth, height: $imgSmallHeight) + urlMedium: url(width: $imgMediumWidth, height: $imgMediumHeight) + urlLarge: url(width: $imgLargeWidth, height: $imgLargeHeight) + urlXL: url(width: $imgXLWidth, height: $imgXLHeight) + } +`; + +export const productInfoFragment = /* GraphQL */ ` + fragment productInfo on Product { + entityId + name + path + brand { + name + } + description + prices { + price { + value + currencyCode + } + salePrice { + value + currencyCode + } + } + images { + edges { + node { + ...responsiveImage + } + } + } + variants { + edges { + node { + entityId + defaultImage { + ...responsiveImage + } + } + } + } + options { + edges { + node { + entityId + displayName + isRequired + values { + edges { + node { + entityId + label + } + } + } + } + } + } + } + + ${responsiveImageFragment} +`; diff --git a/lib/bigcommerce/api/operations/get-all-products.ts b/lib/bigcommerce/api/operations/get-all-products.ts index f120e2e43..5ba9c008f 100644 --- a/lib/bigcommerce/api/operations/get-all-products.ts +++ b/lib/bigcommerce/api/operations/get-all-products.ts @@ -2,7 +2,8 @@ import type { GetAllProductsQuery, GetAllProductsQueryVariables, } from 'lib/bigcommerce/schema'; -import type { RecursivePartial, RecursiveRequired } from '../types'; +import type { RecursivePartial, RecursiveRequired } from '../utils/types'; +import { productInfoFragment } from '../fragments/product'; import { getConfig, Images, ProductImageVariables } from '..'; export const getAllProductsQuery = /* GraphQL */ ` @@ -26,80 +27,14 @@ export const getAllProductsQuery = /* GraphQL */ ` edges { cursor node { - entityId - name - path - brand { - name - } - description - prices { - price { - value - currencyCode - } - salePrice { - value - currencyCode - } - } - images { - edges { - node { - urlSmall: url(width: $imgSmallWidth, height: $imgSmallHeight) - urlMedium: url( - width: $imgMediumWidth - height: $imgMediumHeight - ) - urlLarge: url(width: $imgLargeWidth, height: $imgLargeHeight) - urlXL: url(width: $imgXLWidth, height: $imgXLHeight) - } - } - } - variants { - edges { - node { - entityId - defaultImage { - urlSmall: url( - width: $imgSmallWidth - height: $imgSmallHeight - ) - urlMedium: url( - width: $imgMediumWidth - height: $imgMediumHeight - ) - urlLarge: url( - width: $imgLargeWidth - height: $imgLargeHeight - ) - urlXL: url(width: $imgXLWidth, height: $imgXLHeight) - } - } - } - } - options { - edges { - node { - entityId - displayName - isRequired - values { - edges { - node { - entityId - label - } - } - } - } - } - } + ...productInfo } } } } } + + ${productInfoFragment} `; export interface GetAllProductsResult { diff --git a/lib/bigcommerce/api/operations/get-product.ts b/lib/bigcommerce/api/operations/get-product.ts index 007959d1e..5761978c8 100644 --- a/lib/bigcommerce/api/operations/get-product.ts +++ b/lib/bigcommerce/api/operations/get-product.ts @@ -2,7 +2,8 @@ import type { GetProductQuery, GetProductQueryVariables, } from 'lib/bigcommerce/schema'; -import type { RecursivePartial, RecursiveRequired } from '../types'; +import type { RecursivePartial, RecursiveRequired } from '../utils/types'; +import { productInfoFragment } from '../fragments/product'; import { getConfig, Images, ProductImageVariables } from '..'; export const getProductQuery = /* GraphQL */ ` @@ -22,65 +23,14 @@ export const getProductQuery = /* GraphQL */ ` node { __typename ... on Product { - entityId - name - path - brand { - name - } - description - prices { - price { - currencyCode - value - } - salePrice { - currencyCode - value - } - } - images { - edges { - node { - urlSmall: url(width: $imgSmallWidth, height: $imgSmallHeight) - urlMedium: url( - width: $imgMediumWidth - height: $imgMediumHeight - ) - urlLarge: url(width: $imgLargeWidth, height: $imgLargeHeight) - urlXL: url(width: $imgXLWidth, height: $imgXLHeight) - } - } - } - variants { - edges { - node { - entityId - } - } - } - options { - edges { - node { - entityId - displayName - isRequired - values { - edges { - node { - entityId - label - } - } - } - } - } - } + ...productInfo } } } } } + + ${productInfoFragment} `; export interface GetProductResult { diff --git a/lib/bigcommerce/api/types/index.ts b/lib/bigcommerce/api/utils/types.ts similarity index 100% rename from lib/bigcommerce/api/types/index.ts rename to lib/bigcommerce/api/utils/types.ts diff --git a/lib/bigcommerce/schema.d.ts b/lib/bigcommerce/schema.d.ts index 5d8f5bd6c..5f25ff205 100644 --- a/lib/bigcommerce/schema.d.ts +++ b/lib/bigcommerce/schema.d.ts @@ -1649,6 +1649,70 @@ export enum CurrencyCode { +export type ResponsiveImageFragment = ( + { __typename?: 'Image' } + & { urlSmall: Image['url'], urlMedium: Image['url'], urlLarge: Image['url'], urlXL: Image['url'] } +); + +export type ProductInfoFragment = ( + { __typename?: 'Product' } + & Pick + & { brand?: Maybe<( + { __typename?: 'Brand' } + & Pick + )>, prices?: Maybe<( + { __typename?: 'Prices' } + & { price: ( + { __typename?: 'Money' } + & Pick + ), salePrice?: Maybe<( + { __typename?: 'Money' } + & Pick + )> } + )>, images: ( + { __typename?: 'ImageConnection' } + & { edges?: Maybe>> } + ), variants: ( + { __typename?: 'VariantConnection' } + & { edges?: Maybe + & { defaultImage?: Maybe<( + { __typename?: 'Image' } + & ResponsiveImageFragment + )> } + ) } + )>>> } + ), options: ( + { __typename?: 'OptionConnection' } + & { edges?: Maybe + & { values: ( + { __typename?: 'OptionValueConnection' } + & { edges?: Maybe + ) } + )>>> } + ) } + ) } + )>>> } + ) } +); + export type GetAllProductsQueryVariables = Exact<{ first?: Maybe; imgSmallWidth?: Maybe; @@ -1676,61 +1740,7 @@ export type GetAllProductsQuery = ( & Pick & { node: ( { __typename?: 'Product' } - & Pick - & { brand?: Maybe<( - { __typename?: 'Brand' } - & Pick - )>, prices?: Maybe<( - { __typename?: 'Prices' } - & { price: ( - { __typename?: 'Money' } - & Pick - ), salePrice?: Maybe<( - { __typename?: 'Money' } - & Pick - )> } - )>, images: ( - { __typename?: 'ImageConnection' } - & { edges?: Maybe>> } - ), variants: ( - { __typename?: 'VariantConnection' } - & { edges?: Maybe - & { defaultImage?: Maybe<( - { __typename?: 'Image' } - & { urlSmall: Image['url'], urlMedium: Image['url'], urlLarge: Image['url'], urlXL: Image['url'] } - )> } - ) } - )>>> } - ), options: ( - { __typename?: 'OptionConnection' } - & { edges?: Maybe - & { values: ( - { __typename?: 'OptionValueConnection' } - & { edges?: Maybe - ) } - )>>> } - ) } - ) } - )>>> } - ) } + & ProductInfoFragment ) } )>>> } ) } @@ -1758,57 +1768,7 @@ export type GetProductQuery = ( { __typename?: 'Route' } & { node?: Maybe<{ __typename: 'Brand' } | { __typename: 'Category' } | ( { __typename: 'Product' } - & Pick - & { brand?: Maybe<( - { __typename?: 'Brand' } - & Pick - )>, prices?: Maybe<( - { __typename?: 'Prices' } - & { price: ( - { __typename?: 'Money' } - & Pick - ), salePrice?: Maybe<( - { __typename?: 'Money' } - & Pick - )> } - )>, images: ( - { __typename?: 'ImageConnection' } - & { edges?: Maybe>> } - ), variants: ( - { __typename?: 'VariantConnection' } - & { edges?: Maybe - ) } - )>>> } - ), options: ( - { __typename?: 'OptionConnection' } - & { edges?: Maybe - & { values: ( - { __typename?: 'OptionValueConnection' } - & { edges?: Maybe - ) } - )>>> } - ) } - ) } - )>>> } - ) } + & ProductInfoFragment ) | { __typename: 'Variant' }> } ) } ) }