Skip to content

Instantly share code, notes, and snippets.

@JordanDDisch
Created December 21, 2018 17:47
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JordanDDisch/5fa7f3972b9a4ff91cb7469c01eea1a6 to your computer and use it in GitHub Desktop.
Save JordanDDisch/5fa7f3972b9a4ff91cb7469c01eea1a6 to your computer and use it in GitHub Desktop.
const fetch = require('node-fetch')
const crypto = require('crypto')
const path = require('path')
const fs = require('fs');
const _ = require('lodash');
exports.sourceNodes = async ({ actions }) => {
const { createNode } = actions
const createProduct = (var1, var2) => {
// do stuff to create the product object
return product
}
var fetchNow = function() {
// fetch data
createNode(createProduct(var1, var2))
}
return
}
// Implement the Gatsby API “createPages”. This is called once the
// data layer is bootstrapped to let plugins create pages from data.
exports.createPages = ({ actions, graphql }) => {
const { createPage } = actions;
return new Promise((resolve, reject) => {
// Query for products to use in creating pages.
resolve(
graphql(
`
{
allProduct {
edges {
// query
}
}
}
`
).then(result => {
if (result.errors) {
console.log(result.errors);
reject(result.errors);
}
const allProducts = result.data.allProduct.edges
// Create pages for each product.
allProducts.forEach(({ node }) => {
const decodedSlug = decodeURIComponent(node.slug)
const id = node.id
createPage({
path: `/product/${decodedSlug}`,
component: path.resolve(`src/templates/product-detail.js`),
context: {
data1,
data2,
data3 // ...etc
},
});
});
})
);
});
};
const productQuery = `{
allProduct {
edges {
// query
}
}
}`;
import React from 'react'
const ProductDetailTemplate = ({ pageContext, data, location }) => {
console.log(pageContext.data1, pageContext.data2, pageContext.data3)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment