JSPM

  • Created
  • Published
  • 0
  • Score
    100M100P100Q55588F
  • License MIT

Gatsby plugin which fetches store data from Magento2

Package Exports

  • gatsby-source-magento2

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (gatsby-source-magento2) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

gatsby-source-magento2

Source plugin for pulling data into Gatsby from Magento 2.3

Features

  • Provides public data available via standard Magento 2.3 GraphQL endpoint
  • Supports gatsby-transformer-sharp and gatsby-image for images

Install

yarn add gatsby-source-magento2

How to use

// add in your gatsby-config.js
plugins: [
    {
        resolve: "gatsby-source-magento2",
        options: {
            graphqlEndpoint: "https://yourstore.url/graphql",
            
            // this is optional
            queries: {
                // see example query in src/nodes/queries/products.js
                allProductsQuery: `... custom GraphQL query for fetching all the products you need to publish on Gatsby website ...`
            }
        }
    }
]

Then you can use queries magentoProduct and allMagentoProduct to query the product catalog.

Creating product page nodes

To generate pages for each of the products in your store you need to add this code to your gatsby-node.js file:

exports.createPages = ({ graphql, actions }) => {
    const { createPage } = actions;

    return new Promise((resolve, reject) => {
        resolve(
            graphql(
                `
                    {
                        allMagentoProduct {
                            edges {
                                node {
                                    url_key
                                }
                            }
                        }
                    }
                `
            ).then(result => {
                if (result.errors) {
                    reject(result.errors);
                }

                // Create pages for each product
                result.data.allMagentoProduct.edges.forEach(({node}) => {
                    createPage({
                        path: `/${node.url_key}/`,
                        component: path.resolve(`./src/pages/product.js`),
                        context: {
                            url_key: node.url_key,
                        },
                    });
                });

            })
        );
    });
};

Future work

Add other node sources:

  • category
  • cmsPage
  • cmsBlocks
  • storeConfig