🔮 Sanity Create is here. Writing is reinvented. Try now, no developer setup

Bug with projection/join of a reference inside an array in Sanity.io

12 replies
Last updated: Apr 26, 2021
Updating to
2021-03-25
and having some trouble with projection/join of a reference inside an array.

(the following is nested a few layers inside a query)

'terpenes': terpenes[]{
  percent,
  mg,
  'name': terpene->name
}
This returns what I expect, an array of objects with 3 keys each, with
name
being the proper name of the
terpene
doc.

'terpenes': terpenes[]{
  percent,
  mg,
  'name': terpene->name
},
'dominantTerpenes': terpenes|order(percent desc)[0..2]{
  percent,
  mg
},
This returns two arrays as expected, with
dominantTerpenes
only returning the 3 with the highest
percent
, however when I want the name in that array:

'terpenes': terpenes[]{
  percent,
  mg,
  'name': terpene->name
},
'dominantTerpenes': terpenes|order(percent desc)[0..2]{
  percent,
  mg,
  'name': terpene->name
},

terpenes
is still right but now
dominantTerpenes
is an empty array 🤔
What’s happening here?
Apr 20, 2021, 2:34 PM
dig up this breaking change in the docs, wonder if it’s relevant in your case?
order() only works on arrays, and returns null otherwise
https://www.sanity.io/changelog?platforms=GROQ#5df909ebba61
Apr 20, 2021, 2:46 PM
ah but it still returns an empty array so maybe not
Apr 20, 2021, 2:46 PM
Hoping that was it but yeah,
terpenes
is an array so shouldn’t be 🤔
Apr 20, 2021, 3:15 PM
That looks like a bug. I’ll see if we can reproduce it.
Apr 21, 2021, 1:50 PM
I’m not able to reproduce with
2021-03-25
. Are you absolutely sure that this is the API version you’re testing?
Apr 21, 2021, 1:57 PM
It would also be helpful to see an example of a document you’re querying
Apr 21, 2021, 1:58 PM
For sure, 1 sec I’ll put something together so you can reproduce
Apr 21, 2021, 2:16 PM
I can reproduce it, and it looks like a very rare bug indeed. We’ll triage and hopefully get you a fix very soon.
Apr 21, 2021, 2:58 PM
Thank-you!
Apr 21, 2021, 3:08 PM
I've been able to reduce it even further into this test: https://github.com/sanity-io/groq-test-suite/pull/47 .
The bug is related to an optimization pass not taking into account
order()
in this situation. I've been able to fix it locally by disabling the optimization in this case, but I'll also try to see if I can make it work with both order and optimization because we want things to be fast as well!
Apr 22, 2021, 8:16 AM
Alright, I have a fix ready that will be rolled out soon!
Apr 26, 2021, 7:41 AM
And it's now fully rolled out! Can you see if it works better now?
Apr 26, 2021, 8:34 AM

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?