Query issue with returning a specific "collection" document in a Slack thread.

5 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!
May 11, 2020, 1:48 AM
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?
May 11, 2020, 11:59 AM
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?
May 11, 2020, 11:59 AM
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).
May 11, 2020, 12:34 PM
Feel free to DM me and I’ll check it out 🙂
May 11, 2020, 12:39 PM
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
]{
...
May 11, 2020, 3:36 PM

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?