commerce/framework/vendure/common/get-site-info.ts
2021-01-25 21:05:03 +01:00

60 lines
1.3 KiB
TypeScript

import { VendureConfig, getConfig } from '../api'
import { GetCollectionsQuery } from '@framework/schema'
import { arrayToTree } from '@framework/lib/array-to-tree';
export const getCollectionsQuery = /* GraphQL */ `
query getCollections {
collections {
items {
id
name
description
slug
productVariants {
totalItems
}
parent {
id
}
children {
id
}
}
}
}
`;
async function getSiteInfo({
query = getCollectionsQuery,
variables,
config,
}: {
query?: string
variables?: any
config?: VendureConfig
preview?: boolean
} = {}): Promise<any> {
config = 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<GetCollectionsQuery>(
query,
{ variables }
)
const categories = arrayToTree(data.collections?.items.map(i => ({
...i,
entityId: i.id,
path: i.slug,
productCount: i.productVariants.totalItems,
}))).children;
const brands = [] as any[];
return {
categories: categories ?? [],
brands,
}
}
export default getSiteInfo