Skip to content

fix(apollo-client): do not throw when client is undefined - #87

Open
cpruijsen wants to merge 1 commit into
expo:mainfrom
cpruijsen:fix/issue-41-94756754
Open

cpruijsen wants to merge 1 commit into
expo:mainfrom
cpruijsen:fix/issue-41-94756754

Conversation

@cpruijsen

@cpruijsen cpruijsen commented Oct 8, 2026 •

Copy link
Copy Markdown

useApolloClientDevTools no longer throws a TypeError when the Apollo client is undefined or null. Fixes #41.

The client is unset on the first render when the app creates it asynchronously, for example in a useEffect after it restores a persisted cache. Hooks cannot be called conditionally, so callers had no workaround.

In packages/apollo-client/src/useApolloClientDevTools.ts the effect now returns early when the client is not set. apolloClient is already in the dependency array, so the effect runs again when the client becomes available. The parameter type is now ApolloClientType | null | undefined, so a client held in state type-checks without a cast.

#33 handled a null client with optional chaining inside the helper functions. I used one guard at the top of the effect instead: optional chaining stops the crash, but the hook still registers listeners and sends empty state for a client that does not exist yet. I can switch to the optional-chaining approach if you prefer it.

build/ is not regenerated, same as #85.

The new src/__tests__/useApolloClientDevTools.test.ts covers undefined and null clients, and a mock client that must still get the listeners, __actionHookForDevTools and the initial GQL:response. The two null-client tests fail without the fix (Cannot read properties of undefined (reading 'queryManager')) and pass with it.

useApolloClientDevTools ran its effect unconditionally, so passing an
undefined or null ApolloClient (e.g. while it is still being created in
state) threw a TypeError inside getCurrentState's setTimeout when
getAllMutations read client.queryManager.mutationStore, and again on
apolloClient.__actionHookForDevTools. Hooks cannot be called
conditionally, so callers had no workaround.

The effect now returns early when the client is falsy and re-runs once
the client exists, since apolloClient is already in the dependency
array. The parameter type is widened to ApolloClientType | null |
undefined so this usage type-checks.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

useApolloClientDevTools should not throw when given undefined as client

1 participant