2

I have a GraphQL query that returns a set of notifications. The "creator" field does not appear in every notification which is a problem because it is of type "User" and User has an "id" field that is non-nullible. Is it possible to have non-nullible fields nested in nullable ones?

{
  myNotifications {
    id
    title
    message
    image
    creator {
      id
      full_name
    }
  }
}

1 Answer 1

2

A non-null field will only be validated if it is resolved. If the parent field resolve to null, any children fields will not be resolved, and so the validation never happens. In other words, it's perfectly fine to have a non-null field whose parent field is nullable.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks Daniel. I'm not sure if I was totally clear (or I am still misunderstanding). I would like "User" types (the "creator" above) to have a non-nullable "id". However, the "Notification" type (which the "myNotifications" query above returns an array of) may not always have a creator, but my resolvers are returning null for "id" in those cases, which throws an error. You are saying that I need to make sure the resolver for a notification returns null for the "creator" and not any of its subfields?
Correct, I would expect creator to be null if it doesn't exist -- it shouldn't return an object with all of its properties resolving to null.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.