sanity-plugin-documents-pane icon indicating copy to clipboard operation
sanity-plugin-documents-pane copied to clipboard

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

Open m4rrc0 opened this issue 1 year ago • 3 comments

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.

m4rrc0 avatar Mar 31 '23 08:03 m4rrc0

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

SimeonGriggs avatar Mar 31 '23 11:03 SimeonGriggs

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 avatar Mar 31 '23 16:03 m4rrc0

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

m4rrc0 avatar Mar 31 '23 16:03 m4rrc0