Second day I can't seem to fix this issue at all.
Graphql spits only 20 nodes, while it is supposed to be 2480 which are created during sourceNode / createNode from an array of objects.
on SourceNodes step:
pulling data from API, 2 objects arrays: Products and Prices
Creating Nodes.
Console logging shows 2480 objects in the arrays.
I couldn't find any callbacks for successful createNode, so I just console logged for-of with count.
Fires 2480 times, yet in grapql the totalcount is 20, for both objects (Products and Prices)
Relevant information
This is the code that ought to do the job:
source-plugin:
const axios = require('axios');
exports.onPreInit = () => console.log("Loaded gatsby-starter-plugin")
exports.sourceNodes = async ({ actions, createNodeId, createContentDigest }) => {
const {
createNode,
createTypes,
schema } = actions
//
// Fetching access token
//
const token = await axios({
method: 'POST',
url: `${process.env.COMMERCE_LAYER_API}/oauth/token`,
headers: {
'Content-Type': 'application/json',
},
data: {
grant_type: "client_credentials",
client_id: `${process.env.CLIENTID}`,
client_secret: null,
scope: `${process.env.MARKET}`
}
})
//
// Fetching and paginating
//
const fetchCL = async (url) => {
let x = await axios({
method: 'GET',
url: url,
headers: {
'Accept': 'application/vnd.api+json',
'Authorization': `Bearer ${token.data.access_token}`
}
})
return x.data
}
let productsPageCount = 1
let currentPage = 1
const handleFetchCL = async url => {
let productsData = []
const x = await fetchCL(url)
productsData.push(...x.data)
console.log(productsData)
productsPageCount = x.meta.page_count
while (currentPage < productsPageCount) {
let y = await fetchCL(x.links.next)
productsData.push(...y.data)
currentPage++
console.log('Page', currentPage, productsData.length)
}
console.log('Done', currentPage, productsData.length)
// resetting the current Page for future fetches
currentPage = 1
return productsData
}
//
// Fetching products and Prices
//
const products = await handleFetchCL(`${process.env.COMMERCE_LAYER_API}/api/skus?include=prices`)
const prices = await handleFetchCL(`${process.env.COMMERCE_LAYER_API}/api/prices`)
console.log('JEL SAM SE POMAKO?')
//
// Creating Nodes
//
let productCount = 0
for await (const productAttributes of products) {
await createNode({
// Data for the node.
code: productAttributes.attributes.code,
reference: productAttributes.attributes.reference,
// ...arbitraryOtherData,
...productAttributes,
// Required fields.
id: createNodeId(productAttributes.id),
parent: null, // or null if it's a source node without a parent
children: [],
internal: {
type: 'productCommerceLayer',
mediaType: `text/html`,
content: JSON.stringify(productAttributes),
contentDigest: createContentDigest(productAttributes)
}
})
productCount++
console.log('+_+_+_+', productCount)
}
for await (const productPrices of prices) {
await createNode({
// Data for the node.
skuCode: productPrices.attributes.sku_code,
amountFloat: productPrices.attributes.amount_float,
compareAt: productPrices.attributes.compare_at_amount_float,
// ...arbitraryOtherData,
// Required fields.
id: createNodeId(productPrices.id),
parent: null, // or null if it's a source node without a parent
children: [],
internal: {
type: 'pricesCommerceLayer',
contentDigest: createContentDigest(productPrices.attributes),
},
})
}
return
};
Initially code was located in gatsby-node.
After spending 7 hours hawking and trying to implement a solution, I've made a plugin thinking (in a superstitious manner) that it might just start working.
Summary
Second day I can't seem to fix this issue at all. Graphql spits only 20 nodes, while it is supposed to be 2480 which are created during sourceNode / createNode from an array of objects.
on SourceNodes step:
Console logging shows 2480 objects in the arrays. I couldn't find any callbacks for successful createNode, so I just console logged for-of with count. Fires 2480 times, yet in grapql the totalcount is 20, for both objects (Products and Prices)
Relevant information
This is the code that ought to do the job:
source-plugin:
Initially code was located in gatsby-node. After spending 7 hours hawking and trying to implement a solution, I've made a plugin thinking (in a superstitious manner) that it might just start working.
Environment (if relevant)
File contents (if changed)
gatsby-config:
Else didn't think it relevant will provide if needed!