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…)

1 Like