Get image metadata from image component inside block type with GraphQL

10 replies
Last updated: Oct 29, 2021
Using GraphQL is there a way to get the Image metadata from an image component used inside the
block
type?
AI Update

Yes, when working with images inside Portable Text (block content) in GraphQL, you can access image metadata, though it requires understanding how Sanity structures this data in the GraphQL API.

The Raw Field Approach

For Portable Text fields in GraphQL, Sanity automatically generates a <fieldName>Raw field that returns the raw JSON structure of your block content, including image references. This is documented in the GraphQL documentation under the Portable Text section.

Here's how to query it:

{
  allPost {
    title
    bodyRaw  # Note the "Raw" suffix
  }
}

This returns the Portable Text as raw JSON, which will include image objects with their asset references:

{
  "allPost": [{
    "title": "My Post",
    "bodyRaw": [
      {
        "_type": "block",
        "children": [...]
      },
      {
        "_type": "image",
        "asset": {
          "_ref": "image-abc123...",
          "_type": "reference"
        }
      }
    ]
  }]
}

Accessing Image Metadata

The challenge is that by default, the Raw field only gives you the reference to the image asset, not the actual metadata. The GraphQL API doesn't automatically resolve these references within the raw JSON like GROQ does with the -> operator.

However, if you're using source plugins like gatsby-source-sanity or gridsome-source-sanity, these plugins provide additional arguments on the Raw field that can resolve image asset references for you, giving you access to dimensions, URLs, and other metadata.

Alternative Approach: Separate Asset Queries

If you need full image metadata (dimensions, file size, EXIF data, etc.) and aren't using a source plugin, you may need to:

  1. Query the bodyRaw field to get image asset IDs
  2. Make a separate query to fetch those specific assets with their metadata
  3. Merge the data client-side

Or consider using GROQ instead, which handles reference expansion more elegantly with its asset-> syntax and gives you more flexible querying for nested content structures.

The GraphQL API is fully supported and works well for many use cases, but GROQ tends to be more flexible when dealing with deeply nested content like images within Portable Text blocks.

Show original thread
10 replies
The schema generator will expose a
<your-type-name>Raw
field for your Portable Text, but in order to resolve references you need to use one of our source plugins for Gatsby or Gridsome .
For example, Gatsby source allows you to do the following:
{
  allSanityProject {
    edges {
      node {
        _rawTasks(resolveReferences: {maxDepth: 5})
      }
    }
  }
}
I have the basic
Raw
field, I'm using
Apollo
with
nextjs
. Previously i've looked into the
_raw
field but couldn't figure out how to use it in this stack.
Got it. You unfortunately can't pass the
resolveReferences
argument unless you're using one of those source plugins.
So the answer my initial question in the context of my stack (sorry i should have said it in the ticket), it's a no? 😓
Yeah, it's a no 😞 . But since you're using a Next frontend, have you considered making a change over to using GROQ as your query language? It has a much more intuitive way of handling references within Portable Text.
yeah in hindsite it would be better to do this. Are you able to create chunks of a GROQ call and stitch them? i.e one for each CMS module (we have a modular CMS)
GROQ queries are strings. You can concatenate them or use template literals with string interpolation. for example: https://gist.github.com/mornir/4905c0d9adaa8612d797f8e054764087
Yeah, the great thing about GROQ is that you can stitch information together across any document in your dataset. There's a really good intro here . It's only simple examples, but it'll give you a look at how powerful it is.
Thanks folks!

Sanity – Build the way you think, not the way your CMS thinks

Sanity is the developer-first content operating system that gives you complete control. Schema-as-code, GROQ queries, and real-time APIs mean no more workarounds or waiting for deployments. Free to start, scale as you grow.

Was this answer helpful?