Unlock seamless workflows and faster delivery with our latest releases - Join the deep dive

Querying a custom type reference in Sanity using groq

9 replies
Last updated: Sep 15, 2022
Hey all! I have a document with a block type, and I've added a reference to a custom type. Like:
{
      name: "body",
      type: "array",
      title: "Body",
      of: [
        { type: "block" },
        { type: "image" },
        {
          title: "Class",
          name: "jccClass",
          type: "reference",
          to: [{ type: "jccClass" }],
        },
      ],
    },
This works fine in the studio, but I'm wondering how to pull that in via groq. I can do this:

*[_type == "post" && slug.current == $slug][0] {
    _id,
    title,
    body,
    "slug": slug.current
  }
Which is fine, but it just pulls reference information into the data, rather than the actual information about jccClass itself. I'm getting this:


{
    "post": {
        "_id": "50bc7aa9-f4af-424d-9184-fbabb2b826ed",
        "body": [
            {
                "_key": "307f83dbbafb",
                "_ref": "df6c5ee6-a80f-4e68-843a-501abaafc6ca",
                "_type": "jccClass"
            },
            ...
        ],
        "slug": "post-one",
        "title": "Post Number One"
    }
}
Can I flesh out the jccClass into an object in the initial query, or will I need to have the component that renders it do the data load itself?

Thanks!
Sep 13, 2022, 2:05 PM
Hey Brian. You can tweak your query so it resolves the reference. I think it should work like this:
*[_type == "post" && slug.current == $slug][0] {
    _id,
    title,
    body[] {
      ...,
      _type == "jccClass" => {
        @ -> { ... }
      }
    },
    "slug": slug.current
}
Sep 14, 2022, 6:56 AM
Hi! Thanks for this; should I be replacing the "..." with the fields I want, or is this a way to get all of them?
Sep 15, 2022, 2:27 PM
It’s a way to get all of them. 🙂
Sep 15, 2022, 2:32 PM
So you can replace to cherry-pick but you don’t have to.
Sep 15, 2022, 2:33 PM
Thanks! I'm getting a
Attribute or a string key expected
error when I run this. The bit that seems to be tripping Sanity up is
@ -> { ... }
.
Sep 15, 2022, 3:56 PM
Can you try maybe
"foobar": @ -> { ... }
?
Sep 15, 2022, 4:03 PM
That gets rid of the error, but it doesn't load any of the related data:
*[_type == "post" && slug.current == $slug][0] {
    _id,
    title,
    body[] {
      ...,
     type == "jccClass" => {
        "foobar": @ -> { 
         title,
      slug
    
    }
      }
    },
    "slug": slug.current
}
"result":{4 items
"_id":"50bc7aa9-f4af-424d-9184-fbabb2b826ed"
"body":[7 items
0:{3 items
"_key":"307f83dbbafb"
"_ref":"df6c5ee6-a80f-4e68-843a-501abaafc6ca"
"_type":"jccClass"
}
1:{7 items
"_key":"04f4fd05e1ca"
"_type":"block"
"children":[...]2 items
"level":1
"listItem":"number"
"markDefs":[...]1 item
"style":"normal"
}
Sep 15, 2022, 4:06 PM
It should be
_type
, not
type
.
Sep 15, 2022, 4:15 PM
Ahhhhh, thank you!
Sep 15, 2022, 4:17 PM

Sanity– build remarkable experiences at scale

Sanity is a modern headless CMS that treats content as data to power your digital business. Free to get started, and pay-as-you-go on all plans.

Was this answer helpful?