A Small Helper Function for Conditional Requests in Apollo GraphQL

Perttu Lähteenlahti
2 min readFeb 23, 2024

In a project I’m working on, we have a case where many of the queries ran using the autogenerated Apollo GrapQL hooks that depend on the userID. However, this ID might be null at times, such as when the user's authentication session has ended. This leads to issues in two places:

  • The hook will try to run with a query that has no userID, leading the backend to reject it and return no data/error message on null format userID
  • Typescript will complain about userID possibly being undefined or null

--

--