Skip to content

Instantly share code, notes, and snippets.

@apotox
Last active March 18, 2020 19:38
Show Gist options
  • Save apotox/6b8f94e3c0e9d19ece43cb06acbdec70 to your computer and use it in GitHub Desktop.
Save apotox/6b8f94e3c0e9d19ece43cb06acbdec70 to your computer and use it in GitHub Desktop.
a gatsby-source plugin example
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