Trouble querying for types and limitations of querying objects in Sanity

11 replies
Last updated: May 3, 2021
Hi all!I'm having trouble querying for types. This produces no matches. However, I'm successfully using the type in an array for a page. And the returned json data is correctly displaying, and even shows the testimonial type!

I'm using the Vision query sandbox for production.


*[_type == 'testimonial']
AI Update

I've seen this issue before! When Vision shows no results for *[_type == 'testimonial'] but you know the type exists in your data, it's almost always one of these causes:

1. Dataset mismatch - This is the most common culprit. Check that Vision is querying the same dataset where your testimonials actually exist. Look at the dataset dropdown in Vision's interface (usually in the top toolbar). If you're using a production dataset but Vision is set to development (or vice versa), you won't see any results. Switch to the correct dataset and try again.

2. API version perspective - The Vision tool uses your authenticated credentials, which means you can see drafts and unpublished content. However, if your testimonials only exist as drafts and you're somehow querying with a published perspective filter, they might not appear. Try:

*[_type == 'testimonial'] | order(_updatedAt desc)

And check if you see draft IDs (they start with drafts.).

3. Authentication context - Vision uses your logged-in Studio credentials, so it should see everything in your current project/dataset. But if the documents are somehow in a different project entirely, that would explain the disconnect.

Quick debugging steps:

  • Try *[_type == 'testimonial'][0] to see if at least one exists
  • Run *[_type in ["testimonial"]] as an alternative syntax
  • Check count(*[_type == 'testimonial']) to see if it returns a number
  • Try querying all types with *[defined(_type)] | order(_type) and look for your testimonial documents

The fact that your array references work correctly suggests the documents definitely exist, so this is almost certainly a dataset selection issue in Vision's settings rather than a problem with your query syntax or schema. Double-check that dropdown!

Hi User. When you’re viewing a testimonial document in the studio, you can click the three vertical dots in the top-right corner and select Inspect. Do you then see something like:

{
  "_createdAt": "2021-03-09T06:14:07Z",
  "_id": "22c6c3cb-9f8a-4d6e-9084-627b18a6d45f",
  "_rev": "btIujVUaQMY6qaWz2C2Fbk",
  "_type": "testimonial",
  ...
Hi User, appreciate the quick response! I see this:
The type is displaying
Hi User, appreciate the quick response! I see this:
The type is displaying
It's also an object, not a document. Not sure if that makes a difference. The testimonial type is embedded in a document, and it's quite deeply nested.
Unfortunately, you won’t be able to search for an object by its type in the same way you would a document. I can’t come up with anything more eloquent than:

*[length(content[_type == 'testimonial']) > 0].content[_type == 'testimonial']
This assumes your object is in an array (like your portable text) named content. You said your object was deeply nested, so that might require some finessing. I’d be interested in hearing if this gets the data you’re after.
I see, so would changing the type of testimonial to be a document fix that?
And the query doesn't seem to work, my full object looks like this
Well it seems like it works with documents, I guess that limitation of the querying should be in the docs
Yes, if it were a document you should be able to search for them with your original query.
The query I posted before would require you to use the GROQ API v2021-03-25 or later, which you can get access to in Vision by updating your sanity versions. I’m not sure
v1
has an equivalent query that drills down to the object.
Yes, if it were a document you should be able to search for them with your original query.
The query I posted before would require you to use the GROQ API v2021-03-25 or later, which you can get access to in Vision by updating your sanity versions. I’m not sure
v1
has an equivalent query that drills down to the object.
I see, thanks for your help!

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?