Consider a User
type, with an email
field that, for certain "anonymous" users, should only be accessible if the request was properly authorized. Other users are fine with having their email publicly displayed.
type User {
name: String!
"Anonymous users have the email only accessible to admins"
email: String!
}
What are the pros and cons of these two approaches for handling that field in the email
resolver for anonymous users?
throw
if the request is unauthorized. In that case,email
needs to be declared as nullableString
instead ofString!
, or the entire query will error.- The client may want to match the
errors
to thedata
. Because non-anonymous users will have their email accessible without authorization,errors
anddata
may have different numbers of elements, so this matching seems impossible, at least with apollo-server, which doesn't return anything in eacherrors
element that would indicate to whichuser
it belongs. email
will be misleadinglynull
for anonymous users, confusing the situation with that of the email never having been added in the first place. Remember thatemail
needs to beString
, notString!
, so anull
email is reasonable.- There is a clear
errors
array in the response, so this feels like the "proper" approach?
- return the email in a redacted form, e.g
[NOT AUTHORIZED TO ACCESS THIS USER'S EMAIL]
.- This keeps the objects intact with clear errors for sensitive emails, instead of misleading "null" emails.
email
can stay non-nullable- No need to try to match
errors
withdata
. - There is no explicit
errors
array in the response, so this feels like a hack.
Note that for arrays, returning a REDACTED
form is not an option, because the query may ask for a field within the array (e.g. { anonUsers { email } }
). The only option there is to return []
(directly or by throw
ing).
Am I missing anything? Is there prior work on this topic? How can I make a decision?