4
0
forked from crowetic/commerce
commerce/framework/shopify/common/get-all-pages.ts
Peter Mekhaeil 300d04c1ac
Shopify Provider (#186)
* Start of Shopify provider

* add missing comment to documentation

* add missing env vars to documentation

* update reference to types file
2021-02-12 11:14:16 -03:00

56 lines
1016 B
TypeScript

import { getConfig, ShopifyConfig } from '../api'
import { Page as PageType, PageEdge } from '../types'
export type Page = PageType
export const getAllPagesQuery = /* GraphQL */ `
query($first: Int!) {
pages(first: $first) {
edges {
node {
id
title
handle
body
bodySummary
url
}
}
}
}
`
type Variables = {
first?: number
}
type Options = {
variables?: Variables
config: ShopifyConfig
preview?: boolean
}
type ReturnType = {
pages: Page[]
}
const getAllPages = async (options?: Options): Promise<ReturnType> => {
let { config, variables = { first: 250 } } = options || {}
config = getConfig(config)
const { data } = await config.fetch(getAllPagesQuery, { variables })
const pages = data.pages.edges.map(({ node }: PageEdge) => {
return {
...node,
name: node.handle,
url: `${config!.locale}/${node.handle}`,
}
})
return { pages }
}
export default getAllPages