Query issue with returning a specific collection in a Sanity.io Slack thread.

4 replies
Last updated: May 11, 2020
Hi, I am trying to understand why the "collection" in the below query is never returned (using -> and references(^._id) in the same filter doesn't work). Everything is linked/referenced correctly (the collection has a reference to the partner and it also references the post).

*[_type == 'post' && slug.current == $postSlug && partner->slug.current == $partnerSlug] {
    "post": @{
      ..., 
     images[]{..., asset->{...}}
    },
    "collection": *[_type == 'collection' 
                    && slug.current == $collectionSlug
                    && partner->slug.current == $partnerSlug
                    && references(^._id)
                    ]{
      ...,
      posts[]->{
        ...,
        images[]{..., asset->{...}}
      },
    }[0],
    "partner": *[_type == 'partner' && slug.current == $partnerSlug]{
      ...,
      logo{..., asset->{...}}
    }[0]
  }
Where as this query always returns a "collection":


*[_type == 'post' && slug.current == $postSlug && partner->slug.current == $partnerSlug] {
    "post": @{
      ..., 
     images[]{..., asset->{...}}
    },
    "collection": *[_type == 'collection' 
                    && slug.current == $collectionSlug
                    && references(^._id)
                    ]{
      ...,
      posts[]->{
        ...,
        images[]{..., asset->{...}}
      },
    }[0],
    "partner": *[_type == 'partner' && slug.current == $partnerSlug]{
      ...,
      logo{..., asset->{...}}
    }[0]
  }
As does this query:


*[_type == 'post' && slug.current == $postSlug && partner->slug.current == $partnerSlug] {
    "post": @{
      ..., 
     images[]{..., asset->{...}}
    },
    "collection": *[_type == 'collection' 
                    && slug.current == $collectionSlug
                    && partner->slug.current == $partnerSlug
                    ]{
      ...,
      posts[]->{
        ...,
        images[]{..., asset->{...}}
      },
    }[0],
    "partner": *[_type == 'partner' && slug.current == $partnerSlug]{
      ...,
      logo{..., asset->{...}}
    }[0]
  }
Basically the point of the query is given three slugs, I want to bring back all the documents for all three AND ensure the documents reference each other correctly (the collection references both the post and the partner, the post references the partner).

Anyway, any advice would be greatly appreciated!
AI Update

Based on the references() function documentation and reference access operator documentation, both -> (dereference operator) and references(^._id) are valid GROQ operators that should work together in the same filter. The documentation confirms that ^ correctly refers to the parent scope in subqueries, and there's no documented limitation preventing these operators from being combined.

However, there's an important detail about how the dereference operator works: according to the documentation, when you use partner->slug.current, the -> operator "internally executes a subquery to fetch the complete document with the matching _id". This means each -> operation is effectively performing an additional query lookup.

The most likely explanation for why your first query returns no results is that you're over-constraining the filter. When you combine:

&& partner->slug.current == $partnerSlug
&& references(^._id)

You're requiring that the collection document satisfies BOTH conditions simultaneously. If your collection's partner reference structure or the way references() checks for the post ID doesn't match exactly what you expect, the combined filter will fail to return results.

Practical debugging approach:

First, verify what's actually in your collection document. Run this diagnostic query:

*[_type == 'collection' && slug.current == $collectionSlug][0]{
  _id,
  partner,
  "partnerRef": partner._ref,
  "partnerSlug": partner->slug.current,
  posts
}

Then test each condition individually within your subquery to see which one is failing:

*[_type == 'post' && slug.current == $postSlug && partner->slug.current == $partnerSlug] {
  "testReferences": *[_type == 'collection' 
                      && slug.current == $collectionSlug
                      && references(^._id)]{_id},
  "testPartner": *[_type == 'collection' 
                   && slug.current == $collectionSlug
                   && partner->slug.current == $partnerSlug]{_id},
  "testBoth": *[_type == 'collection' 
                && slug.current == $collectionSlug
                && partner->slug.current == $partnerSlug
                && references(^._id)]{_id}
}

Alternative approach using reference comparison:

Instead of dereferencing in the filter (which performs a subquery), compare the reference IDs directly:

"collection": *[_type == 'collection' 
                && slug.current == $collectionSlug
                && references(^._id)
                && partner._ref == ^.partner._ref
                ]{
  ...,
  posts[]->{
    ...,
    images[]{..., asset->{...}}
  },
}[0]

This avoids the dereference operator in the filter condition and directly compares the stored reference IDs, which should be more reliable and performant.

The documentation doesn't indicate any technical limitation with combining these operators, so the issue is most likely with how your data is structured or how the conditions are being evaluated against your actual document structure. The diagnostic queries above should help you identify exactly which condition is failing.

Hi Matthew, without having reproduced this yet, have you tried using
references(…)
for both the
_id
and
$partnerSlug
? Also, you have verified that the returned collection is exactly the same for both working queries, correct?
Hi Peter, I thought references could only be used with an _id? The partnerSlug is of type slug... And yes, the returned collection is exactly the same. I'm happy to give access to our dataset - there isn't much in there (yet).
Feel free to DM me and I’ll check it out 🙂
Resolved with the snippet below, although some follow-up is required to investigate a non-working
^._id
in the query above (and variations of it).
...
"collection": *[_type == 'collection' 
  && slug.current == $collectionSlug
  && partner->slug.current == $partnerSlug
  && $postSlug in posts[]->slug.current
]{
...

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?