Description
I see, that it looks like obvious issue and reported under #192, raised few times and has explanation. However, today I faced same issue. After a short battle I figured out why I faced it. The issue is that in our NodeJs based GraphQL server we where able to do:
listUsers: [User!]!
which states, that result of listUsers
will always be an array (outer-most !
), of non null elements (inner !
). what really means that in case of empty result set, rendered output will be:
"listUsers": []
At least under NodeJs Apollo. Under Sangria, deferOpt
as suggested requires type to be OptionType(ListType(User))
which obviously leads to definition:
listUsers: [User!]
where outer-most !
is gone, meaning that in case of empty set result will be and it is:
"listUsers": null
which is, logical, but inconvenient. Because:
- if result is empty array like
[]
, on client side you don't have to doif (result != null)
checks, just loop/map/etc. which effectively does nothing on empty array, but without if null check throws exception. - it saves 2 character spaces :) kidding
I could not find way to achieve []
array result with Sangria, any body knows?
UPDATE: here is actually []
can be returned:
lazy val listUsersByGroup =
Fetcher.caching((_: MyCtx, groups: Seq[String]) => {
db.run(users.filter(_.group inSet groups).resultList).map { r =>
groups.map(g => r.filter(_.group == g)
}
})(HasId(_.headOption.map(_.who).getOrElse("abc")))
where if initial request was done for group "abc", if it is hardcoded for HasId
, the result is rendered as expected for defer
without OptionType
like:
"listUsersByGroup": []
However, it is very messy (even if possible) to pass information about id for empty result set. I also tried to use Relation
, but it is somehow does not fit my case.