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

Question: how to pass an <expression> as param? #36

Open
m4rrc0 opened this issue Mar 31, 2023 · 3 comments
Open

Question: how to pass an <expression> as param? #36

m4rrc0 opened this issue Mar 31, 2023 · 3 comments

Comments

@m4rrc0
Copy link

m4rrc0 commented Mar 31, 2023

Hey. I am trying to use an input field from my document to specify the ordering of the query.
The order() function takes an <expr> apparently and I cannot find a way to pass an <expr> from the params...

Am I missing something?

I would like to accomplish something like that:

{
  query: `*[_type == "mytype"] | order($sortProp $sortDirection)`,
  params: { sortProp: "name", sortDirection: "asc" }
}

I think the easiest way to accomplish what I want would be to be able to pass a function to query so that we could compose the query easily with template literals.

@SimeonGriggs
Copy link
Collaborator

It’s not possible with GROQ to use a variable for a key name, unfortunately

@m4rrc0
Copy link
Author

m4rrc0 commented Mar 31, 2023

Ok, that is what I thought.

In this plugin specifically, would it be possible to have the query parameter accept a function so that we can modify it manually?
I am thinking something like this:

{
  query: ({ params, ...maybeSomethingElse }) => `*[_type == "mytype"] | order(${params.sortProp} ${params.sortDirection})`,
  params: { sortProp: "name", sortDirection: "asc" }
}

@m4rrc0
Copy link
Author

m4rrc0 commented Mar 31, 2023

For reference, I suggested adding a feature on the GROQ spec Github here.

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

No branches or pull requests

2 participants