Created
December 21, 2018 17:47
-
-
Save JordanDDisch/5fa7f3972b9a4ff91cb7469c01eea1a6 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} | |
} | |
}`; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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