Last active
March 18, 2020 19:38
-
-
Save apotox/6b8f94e3c0e9d19ece43cb06acbdec70 to your computer and use it in GitHub Desktop.
a gatsby-source plugin example
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 { api_url } = require("./config"); | |
exports.sourceNodes = async ( | |
{ reporter, actions, createNodeId, createContentDigest }, | |
options | |
) => { | |
if (process.env.NODE_ENV == "development") { | |
reporter.warn("Welcome to MYCustomer Plugin"); | |
} | |
const { createNode } = actions; | |
// Create nodes here, generally by downloading data | |
// from a remote API. | |
const resp = await fetch(`${api_url}/users`); | |
const data = await resp.json(); | |
data.forEach(customerData => { | |
const nodeContent = JSON.stringify(customerData); | |
const nodeMeta = { | |
// the customer unique id is in _id | |
id: createNodeId(`customer-${customerData.id}`), | |
parent: null, | |
children: [], | |
internal: { | |
// this will be important in finding the node | |
type: `customer`, | |
content: nodeContent, | |
contentDigest: createContentDigest(customerData) | |
} | |
}; | |
const node = Object.assign({}, customerData, nodeMeta); | |
console.log("\n customer id --> ", customerData.id); | |
createNode(node); | |
}); // We're done, return. | |
return; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment