Skip to content

Using params in functions (E.g. *[] | order($orderBy))Β #103

@m4rrc0

Description

@m4rrc0

I have been looking for a solution for this but it is apparently impossible (or at least very impractical; see below).

For example:
*[] | order($orderBy)
With params
{ "orderBy": "createdAt"}

The only "not-really-a-solution" I found is this:

*[] | order(select(
  $orderBy == "_createdAt" => _createdAt,
  _id
))

(context: I am using the plugin sanity-plugin-documents-pane and the Sanity studio crashed when trying to do the same for asc)

This is deeply impractical.

Improvements I can come up with:

  • A function or specific syntax to unquote strings. E.g. *[] | order(unquote($orderBy))
  • Being able to lookup a scope for a dot-notation path.

*[] | order(@[$orderBy])

Or

*[]
{
  ...@,
  "temp": @[$orderBy],
}
| order(temp)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions