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

Trouble with sorting documents in a query using Groq params.

40 replies
Last updated: Oct 11, 2022
Dear Sanity team 👋
I am having trouble with this query and am happy if you can point me into the right direction.
I want to get specific documents which have a reference to specific tags ordered by publishedAt descending or ascending.


`*[_type in $documentTypes 
    && count((tags[]->tag.current)[@ in $referenceTags]) > 0] 
   | order($orderBy) 
   {
      "type": _type,
      publishedAt, 
      title, 
      "tags": tags[]->label
    }`

Sep 9, 2022, 2:22 PM
I am passing
$documentTypes
and
$referenceTags
as well as
$orderBy
as a params to the querywhere the
$documentTypes
and
$referenceTags
are arrays of strings like ["post", "video"] and ["tag1", "tag2"]and
$orderBy
a string of either
publishedAt desc
or
publishedAt asc

The correct documents with the correct reference tags are returned.

However, the order is always wrong.

Is it maybe not possible to pass the value for "order" as a param to the query?
🤔
Sep 9, 2022, 2:22 PM
Or do I need to order first and then filter by tags?
Sep 9, 2022, 2:55 PM
Could you share your documenType schema please?
Sep 9, 2022, 6:10 PM
Hey there. What’s not working? Are you not getting the right documents? Or is the sorting not working?
Sep 10, 2022, 7:50 AM
Hey
user F
Thanks for your reply and sorry for the late answer.

I am still having the same problem
😢
Oct 3, 2022, 9:40 AM
The sorting is not working. That is the main issue.
Oct 3, 2022, 9:40 AM
The correct documents with the correct reference tags are returnedbut the sorting is always wrong

I can not even tell which sorting is returned
but definitely not sorted by the published date
Oct 3, 2022, 9:41 AM
user F
What am I doing wrong?Thanks for your help again.
Oct 3, 2022, 9:45 AM
user F

Just pinging you again to show that I am really here.
Sorry that my last answer took so long.
😅
I am happy if you can find the time
for my code above and the issue with the wrong sorting.
Oct 4, 2022, 8:51 AM
Hellooooo. I unfortunately don't have time today, I'm very sorry. 😞
Oct 4, 2022, 8:54 AM
Alright! Thanks for the information.
Oct 4, 2022, 8:56 AM
Hi
user F
Maybe it fits in today?
🙂
Oct 5, 2022, 7:10 AM
I'll try but not sure. 😞
Oct 5, 2022, 7:14 AM
It's just that the order doesn't work in the query above.
Thanks!
Oct 5, 2022, 7:15 AM
Thanks
user F

I am posting this again in the main channel. Maybe someone else has time?
Oct 6, 2022, 8:07 AM
Hi User. I’m not sure if query params can be used in the
order()
function. Can you confirm that hardcoding something works as intended? Can you then try using string template literals instead to see if that works?
Oct 6, 2022, 2:02 PM
Hi
user A

Thanks so much for your reply!

Yes, hard coding the order works.

Problem is I am having a select with which the user can decide the order of the query results (oldest first, latest first, A-Z, Z-A ....)

How can I then dynamically pass these orderBy values to the query?
Oct 6, 2022, 2:56 PM
Were you able to try using template literals?
Oct 6, 2022, 3:57 PM
Thanks.
In my eyes this whole query is already a template literal


`*[_type in $documentTypes 
    && count((tags[]->tag.current)[@ in $referenceTags]) > 0] 
   | order($orderBy)` 

Oct 6, 2022, 4:26 PM
Maybe I am getting the definition of a template literal wrong?
Oct 6, 2022, 4:27 PM
Sorry, I don’t want to introduce confusion. No argument from me about what you just posted.
Specifically,
$orderBy
is a GROQ parameter. Can you instead try
${orderBy}
?
Oct 6, 2022, 4:29 PM
I mainly want to rule out what’s not possible before bringing it to the team internally to consider options.
Oct 6, 2022, 4:31 PM
Ah, now I see
Yes, this works too.
Oct 6, 2022, 4:36 PM
Okay, that’s good to hear. Thanks for confirming.
I have a feeling that parameters aren’t going to work in the
order()
function, and that this is the route you’ll need to take.
Oct 6, 2022, 4:38 PM
Ah I see. Thanks!
So in sum: Passing a Groq param to
order
is not working.
Oct 6, 2022, 4:39 PM
If I need to pass the value as
${orderBy}
then I'll need to rewrite my whole query setup
as these are currently defined in a re-usable object.
Oct 6, 2022, 4:40 PM
Given what you’ve posted above, that’s the way it seems. I’ll still try to confirm internally.
Oct 6, 2022, 4:42 PM
Thanks 🙏
Oct 6, 2022, 4:44 PM
Hi User. Thanks for your patience on this. Confirmed that a param can’t be passed into
order()
.
(See follow-up.)
Oct 6, 2022, 10:43 PM
Thanks for all your help and explanations
user A
Oct 7, 2022, 7:37 AM
Hi User. You’re welcome, though before you get too far refactoring, it turns out what I said wasn’t the case.

order($orderBy)
will try to sort by the literal string value of
$orderBy
, which is why it doesn’t work. However, one of our engineers just let us know that
order(@[$orderBy])
should in fact work how you’re wanting.
Hopefully that makes your refactor easier, as it’s more in line with what you were originally trying. Let us know how it goes. We’ll also work on adding it to the docs.
Oct 7, 2022, 1:48 PM
Hi
user A

Thanks for the update!

I tried


`*[_type in $documentTypes 
    && count((tags[]->tag.current)[@ in $referenceTags]) > 0] 
   | order(@[$orderBy]) 
   {
      "type": _type,
      publishedAt, 
      title, 
      "tags": tags[]->label
    }`
with param
$orderBy: "publishedAt desc"but it doesn't work.

The response is a 400 with

{"error":{"description":"Name contains invalid characters","type":"validationError"}}

When I remove
order(@[$orderBy])
it works again.
Oct 11, 2022, 7:35 AM
When you define your params, you won’t want the
$
to be there. Only in the query.
Oct 11, 2022, 2:17 PM
Yes, sure. Sorry for the confusion.
Oct 11, 2022, 3:30 PM
It´s orderBy
Oct 11, 2022, 3:30 PM
Bit still the same error 😄
Oct 11, 2022, 3:31 PM
The space between
publishedAt
and
desc
is throwing that error. I’m not sure there’s a way to specify the order direction dynamically here so
desc
or
asc
might need to be hard-coded (with
$orderBy
changed to just
publishedAt
).
Oct 11, 2022, 4:11 PM
Oh that's interesting!Thanks again for checking it out yourself
🌟
I need to have it dynamically as I'll pass these params from a user´s input select:

• "publishedAt desc",
• "publishedAt asc",
• "lower(title) desc",
• "lower(title) asc",
Oct 11, 2022, 5:40 PM
I refactored the queries now completelyand generate the groq queries dynamically with template literals

I general it would be more elegant to have it work with groq params as I am having other queries which do work like that before.

But if this is not possible with dynamic orderBy values
then I would stick with the refactored solution then.

Still I would like to know if you updated the docs
🙂
Oct 11, 2022, 5:49 PM
I would like to know if you updated the docs
Not yet. Waiting to hear back internally about passing a direction dynamically.
Oct 11, 2022, 5:53 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?