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

Resolve Gatsby image data in _rawContent #143

Open
aaronadamsCA opened this issue Jan 7, 2022 · 4 comments
Open

Resolve Gatsby image data in _rawContent #143

aaronadamsCA opened this issue Jan 7, 2022 · 4 comments

Comments

@aaronadamsCA
Copy link

aaronadamsCA commented Jan 7, 2022

I'd like to see a new option in the GraphQL schema to resolve Gatsby image data in _rawContent.

Intended usage:

{
  allSanityPage {
    nodes {
      id
      _rawContent(
        resolveReferences: {maxDepth: 10}
        resolveGatsbyImageData: {width: 1600}
      )
    }
  }
}

Intended result: Block content objects of "_type": "image" would contain asset.gatsbyImageData. This could be passed directly to gatsby-plugin-image while serializing block content, for example:

import { GatsbyImage } from "gatsby-plugin-image";

export const imageSerializer = ({ node }) => {
  return <GatsbyImage image={node.asset.gatsbyImageData} />;
};

This would make life with block content and Gatsby a lot easier!

@aaronadamsCA
Copy link
Author

aaronadamsCA commented Jan 7, 2022

My amusing workaround, for now, is to populate siteMetadata with my Sanity projectId and dataset, and then use this very clumsy image serializer:

import { graphql, StaticQuery } from "gatsby";
import { GatsbyImage } from "gatsby-plugin-image";
import { getGatsbyImageData } from "gatsby-source-sanity";

export const imageSerializer = ({ node: { asset } }) => (
  <StaticQuery
    query={graphql`
      {
        site {
          siteMetadata {
            sanityLocation {
              projectId
              dataset
            }
          }
        }
      }
    `}
    render={({ site: { siteMetadata } }) => (
      <GatsbyImage
        image={getGatsbyImageData(asset._id, {}, siteMetadata.sanityLocation)}
      />
    )}
  />
);

@1-800-jono
Copy link

Is this the only way to do this?

@aaronadamsCA
Copy link
Author

As far as I know.

I mean - there are for sure better ways to grab projectId and dataset. For us, we'll probably switch to a context-based solution for that part when we eventually refactor that code.

But resolving the image data still needs to be done in the serializer, and is not happening inside the Gatsby graph.

@1-800-jono
Copy link

Thanks @aaronadamsCA - I was able to solve the issue with your comment.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants