Apollo Client

Apollo Client Creation

The Apollo Client is set up using the GraphQL server's URI and the client's cache is configured.

typescriptCopy codeimport { ApolloClient, InMemoryCache } from "@apollo/client/core/index.js";
import { HttpLink } from "@apollo/client/link/http/HttpLink.js";

export function createApolloClient(uri: string) {
  return new ApolloClient({
    link: new HttpLink({
      uri,
    }),
    ssrMode: typeof window === "undefined",
    connectToDevTools:
      typeof window !== "undefined" && process.env.NODE_ENV === "development",
    cache: new InMemoryCache(),
  });
}
  • uri: The endpoint of your GraphQL server.

  • ssrMode: Configures the client for server-side rendering if the code is running on the server.

  • connectToDevTools: Enables connection to browser development tools in development mode.

  • cache: Configures the client to use an in-memory cache for storing query results.

Fetching Data

To fetch data specific to a blockchain network (e.g., Polygon or BSC), the appropriate GraphQL subgraph URI is determined based on the network chain ID. Then the Apollo Client executes a query to the subgraph.

Last updated