graphql-php: How to handle a partial error?
Suppose we are querying a list of post:
Query {
posts: [Post]
}
query {
posts {
id
title
body
}
}
The requesting user does not have the read permission of some of the retrieved posts.
Say if we got 10 posts, 2 of them are not allowed to be read by the requesting user (or maybe only the title
field is can be read).
How can I handle such a “partial error” case?
About this issue
- Original URL
- State: closed
- Created 6 years ago
- Comments: 18 (6 by maintainers)
If you don’t need a title, then you can just return an array with 10 entries, but have nulls in place of entries which user can’t read (since your types are defined as nullable):
Also you may try returning an Exception in place of null:
It will still return nulls in the final result but will also add two entries in
errors
section of the result.And yet another way is to have some field defined as non-null in your
Post
type and then throw there:If you throw an exception in
body
resolver, the wholepost
in resulting list will be replaced with null (since post is nullable) and your error added to resulterrors
.But: I understand you have a desire to solve this differently => not arguing against it and I’m following this discussion with interest!
Or:
Counter-question: why are those then retrieved?
Just throwing in some perspective, suggested points 1) and 2) are perfectly valid and it really depends on your use-case.
In my casts, as I’m working with a system which also has the concept of posts, the ones the user has no permission are never returned (and if you try to get a specific post you don’t have permission it’s treated as missing).