Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dynamic ordering #1090

Open
ozgurdemir opened this issue Dec 4, 2024 · 2 comments
Open

Dynamic ordering #1090

ozgurdemir opened this issue Dec 4, 2024 · 2 comments
Labels
question Further information is requested

Comments

@ozgurdemir
Copy link

ozgurdemir commented Dec 4, 2024

I'm trying to order a GROQ query dynamically by passing an $order variable.

Hardcoding does work

const query = groq`{"BlogPosts": *[_type=='blog_post'] | order(publishedAt desc)}`;
const { data, status } = await useSanityQuery(query)

Passing a variable does not work. The ordering is just ignored and the results are returned without ordering

const query = groq`{"BlogPosts": *[_type=='blog_post'] | order($order desc)}`;
const { data, status } = await useSanityQuery(query, {
  'order': 'publishedAt',
});

The same when passing the order field as a string. This does not work:

const query = groq`{"BlogPosts": *[_type=='blog_post'] | order('publishedAt' desc)}`;
const { data, status } = await useSanityQuery(query)

Am I passing the order variable in a wrong format or is this not possible with this module?

@ozgurdemir ozgurdemir added the question Further information is requested label Dec 4, 2024
@silvio-e
Copy link
Contributor

silvio-e commented Dec 5, 2024

I haven't tested your query but what jumped at me is that you are not using a template literal for the query like:

const query = groq`{"BlogPosts": *[_type=='blog_post'] | order($order desc)}`;
const { data, status } = await useSanityQuery(query, {
  'order': 'publishedAt',
});```

@ozgurdemir
Copy link
Author

I haven't tested your query but what jumped at me is that you are not using a template literal for the query like:

const query = groq{"BlogPosts": *[_type=='blog_post'] | order($order desc)};
const { data, status } = await useSanityQuery(query, {
'order': 'publishedAt',
});```

Thx for the response. However, this was just a copy paste error. I've updated the questions to include the correct backticks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants