Using Apollo Client Without React
I’ve been experimenting with Apollo Client recently and started work on a new package that will use it as a dependency. After some initial experiments in a Create React App project, I scaffolded a new vanilla JS project using Vite. Much to my surprise, when I added Apollo my build started failing because React was missing as a dependency. It took me a little longer than I expected to figure out why this was happening.
As of Apollo 3.0 the import example in the getting started docs will also bring some React hooks along for the ride:
import { ApolloClient, gql, InMemoryCache } from "@apollo/client";
To use Apollo in a vanilla JS project, you instead need to use the entry point @apollo/client/core
:
import { ApolloClient, gql, InMemoryCache } from "@apollo/client/core";
This is documented, but was a little hard to find within the Apollo 3.0 migration guide. Hopefully giving this a little more Google juice might save someone a few minutes in the future.