Sorting in autogenerated GraphQL queries

Fauna relies on your feedback to educate our product development process. To help us prioritize this feature in our product roadmap, please vote for this topic and complete our 2020 GraphQL Roadmap Survey. Additionally, please reply to this post to share your use case/s for this feature, and your experiences (good or bad) using it in other products.

Proposal

This would likely follow the autogenerated API defined by the OpenCRUD spec. See OpenCRUD: Queries

Workaround example

While you wait for this new feature to be released, here is one way to work around it. If you have implemented your own solutions that you think could help other users, please share them as replies to this topic and/or make a gist and link to it from the awesome-faunadb list.

1. Import schema:

type Post {
  title: String!
  content: String!
}

type Query {
  allPostsSortedByTitle: [Post!]! @resolver(name: "all_posts_sorted_by_title", paginated: true)
}

2. Create index

CreateIndex(
  {
    "name": "all_posts_sorted_by_title",
    "source": Collection("Post"),
    "terms": [],
    "values": [
      {
        "field": [
          "data",
          "title"
        ]
      },
      {
        "field": [
          "ref"
        ]
      }
    ],
  }
);

3. Update UDF:

Update(
  Function("all_posts_sorted_by_title"),
  {
    body: Query(Lambda(["size", "after", "before"],
      Let(
        {
          match: Match(Index("all_posts_sorted_by_title")),
          page: If(
            Equals(Var("before"), null),
            If(
              Equals(Var("after"), null),
                Paginate(Var("match"), { size: Var("size") }),
                Paginate(Var("match"), { size: Var("size"), after: Var("after") })
            ),
            Paginate(Var("match"), { size: Var("size"), before: Var("before") }),
          )
        },
        Map(Var("page"), Lambda("values", Get(Select(1, Var("values")))))
      )
    ))
  }
)

Note: the example could be improved by allowing the sorting fields to be defined as an argument of the sorting query (similar to the filtering example).

I think filter, search and sort are the 20% of the custom resolvers that are being built 80% of the time. If they would come baked in the GraphQL API, that would save devs a lot of time of writing and testing custom resolvers+UDFs.
Something similar to what other vendors do (eg. Prisma, Dgraph…)

3 Likes

Something like this would save a lot of time and really bump up the competitive advantage of Fauna: Filtering (Reference) | Prisma Docs

1 Like

@OlivierMills that would be definitely a case of

2e6e6dae8e77126e606ef90fa5615d58

1 Like

@vasco3 and@olivermills thank you for your input here. I am going to close this ticket as we do not to intend to support the OpenCRUD spec. We are working on improvements to the query language which will make writing resolvers, which perform sorting and filtering much easy to write. Please stay tuned for those updates coming soon