diff --git a/demo/gatsby-node.js b/demo/gatsby-node.js index f59155b..8ce65d7 100644 --- a/demo/gatsby-node.js +++ b/demo/gatsby-node.js @@ -1,9 +1,7 @@ exports.createPages = async ({ graphql, actions }) => { const { createPage } = actions; - const { - data: { products, categories }, - } = await graphql(` + const { data } = await graphql(` { products: allChecProduct { nodes { @@ -21,23 +19,25 @@ exports.createPages = async ({ graphql, actions }) => { } `); - products.nodes.forEach(({ id, permalink }) => - createPage({ - path: `/products/${permalink}`, - component: require.resolve(`./src/templates/ProductPage.js`), - context: { - id, - }, - }) - ); + data && + data.products.nodes.forEach(({ id, permalink }) => + createPage({ + path: `/products/${permalink}`, + component: require.resolve(`./src/templates/ProductPage.js`), + context: { + id, + }, + }) + ); - categories.nodes.forEach(({ id, slug }) => - createPage({ - path: `/categories/${slug}`, - component: require.resolve(`./src/templates/CategoryPage.js`), - context: { - id, - }, - }) - ); + data && + data.categories.nodes.forEach(({ id, slug }) => + createPage({ + path: `/categories/${slug}`, + component: require.resolve(`./src/templates/CategoryPage.js`), + context: { + id, + }, + }) + ); }; diff --git a/demo/src/pages/index.js b/demo/src/pages/index.js index 74f97be..77730e9 100644 --- a/demo/src/pages/index.js +++ b/demo/src/pages/index.js @@ -4,11 +4,17 @@ import { graphql, Link } from 'gatsby'; import ProductList from '../components/ProductList'; export default function IndexPage({ - data: { merchant, products, categories }, + data: { merchants, products, categories }, }) { return ( -

{merchant.name}

+

Merchants

+ +

Categories

@@ -29,8 +35,10 @@ export default function IndexPage({ export const pageQuery = graphql` { - merchant: checMerchant { - name: business_name + merchants: allChecMerchant { + nodes { + name: business_name + } } categories: allChecCategory { diff --git a/gatsby-source-chec/gatsby-node.js b/gatsby-source-chec/gatsby-node.js index 6cb1ed7..9b6ccab 100644 --- a/gatsby-source-chec/gatsby-node.js +++ b/gatsby-source-chec/gatsby-node.js @@ -51,19 +51,31 @@ exports.sourceNodes = async ( } }; - const { id: merchantId, ...merchant } = await commerce.get('merchants'); + const createMerchant = (merchant) => + createNode({ + ...merchant, + id: merchant.id.toString(), + internal: { + type: `ChecMerchant`, + content: JSON.stringify(merchant), + contentDigest: createContentDigest(merchant), + }, + }); + + const merchant = await commerce.get('merchants'); + + let merchants = []; + + if (merchant && merchant.id) { + merchants = [merchant]; + } else { + merchants = await fetchAllPages('merchants'); + } + const categories = await fetchAllPages('categories'); const products = await fetchAllPages('products'); - createNode({ - id: merchantId.toString(), - ...merchant, - internal: { - type: `ChecMerchant`, - content: JSON.stringify(merchant), - contentDigest: createContentDigest(merchant), - }, - }); + merchants.forEach(createMerchant); categories.forEach((category) => { const productIds = products.reduce((ids, product) => {