Handling refs ('@ref') in javascript

Sorry if I’m missing something obvious, but I’m having a bit of trouble sending refs back and forth between my backend and frontend. When I paginate I get back something like this:

  "after": [
    1.5,
    {
      "@ref": {
        "id": "287890180395762177",
        "collection": {
          "@ref": {
            "id": "foo",
            "collection": { "@ref": { "id": "collections" } }
          }
        }
      }
    },
   ... etc
  ]

When I try to pass this as the after key to paginate I get "Ref expected, Object provided.".
Do I have to manually turn it into refs, somewhat like:

after: after.map((el) =>  q.Ref(q.Collection('foo'), el['@ref'].id))

Or is there an intended solution for this?

Hi @jescowuester. This is very related to question such as some issues raised on our driver such as these ones:

Instead of passing the ref to your frontend you want to get the id out of that ref and instead construct the ref again in your backend once your frontend provides you with the id.

Ref(Collection("..."), "287890180395762177")

Not ideal, I know, we’re hoping to make it easier to serialize/deserialize these references in the future. A ticket has been made internally.

Did you find a reasonable solution for this?

The parseJSON function is now exported from the JS driver. After you receive the response from your own API, you can run pass it into parseJSON to transform the result into the Fauna values.

import { parseJSON } from 'faunadb'

// ...

const resultJSON = await fetchFromMyAPI()

const resultFaunaValues = parseJSON(resultJSON)

// ...

This change was made in version 4.3 (release notes) of the driver, so you sure it’s updated to at least that.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.