commerce/framework/vendure/product/get-all-products.ts
2021-05-27 12:27:22 +02:00

50 lines
1.2 KiB
TypeScript

import { getConfig, VendureConfig } from '../api'
import { searchResultFragment } from '@framework/api/fragments/search-result'
import { GetAllProductsQuery } from '@framework/schema'
import { normalizeSearchResult } from '@framework/lib/normalize'
export const getAllProductsQuery = /* GraphQL */ `
query getAllProducts($input: SearchInput!) {
search(input: $input) {
items {
...SearchResult
}
}
}
${searchResultFragment}
`
export type ProductVariables = { first?: number }
async function getAllProducts(opts?: {
variables?: ProductVariables
config?: VendureConfig
preview?: boolean
}): Promise<{ products: Product[] }>
async function getAllProducts({
query = getAllProductsQuery,
variables: { ...vars } = {},
config,
}: {
query?: string
variables?: ProductVariables
config?: VendureConfig
preview?: boolean
} = {}): Promise<{ products: Product[] | any[] }> {
config = getConfig(config)
const variables = {
input: {
take: vars.first,
groupByProduct: true,
},
}
const { data } = await config.fetch<GetAllProductsQuery>(query, { variables })
return {
products: data.search.items.map((item) => normalizeSearchResult(item)),
}
}
export default getAllProducts