Closed PHironaka closed 5 years ago
closing this issue. The problem was w/the nodeId in my helper, it was just pulling in the id and not the store object.
change to my gatsby-node.js file below helped solve the issue, thank you @LekoArts for the assist!
const fetch = require("node-fetch")
const queryString = require("query-string")
exports.sourceNodes = (
{ actions, createNodeId, createContentDigest },
configOptions
) => {
const { createNode } = actions
// Gatsby adds a configOption that's not needed for this plugin, delete it
delete configOptions.plugins
// // Helper function that processes a photo to match Gatsby's node structure
const processShop = (id, shop) => {
const nodeId = createNodeId(`store-locator-widget-photo-${id}`)
const nodeContent = JSON.stringify(shop)
const nodeData = Object.assign({}, shop, {
id: nodeId,
parent: nodeId,
children: {},
internal: {
type: `StoreLocatorWidgetId`,
content: nodeContent,
contentDigest: createContentDigest(shop),
},
})
return nodeData
}
// Convert the options object into a query string
const apiOptions = queryString.stringify(configOptions)
// Join apiOptions with the Pixabay API URL
const apiUrl = `https://www.storelocatorwidgets.com/admin/api/v1/locations?${apiOptions}`
// Gatsby expects sourceNodes to return a promise
return (
// Fetch a response from the apiUrl
fetch(apiUrl)
// Parse the response as JSON
.then(response => response.json())
// Process the response data into a node
.then(data => {
Object.entries(data.locations).forEach(([key, shop]) => {
console.log(shop)
const nodeData = processShop(key, shop)
createNode(nodeData)
})
})
)
}
Summary
I'm building a Gatsby source plugin for the first time and running into issues fetching data. I was using this tutorial for guidance, but the way my API is structured is different.
I'm using the storelocatorwidget's API.
I would like to be able to query all the data inside each store's id (which is an object).
I'm able to find each store id (in the example above that would be '13466445'), but I would also like to query all the nodes inside that object id.
I've included both the gatsby-config.js file as well as the plugin's gatsby-node.js file for reference. Any tips would be appreciated.
Relevant information
Here is a sampling of store data that gets returned, for context on how their data is structured:
Environment (if relevant)
File contents (if changed)
gatsby-config.js
:package.json
: N/Agatsby-node.js
:gatsby-browser.js
: N/Agatsby-ssr.js
: N/A