apollo-server

Apollo Server

Apollo Server patterns for building GraphQL APIs with schema-first development

Details

Language / Topic
graphqlGraphQL
Category
framework
Compatible Frameworks
apollo-server

Rules

balanced
- Initialize `ApolloServer` with `typeDefs`, `resolvers`, and a `context` factory function — pass dataloaders and authenticated user through context, never as globals.
- Use `useQuery` and `useMutation` hooks from `@apollo/client` for data fetching in React — avoid manual `fetch` calls to the GraphQL endpoint.
- Configure the Apollo Client `InMemoryCache` with `typePolicies` to define how normalized entities are keyed and merged across queries.
- Use `ApolloProvider` at the app root with a single `ApolloClient` instance — pass the client as a prop rather than creating it inside components.
- Apply `apolloServerPlugin` for request lifecycle hooks: logging, tracing, and error reporting without modifying individual resolvers.
- Use `readQuery`/`writeQuery` for optimistic UI updates rather than forcing a refetch after every mutation.
- Configure `defaultOptions` on the client to set consistent `fetchPolicy` values: `cache-first` for reads, `no-cache` for auth-sensitive queries.