Skip to content

Instantly share code, notes, and snippets.

@peterjaap
Last active November 21, 2018 10:03
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save peterjaap/4de0e4543bc1363457165370b41ce68a to your computer and use it in GitHub Desktop.
Save peterjaap/4de0e4543bc1363457165370b41ce68a to your computer and use it in GitHub Desktop.
Magento 2.3 GraphQL all products query for Gatsby
const _ = require('lodash')
const { GraphQLClient } = require('graphql-request')
const path = require('path')
const crypto = require('crypto')
const baseUrl = 'https://www.yourmagento2shop.com/'
const { createRemoteFileNode } = require(`gatsby-source-filesystem`)
// Query Magento 2.3
const allProductsQuery = `{
allProducts: products (
filter:{sku:{like:"%"}}
pageSize:10000
)
{
total_count
items {
id
name
image
sku
url_key
categories {
id
name
url_key
}
description
short_description
meta_title
stock_status
product_links {
sku
link_type
linked_product_sku
linked_product_type
position
}
price {
regularPrice {
amount {
value
currency
}
}
}
}
}
}`
exports.createPages = ({graphql, actions}) => {
const {createPage} = actions
return new Promise((resolve, reject) => {
const client = new GraphQLClient(baseUrl + 'graphql', {});
const allProducts = client.request(allProductsQuery);
const productComponent = path.resolve('./src/templates/product.js')
resolve(
allProducts.then((result) => {
_.each(result.allProducts.items, (product, index) => {
createPage({
path: product.url_key,
component: productComponent,
context: {
sku: product.sku
},
})
})
}
)
)
})
}
exports.sourceNodes = async (
{ actions, createNodeId, createContentDigest },
configOptions
) => {
const { createNode } = actions
const processProduct = ({ included, product, options }) => {
const nodeId = createNodeId(`magento-product-${product.id}`)
const nodeContent = JSON.stringify(product)
const nodeContentDigest = createContentDigest(nodeContent)
return Object.assign({}, product, {
id: nodeId,
originalId: product.id,
included,
includedData: { ...options },
parent: null,
children: [],
internal: {
type: `MagentoProducts`,
content: nodeContent,
contentDigest: nodeContentDigest,
},
})
}
const client = new GraphQLClient(baseUrl + 'graphql', {});
const allProducts = client.request(allProductsQuery);
return new Promise((resolve, reject) => {
allProducts.then((result) => {
result.allProducts.items.forEach(product => {
const {included} = result.allProducts;
const options = [];
const nodeData = processProduct({included, product, options});
createNode(nodeData)
})
})
resolve()
})
}
exports.onCreateNode = async ({ node, actions, cache, store }) => {
if (node.internal.type !== "MagentoProducts") {
return
}
const {createNode} = actions
let fullImagePath = baseUrl + 'media/catalog/product' + node.image;
const fileNode = await createRemoteFileNode({
url: fullImagePath,
store,
cache,
createNode,
createNodeId: id => `magento-image-sharp-${id}`,
});
if (fileNode) {
node.image___NODE = fileNode.id
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment