mirror of
https://github.com/vercel/commerce.git
synced 2025-06-09 01:36:57 +00:00
42 lines
815 B
TypeScript
42 lines
815 B
TypeScript
import { ProductEdge } from '../../schema'
|
|
import { ReactionCommerceConfig } from '..'
|
|
|
|
const fetchAllProducts = async ({
|
|
config,
|
|
query,
|
|
variables,
|
|
acc = [],
|
|
cursor,
|
|
}: {
|
|
config: ReactionCommerceConfig
|
|
query: string
|
|
acc?: ProductEdge[]
|
|
variables?: any
|
|
cursor?: string
|
|
}): Promise<ProductEdge[]> => {
|
|
const { data } = await config.fetch(query, {
|
|
variables: { ...variables, cursor },
|
|
})
|
|
|
|
const edges: ProductEdge[] = data.products?.edges ?? []
|
|
const hasNextPage = data.products?.pageInfo?.hasNextPage
|
|
acc = acc.concat(edges)
|
|
|
|
if (hasNextPage) {
|
|
const cursor = edges.pop()?.cursor
|
|
if (cursor) {
|
|
return fetchAllProducts({
|
|
config,
|
|
query,
|
|
variables,
|
|
acc,
|
|
cursor,
|
|
})
|
|
}
|
|
}
|
|
|
|
return acc
|
|
}
|
|
|
|
export default fetchAllProducts
|