biota icon indicating copy to clipboard operation
biota copied to clipboard

Add custom resolvers

Open gahabeen opened this issue 4 years ago • 0 comments

Summary

Goal is to be able to resolve data from a document with FQL. It would not be merged with the data itslef but instead provided on the side in a resolved property. Main reason being that I don't want to mess-up with the editable properties with resolved ones.

Any number of resolvers could be provided per collection. They would get merge (with q.Merge) when pushed.

Basic example

A users collection has the fields profile.firstName and profile.lastName. We would like to get the fullName for quick display. We would create the following resolver object:

{
  fullName: Let(
    {
      firstName: Select(['profile', 'firstName'], Var('doc'), null),
      lastName: Select(['profile', 'firstName'], Var('doc'), null),
      list: Filter([Var('firstName'), Var('lastName')], Lambda(['item'], Not(IsNull(Var('item'))))),
    },
    Concat(Var('list'), ' ')
  ),
}

Which would end up being wrapped in a User-Defined Function:

Query(Lambda(["ctx", "inputs"], q.Let({ doc: Select("doc", "inputs", {})}, <RESOLVER_OBJECT>)))

Which when getting the following document:

{
  "profile": {
    "firstName": "Gabin",
    "lastName": "Desserprit"
  }
}

would return:

{
  "data": {
    "profile": {
      "firstName": "Gabin",
      "lastName": "Desserprit"
    }
  },
  "resolved": {
    "fullName": "Gabin Desserprit"
  },
 "context: { contextInformations },
 "errors": { errorInformations }
}

Motivation

Provide the ability to fetch processed content straight from Fauna, without any in-between server.

gahabeen avatar Apr 26 '20 08:04 gahabeen