import type { OperationContext, OperationOptions, } from '@commerce/api/operations' import type { GetAllProductPathsQuery } from '../../schema' import type { GetAllProductPathsOperation } from '../../types/product' import type { RecursivePartial, RecursiveRequired } from '../utils/types' import filterEdges from '../utils/filter-edges' import { BigcommerceConfig, Provider } from '..' export const getAllProductPathsQuery = /* GraphQL */ ` query getAllProductPaths($first: Int = 100) { site { products(first: $first) { edges { node { path } } } } } ` export default function getAllProductPathsOperation({ commerce, }: OperationContext) { async function getAllProductPaths< T extends GetAllProductPathsOperation >(opts?: { variables?: T['variables'] config?: BigcommerceConfig }): Promise async function getAllProductPaths( opts: { variables?: T['variables'] config?: BigcommerceConfig } & OperationOptions ): Promise async function getAllProductPaths({ query = getAllProductPathsQuery, variables, config, }: { query?: string variables?: T['variables'] config?: BigcommerceConfig } = {}): Promise { config = commerce.getConfig(config) // RecursivePartial forces the method to check for every prop in the data, which is // required in case there's a custom `query` const { data } = await config.fetch< RecursivePartial >(query, { variables }) const products = data.site?.products?.edges return { products: filterEdges(products as RecursiveRequired).map( ({ node }) => node ), } } return getAllProductPaths }