Trouble with sorting documents in a query using Groq params.
40 replies
Last updated: Oct 11, 2022
L
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.
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
L
I am passing
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?
đ¤
$documentTypesand
$referenceTagsas well as
$orderByas a params to the querywhere the
$documentTypesand
$referenceTagsare arrays of strings like ["post", "video"] and ["tag1", "tag2"]and
$orderBya string of either
publishedAt descor
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
L
Or do I need to order first and then filter by tags?
Sep 9, 2022, 2:55 PM
S
Could you share your documenType schema please?
Sep 9, 2022, 6:10 PM
K
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
L
Hey
I am still having the same problem
đ˘
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
L
The sorting is not working. That is the main issue.
Oct 3, 2022, 9:40 AM
L
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
I can not even tell which sorting is returned
but definitely not sorted by the published date
Oct 3, 2022, 9:41 AM
L
user F
What am I doing wrong?Thanks for your help again.Oct 3, 2022, 9:45 AM
G
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
K
Hellooooo. I unfortunately don't have time today, I'm very sorry. đ
Oct 4, 2022, 8:54 AM
G
Alright! Thanks for the information.
Oct 4, 2022, 8:56 AM
G
Hi
đ
user F
Maybe it fits in today? đ
Oct 5, 2022, 7:10 AM
K
I'll try but not sure. đ
Oct 5, 2022, 7:14 AM
G
It's just that the order doesn't work in the query above.
Thanks!
Thanks!
Oct 5, 2022, 7:15 AM
G
Thanks
I am posting this again in the main channel. Maybe someone else has time?
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
G
Hi
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?
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
G
Thanks.
In my eyes this whole query is already a template literal
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
G
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,
Specifically,
$orderByis 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
G
Ah, now I see
Yes, this works too.
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
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
G
Ah I see. Thanks!
So in sum: Passing a Groq param to
So in sum: Passing a Groq param to
orderis not working.
Oct 6, 2022, 4:39 PM
G
If I need to pass the value as
as these are currently defined in a re-usable object.
${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
G
Thanks đ
Oct 6, 2022, 4:44 PM
Hi User. Thanks for your patience on this. Confirmed that a param canât be passed into (See follow-up.)
order().
Oct 6, 2022, 10:43 PM
G
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.
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.
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
G
Hi
Thanks for the update!
I tried
with param
$orderBy: "publishedAt desc"but it doesn't work.
The response is a 400 with
When I remove
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 }`
$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
G
Yes, sure. Sorry for the confusion.
Oct 11, 2022, 3:30 PM
G
It´s orderBy
Oct 11, 2022, 3:30 PM
G
Bit still the same error đ
Oct 11, 2022, 3:31 PM
The space between
publishedAtand
descis throwing that error. Iâm not sure thereâs a way to specify the order direction dynamically here so
descor
ascmight need to be hard-coded (with
$orderBychanged to just
publishedAt).
Oct 11, 2022, 4:11 PM
G
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",
đ
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
G
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
đ
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 docsNot 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.