✓ Recommended
GraphQL Client with Apollo/urql
GraphQL client patterns with Apollo Client, urql, and type-safe query generation.
CLAUDE.md
# GraphQL Client with Apollo/urql
You are an expert in GraphQL, Apollo Client, urql, and TypeScript codegen.
Query Patterns:
- Colocate queries with components — each component declares its own data needs
- Use fragments to share field selections across queries: `fragment UserFields on User { id name email }`
- Name all operations: `query GetUser($id: ID!) { ... }` — never use anonymous queries
- Use variables for all dynamic values — never interpolate into query strings
Apollo Client:
- Configure cache with `InMemoryCache` and type policies for custom field behavior
- Use `useQuery()` for data fetching: `const { data, loading, error } = useQuery(GET_USER, { variables: { id } })`
- Use `useMutation()` with optimistic response for instant UI updates
- Update cache after mutations: use `cache.modify()` or `refetchQueries` option
- Use `fetchPolicy` appropriately: cache-first for stable data, network-only for fresh data
urql (Lightweight Alternative):
- Simpler API with document caching by default
- `const [result] = useQuery({ query: GetUser, variables: { id } })`
- Use `@urql/exchange-graphcache` for normalized caching (like Apollo)
- Built-in SSR support with `ssrExchange`
Type Safety with Codegen:
- Use `graphql-codegen` to generate TypeScript types from schema + operations
- Configure `@graphql-codegen/typescript-operations` for query/mutation types
- Use `@graphql-codegen/typed-document-node` for fully typed useQuery/useMutation
- Run codegen in watch mode during development: `graphql-codegen --watch`
Error Handling:
- GraphQL can return data AND errors simultaneously — always check both
- Use error policies: `errorPolicy: 'all'` to receive partial data with errors
- Distinguish between network errors (no response) and GraphQL errors (in response)
- Implement retry logic for network failures with exponential backoff
Performance:
- Use `@defer` directive for slow fields — stream them in progressively
- Use `@stream` for large lists — render items as they arrive
- Implement pagination with cursor-based connections (Relay spec)
- Use persisted queries in production to reduce request size
- Batch multiple queries with query batching or `@apollo/client` batch link
Add to your project root CLAUDE.md file, or append to an existing one.