Reduce not compatible with Graphql

As per the issue below:

When using Paginate alone, the result is:

type Page<T> = {
  before: Ref,
  after: Ref,
  data: Array<T>
}

However, I’ve established that when using Reduce with Paginate, the return becomes:

type Page<T> = {
  before: Ref,
  after: Ref,
  data: Array<Array<T>>
}

Notice that result.data becomes an Array<Array<T>> which is going to cause an error when you use Reduce for a custom query.

My custom query:

getUserGoals: [Goal!] @resolver(name: "getUserGoals", paginated: true)
Update(Function(“getUserGoals”), {
  role: “admin”,
  body: Query(
    Lambda(
      [“size”, “afterCursor”, “beforeCursor”],
      Reduce(
        Lambda(
          [“acc”, “current”],
          Let(
            {
              doc: Get(
                Select(1, Var(“current”))
              )
            },
            If(
              Not(
                ContainsPath(
                  [“data”, “deletedAt”],
                  Var(“doc”)
                )
              ),
              Append(Var(“doc”), Var(“acc”)),
              Var(“acc”)
            )
          )
        ),
        [],
        Paginate(
          Match(
            Index(“userGoalsBySortOrder”),
            CurrentIdentity()
          )
        )
      )
    )
  )
});

The error:

{
  “data”: {
    “getUserGoals”: {
      “data”: [
        null
      ]
    }
  },
  “errors”: [
    {
      “message”: “Cannot return null for non-nullable type (line 4, column 7):\n      _id\n      ^“,
      “path”: [
        “getUserGoals”,
        “data”,
        0,
        “_id”
      ],
      “locations”: [
        {
          “line”: 4,
          “column”: 7
        }
      ]
    },
    {
      “message”: “Cannot return null for non-nullable type (line 5, column 7):\n      name\n      ^“,
      “path”: [
        “getUserGoals”,
        “data”,
        0,
        “name”
      ],
      “locations”: [
        {
          “line”: 5,
          “column”: 7
        }
      ]
    }
  ]
}

Notice that the returned value from graphql is:

“data”: {
  “getUserGoals”: {
    “data”: [
      null
    ]
  }
}

Why use reduce?

Reduce, reduces :wink: the number of times you have to loop, consider that you have a data that you want to filter, so what you’ll do in fauna is Paginate.Map.Filter, the BigO notation of such is O(2n). But if you use Paginate.reduce, you would have reduced :wink: that to O(n).