GraphQL throws error of non-nullable field on a nullable field

GraphQL throws error of non-nullable field on a nullable field


0

I’ve the following schema

type Employee {
    id: UUID!
    createdAt: Time!
    updatedAt: Time!
    lastActivity: Activity
}

type Activity {
    id: UUID!
    createdAt: Time!
    updatedAt: Time!
}

extend type Query {
    Myself: Employee!
}

The lastActivity may or may not be present of course, but when querying it from my client it throws the following error

{
    "errors": [
        {
            "message": "the requested element is null which the schema does not allow",
            "path": [
                "Myself",
                "lastActivity",
                "createdAt"
            ]
        }
    ],
    "data": {
        "Myself": {
            "id": "257d9db3-118e-4728-9e6b-ef28e87b4907",
            "lastActivity": null,
            "__typename": "Employee"
        }
    }
}

What I send from the client is the following

query Myself {
  Myself {
    id
    lastActivity {
      id
      action
      createdAt
    }
    organization {
      id
      name
      slug
    }
  }
}

I’m not sure to understand why the query isn’t satisfied, because Activity in the context of Employee is indeed nullable. I tried changing the Activity type itself to make everything nullable and it solved the problem, but it’s only in this context that I want it nullable, not other ones. What I receive right now is actually what I expected.

What am I doing wrong here?

PS: I use Apollo on the client side with Vite and GQLGen with Golang on the server side, if that changes anything.

1

  • I read that error message as saying a lastActivity exists, but its createdAt time is incorrectly null. When a field error is reported the nearest nullable ancestor of the failing field becomes null in the response data.

    – David Maze

    4 mins ago


Load 3 more related questions


Show fewer related questions

0



Leave a Reply

Your email address will not be published. Required fields are marked *