I have this type:
type Profile {
id: ID! @isUnique
email: String! @isUnique
...
}
And a query:
profile(email: String!):Profile
When I run the query with a user that doesn't exist, my underlying resolver returns null and I was expecting GraphQL to like this.
But, I get this error:
Cannot return null for non-nullable field Profile.id.
This happens because the query is expected to return a Profile and a Profile must have id field.
But, the query's return type is not non-nullable Profile!, it is Profile which would mean the query might return nothing.
How to fix this properly?
Try returning
nullin your profile query resolver. If you return something like an empty object, the Profile resolver will pick that up and attempt to returnundefinedasProfile.id, which as you noted is a schema error.Try something like this:
your graphQL response will then look something like this