Apollo GraphQL Client With Cache
Configure an Apollo Client with in-memory cache error handling and typed queries for GraphQL data fetching.
Code
import{ApolloClient,InMemoryCache,gql}from'@apollo/client';export function createApolloClient(uri){return new ApolloClient({uri,cache:new InMemoryCache({typePolicies:{Query:{fields:{items:{merge(existing=[],incoming){return[...existing,...incoming];}}}}}}),defaultOptions:{query:{errorPolicy:'all',fetchPolicy:'cache-first'}},onError:({networkError,graphQLErrors})=>{if(networkError)console.error('network:',networkError);if(graphQLErrors)console.error('graphql:',graphQLErrors);}});}export const GET_ITEMS=gql`query GetItems($limit:Int){items(limit:$limit){id name}}`;Report a problem with this exampleOpens a pre-filled issue on GitHub.