Skip to content

Instantly share code, notes, and snippets.

@matthewbeta
Created June 18, 2020 21:53
Show Gist options
  • Select an option

  • Save matthewbeta/d5e50b3d06a171876272f2a98a2ed22e to your computer and use it in GitHub Desktop.

Select an option

Save matthewbeta/d5e50b3d06a171876272f2a98a2ed22e to your computer and use it in GitHub Desktop.

Revisions

  1. matthewbeta created this gist Jun 18, 2020.
    120 changes: 120 additions & 0 deletions gatsby-node.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,120 @@
    const { createRemoteFileNode } = require(`gatsby-source-filesystem`);
    const axios = require('axios');
    require('console-success');

    const ENTRY_API_URI = `${process.env.GATSBY_API_URL}/entries.json`;
    const CATEGORY_API_URI = `${process.env.GATSBY_API_URL}/categories.json`;

    exports.sourceNodes = async ({ actions, createNodeId, cache, store, createContentDigest }) => {
    const { createNode, createNodeField } = actions;

    const { data: entry } = await axios.get(ENTRY_API_URI);
    const { data: category } = await axios.get(CATEGORY_API_URI);

    let data = [];

    data.push(...entry.data);
    data.push(...category.data);

    let barIncrement = 0;
    let totalImages = 0;

    for (const entry of data) {
    totalImages = totalImages + entry.craftImages.length;
    }

    for (const entry of data) {
    for (const image of entry.craftImages) {
    barIncrement++;

    try {
    console.success(`Downloaded ${barIncrement} of ${totalImages} | ${image.name}`);
    const node = Object.assign(
    {
    id: createNodeId(`craft-source-${image.id}-${barIncrement}`),
    createNodeId,
    createNode, // note: some unique identifier here, I'm not familiar with the data source
    parent: null,
    store,
    cache,
    children: [],
    internal: {
    type: `CraftImage`,
    contentDigest: createContentDigest(image)
    }
    },
    image
    ); // now we can query for any of the data on the API response for an image

    createNode(node);

    const imageNode = await createRemoteFileNode({
    url: `${process.env.GATSBY_API_URL}${image.url}`,
    cache,
    store,
    createNode,
    createNodeId,
    parentNodeId: node.id
    });

    await createNodeField({
    node: imageNode,
    name: 'craftImage',
    value: 'true'
    });

    await createNodeField({
    node: imageNode,
    name: 'featureImage',
    value: JSON.stringify(image.featureImage)
    });

    await createNodeField({
    node: imageNode,
    name: 'entrySlug',
    value: entry.slug
    });

    await createNodeField({
    node: imageNode,
    name: 'entryUri',
    value: entry.uri
    });

    await createNodeField({
    node: imageNode,
    name: 'entryTitle',
    value: entry.title
    });

    await createNodeField({
    node: imageNode,
    name: 'craftImageId',
    value: image.id
    });

    await createNodeField({
    node: imageNode,
    name: 'craftAltTag',
    value: image.altTag
    });

    await createNodeField({
    node: imageNode,
    name: 'craftAssetVolume',
    value: image.volume
    });

    await createNodeField({
    node: imageNode,
    name: 'craftImageEntryUrl',
    value: image.url
    });

    node[`image___NODE`] = imageNode.id;
    } catch (error) {
    console.warn('error creating node', error);
    }
    }
    }
    };