sanitygroq

Resolve reference in Block Content in Sanity.io


I have the Content Block in Sanity like so:

export default {
title: "Block Content",
name: "blockContent",
type: "array",
of: [
    /// stuff here
    {
        title: "Book",
        type: "reference",
        to: [{ type: "book" }],
    },
],
};

When making a query like

'*[_type == "post"]{...,body[]{..., asset->{..., "_key": _id}, markDefs[]{..., _type == "internaLink" => {"slug": @.reference->slug}}}';

I get the reference, but I want to return the full Document. I tried every method but the documentation only explain references outside Content Blocks and those methods aren't working.

This is what's returned from the Query:

  _createdAt: '2020-12-07T14:43:34Z',
  _id: '9d628aa6-aba7-4b53-aa9f-c6e97583baf9',
  _rev: 'ZZ0GkIKCRvD0tdMQPywPfl',
  _type: 'post',
  _updatedAt: '2020-12-07T14:43:34Z',
  body: [
    {
      _key: '4184df372bae',
      _type: 'block',
      children: [Array],
      markDefs: [],
      style: 'normal'
    },
    {
      _key: '56bed8835a7d',
      _ref: 'dc2eefee-2200-43e1-99c7-ea989dda16ba',
      _type: 'reference'
    }
  ],
  title: 'Example'

Solution

  • Solved, I'm writing the solution here as I found nothing on the web. Basically I tried anything randomly until I got the desired result. The query is:

    _type=="reference"=>^->