Cannot return null for non-nullable type

There is one document in the Ranking collection.
I have addressed similar issues before, however, this one is slightly different since I get the error with Basic authorization unlike here and I’m already selecting ‘data’ unlike here.
The error:

"message": "Cannot return null for non-nullable type (line 3, column 5):\n    active\n    ^",

the function FQL:


{
  name: "all_rankings",
  role: null,
  body: Query(
    Lambda(
      [],
      Select(
        "data",
        Map(Paginate(Match(Index("allRankings"))), Lambda("x", Get(Var("x"))))
      )
    )
  )
}

the index:

{
  name: "allRankings",
  unique: false,
  serialized: true,
  source: "Ranking"
}

the query in PG:

query AllRankings {
  allRankings {
    active
    rankingname
    rankingownerid
  }
}

the relevant schema bits:

type Ranking
  { 
    active : Boolean!
    rankingname : String!
    rankingdesc : String
    rankingownerid : String!
    }
allRankings: [Ranking] @resolver(name: "all_rankings")
  1. Why do I get ‘Cannot return null for non-nullable type’ error?
  2. What can I do to fix?
    Thanks …

Fauna does automatically create graphql queries using the same name as the Indexes you create. You may not need to create an FQL function at all the achieve this. You can probably add this:

Query {
all_rankings: [Ranking]
}

and then in your actual query

query AllRankings {
  all_rankings {
    data {
      active
      rankingname
      rankingownerid
    }
  }
}

I do think that this would require you to update your schema in Fauna again. Even though the query is available you have to add it to your Query type manually.

But to answer your question, the FQL function you have written has a few issues.

  1. I believe you are wanting to use Select() to return only the data{}. If that is the case you are using it in the wrong spot. You want to use Select() on the Get() function that is actually retrieving the document from your collection. The reason you are getting null as your result is because you have the Select() function trying to select the path ['data'] on the paginated result of the index. Since there is no [‘data’] path on that result you are getting null. You want your FQL to look like the below.
{
  name: "all_rankings",
  role: null,
  body: Query(
    Lambda(
      [],
      Map(
        Paginate(Match(Index("allRankings"))),
        Lambda("x", Select("data", Get(Var("x"))))
      )
    )
  )
}
  1. Fauna uses the Paginate() function as a way to break up the return results for performance purposes. If you are creating a custom FQL function in conjunction with Graphql you probably want to get familiar with Fauna’s pagination. In your FQL you are using Paginate() which mean this FQL will only return the first 64 results of your query. Which may or may not be all of your rankings. You can increase the size of the result page like the below or pass paginated: true to the query and use pagination logic on your client to allow the user to page through the results instead of having them all appear in one query. When I was first getting used to Fauna I just passed in a large size to the page in hopes I would not miss any. Since I have been using Fauna for awhile now I have switched to using pagination and implemented infinite scroll pagination logic on my client. See below for how to increase the size on your page.
{
  name: "all_rankings",
  role: null,
  body: Query(
    Lambda(
      [],
      Map(
        Paginate(Match(Index("allRankings")), { size: 200 }),
        Lambda("x", Select("data", Get(Var("x"))))
      )
    )
  )
}

I have a query (slightly different to the one posted above, but ‘working’ apart from the issue below):

query AllRankings {
allRankings {
active
rankingname
rankingownerid
}
}

based on schema entry:

allRankings: [Ranking] @resolver(name: "all_rankings")

which returns 2 of my 3 documents with the following error:

{
"data": {
"allRankings": [
{
"active": true,
"rankingname": "Test1Rnk",
"rankingownerid": "283120055707763201"
},
{
"active": true,
"rankingname": "Test3Rnk",
"rankingownerid": "283120055707763201"
},
null
]
},
"errors": [
{
"message": "Cannot return null for non-nullable type (line 5, column 5):\n rankingownerid\n ^",
"path": [
"allRankings",
2,
"rankingownerid"
...

The document data is:

{
"ref": Ref(Collection("Ranking"), "282953512300577285"),
"ts": 1606449353050000,
"data": {
"active": true,
"rankingname": "Test1Rnk",
"rankingdesc": "t1",
"rankingownerid": "283120055707763201"
}
}

{
"ref": Ref(Collection("Ranking"), "283311136082756097"),
"ts": 1606449338320000,
"data": {
"active": true,
"rankingname": "Test3Rnk",
"rankingdesc": "U19s",
"rankingownerid": "283120055707763201"
}
}

{
"ref": Ref(Collection("Ranking"), "283315189344895493"),
"ts": 1606449269540000,
"data": {
"active": true,
"rankingname": "Test2Rnk",
"rankingdesc": "U19s",
"rankingowneraddr": "283120055707763201"
}
}

The all_rankings function (based on recommendation above) is:

Query(
Lambda(
[],
Select(
"data",
Map(Paginate(Match(Index("allRankings"))), Lambda("x", Get(Var("x"))))
)
)
)

The allRankings index is:

{
  name: "allRankings",
  unique: false,
  serialized: true,
  source: "Ranking"
}

Why does the Test2Rnk document ‘fail’, but the other 2 ‘pass’?
thanks …

Hi @FreeRoss,

document with Ref 283315189344895493 does not have rankingownerid but it has rankingowneraddr instead.

Luigi

Thanks @Luigi_Servini. I should have spotted it, but didn’t. Appreciate your assistance.