Apollo Client — Industry-Leading GraphQL Client for TS/JS
Apollo Client is the industry-leading GraphQL client for TypeScript, JavaScript, React, Vue, Angular, and more. Powerful caching with normalized store, intuitive APIs, optimistic UI, subscriptions, and comprehensive developer tools.
Staging sûr pour cet actif
Cet actif est d'abord staged. Le prompt copié demande à l'agent d'inspecter les fichiers staged avant d'activer scripts, config MCP ou config globale.
npx -y tokrepo@latest install 70e5ac06-35a3-11f1-9bc6-00163e2b0d79 --target codexStage les fichiers d'abord; l'activation exige la revue du README et du plan staged.
What it is
Apollo Client is a comprehensive GraphQL client for TypeScript and JavaScript applications. It provides a normalized in-memory cache, declarative data fetching via hooks, optimistic UI updates, real-time subscriptions, and browser devtools for inspecting cache state.
Frontend developers building React, Vue, or Angular apps that consume GraphQL APIs use Apollo Client to handle data fetching, caching, and state management in one layer rather than juggling multiple libraries.
How it saves time or tokens
Apollo Client's normalized cache eliminates redundant network requests. When a query fetches an entity by ID, subsequent queries referencing the same entity read from cache instead of hitting the server. This reduces both API call volume and latency.
Optimistic UI lets you update the interface before the server confirms a mutation, making the app feel instant. Combined with automatic cache updates, this replaces manual state synchronization code that would otherwise take hundreds of lines.
How to use
- Install Apollo Client and GraphQL:
npm i @apollo/client graphql
- Create a client instance pointing to your GraphQL endpoint and wrap your app with ApolloProvider.
- Use the
useQueryhook in any component to fetch data:
import { useQuery, gql } from '@apollo/client';
const GET_USERS = gql`
query GetUsers {
users { id name email }
}
`;
function UserList() {
const { loading, error, data } = useQuery(GET_USERS);
if (loading) return <p>Loading...</p>;
if (error) return <p>Error: {error.message}</p>;
return (
<ul>
{data.users.map((u: any) => (
<li key={u.id}>{u.name}</li>
))}
</ul>
);
}
Example
Setting up an Apollo Client with InMemoryCache:
import { ApolloClient, InMemoryCache } from '@apollo/client';
const client = new ApolloClient({
uri: 'https://api.example.com/graphql',
cache: new InMemoryCache({
typePolicies: {
User: { keyFields: ['id'] },
},
}),
});
The typePolicies config tells the cache how to normalize entities by their unique key fields.
Related on TokRepo
- Coding Tools -- Developer tools for building and debugging web applications
- Automation Tools -- Automate GraphQL schema generation and testing pipelines
Common pitfalls
- Cache normalization requires unique
idor_idfields on every type. If your schema uses non-standard key fields, configuretypePolicies.keyFieldsor queries will not merge correctly. - Over-fetching with deeply nested queries can bloat the cache. Use fragments and field policies to keep cache size manageable.
- Subscriptions require a separate WebSocket link. Forgetting to configure
splitbetween HTTP and WS links is a frequent setup error.
Questions fréquentes
Yes. Apollo Client has official integrations for Vue (@vue/apollo-composable) and Angular (apollo-angular). The core client is framework-agnostic; the framework-specific packages provide hooks or services that wrap the core API.
Apollo Client stores GraphQL responses as flat, deduplicated entities keyed by type and ID. When two queries return the same User, the cache holds one copy. Updates to that user propagate to every component reading it, eliminating stale data without manual refetching.
Use Apollo Link to add an authorization header to every request. Create an authLink that reads your token from localStorage or a cookie and sets the Authorization header, then chain it with the httpLink using concat or from.
Yes. You can define client-side fields using the @client directive and resolve them with local resolvers or reactive variables. This lets you use Apollo as your single state management layer for both remote and local data.
Relay is optimized for large-scale apps with strict schema conventions and compiler-based optimizations. urql is lighter and more modular. Apollo Client sits between them with a rich feature set, large community, and flexible cache configuration.
Sources citées (3)
- Apollo Client GitHub— Apollo Client provides normalized caching and declarative data fetching for Grap…
- Apollo Client Documentation— Apollo Client supports React, Vue, and Angular integrations
- Apollo Cache Configuration— Normalized cache with type policies for custom key fields
En lien sur TokRepo
Fil de discussion
Actifs similaires
Undici — High-Performance HTTP Client for Node.js
The official HTTP/1.1 client for Node.js, written from scratch to provide a faster, more correct, and fully featured alternative to the legacy http module.
Got — Human-Friendly HTTP Client for Node.js
Got is a lightweight, feature-rich HTTP client for Node.js with built-in retry logic, pagination, caching, and hooks for composable request pipelines.
Apollo Server — Production GraphQL Server for Node.js
Apollo Server is an open-source, spec-compliant GraphQL server that works with any Node.js HTTP framework. It provides schema-first development, built-in caching, federation support for microservices, and integrations with Express, Fastify, Koa, and serverless platforms.
URQL — Lightweight Extensible GraphQL Client for React and Beyond
URQL is a highly customizable and lightweight GraphQL client for JavaScript frameworks that prioritizes simplicity and extensibility through a modular exchange-based architecture.