Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Querying fixed, fluid, or gatsbyImage nodes of the childImageSharp node of a Craft asset results in an error. #6

Closed
sandren opened this issue Nov 3, 2020 · 6 comments
Assignees
Labels
bug Something isn't working

Comments

@sandren
Copy link

sandren commented Nov 3, 2020

Description

The gatsby-source-craft plugin makes childImageSharp nodes appear in Gatsby's GraphiQL interface, but querying any of the fields within the fixed, fluid, or gatsbyImage nodes results in the following error:

The "path" argument must be of type string or an instance of Buffer or URL. Received undefined

Steps to reproduce

  1. install and configure gatsby-source-craft
  2. run gatsby develop
  3. open GraphiQL on http://localhost:8000/___graphql and query any fields within the fixed, fluid, or gatsbyImage nodes of the childImageSharp of a Craft CMS asset such as:
... on Craft_EXAMPLE_Asset {
  childImageSharp {
    gatsbyImageData(layout: FLUID)
  }
}

Additional info

  • Craft version: Craft Pro 3.5.15.1
  • PHP version: 7.3.19
  • Database driver & version: MySQL 5.5.5
  • Plugins & versions: Gatsby 1.0.0-beta.1
@sandren sandren added the bug Something isn't working label Nov 3, 2020
@sandren
Copy link
Author

sandren commented Nov 5, 2020

Until the issue with gatsby-source-craft is resolved, there is a workaround using gatsby-source-filesystem to create remote file nodes based on the public URL of Craft assets.

Steps to implement workaround

  1. install gatsby-source-filesystem
  2. add the following to gatsby-node.js, replacing EXAMPLE with the handle of your Craft volume with working public URLs:
const { createRemoteFileNode } = require('gatsby-source-filesystem');
exports.createResolvers = async ({
  actions,
  cache,
  createNodeId,
  createResolvers,
  store,
  reporter,
}) => {
  const { createNode } = actions;
  await createResolvers({
    Craft_EXAMPLE_Asset: {
      imageFile: {
        type: `File`,
        async resolve(source) {
          return await createRemoteFileNode({
            url: source.url,
            store,
            cache,
            createNode,
            createNodeId,
            reporter,
          });
        },
      },
    },
  });
};
  1. run gatsby develop
  2. open GraphiQL on http://localhost:8000/___graphql
  3. a new imageFile node will appear on any Craft assets, which can be queried for use with Gatsby Image, being sure to query the url field as well or else the createRemoteFileNode() function will fail:
... on Craft_EXAMPLE_Asset {
  url
  imageFile {
    childImageSharp {
      gatsbyImageData(layout: FLUID)
    }
  }
}

Steps to reverse workaround

Once the issue with gatsby-source-craft is resolved, gatsby-source-filesystem can be uninstalled, the code from gatsby-node.js can be removed, and the query for images can be shortened to:

... on Craft_EXAMPLE_Asset {
  childImageSharp {
    gatsbyImageData(layout: FLUID)
  }
}

andris-sevcenko added a commit that referenced this issue Nov 10, 2020
@andris-sevcenko
Copy link
Contributor

Okay, so.

gatsby-transform-sharp adds the childImageSharp child node to anything that has an extension field that contains a file extension that it can transform. Since an Asset describes a file, it's a valid scenario for it to have the extension field, even if it is not the file itself.

I've submitted a bug report in hopes that the current eager behavior can be adjusted since actually removing that field once it's added can get very ugly. Or, well, maybe I just can't figure out how to do it cleanly.

Until then, I've pretty much done exactly what @sandren has done, except I went with a localFile field that gets created automatically on all AssetInterface implementing types.

Since this is a resolver, if you never touch the localFile field, it never gets downloaded, so this should be pretty efficient.

I'll leave this issue open for now because part of the issue is the incorrect childImageSharp field on Assets.

@pieh
Copy link

pieh commented Nov 12, 2020

Hi @sandren

I just opened pull request with fix for gatsby-transformer-sharp wrongly adding childImageSharp field to Asset (as described by @andris-sevcenko in previous comment) if you'd like to follow that.

The provided workaround is one that makes sense to me, and maybe the localFile / imageFile resolver could be added by gatsby-source-craft plugin automatically so users don't have to implement this on their own, but this is up to plugin maintainers to decide.

@andris-sevcenko
Copy link
Contributor

This fix has been implemented in gatsby-transformer-sharp@2.6.0

@kara-todd
Copy link
Contributor

kara-todd commented Dec 1, 2020

@andris-sevcenko Is the manual resolver still required? So is this closed with a "Won't fix" sort of closed? Or should this be working where asset images are automatically detected and receive the childImageSharp field?

Edit: This worked for me as soon as I added the resolver. Since this was closed I just wanted to make sure this was the intention. Might be good to clarify this point in the documentation?

@andris-sevcenko
Copy link
Contributor

@kara-todd it's not required, not. The sourcing plugin automatically adds a localFile node. The file is actually downloaded from Craft only if you access it in a query.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

4 participants