apollographql / react-apollo

:recycle: React integration for Apollo Client
https://www.apollographql.com/docs/react/
MIT License
6.85k stars 790 forks source link

[v3] Local state management #4035

Open TeoTN opened 4 years ago

TeoTN commented 4 years ago

Intended outcome: I'm trying to implement storing a collection of notifications fully on the client side. However, the documentation contains plenty of references to cache.writeData that doesn't exist:

TS2339: Property 'writeData' does not exist on type 'InMemoryCache'.

I've implemented local type defs and resolvers using writeFragment and writeQuery for initial data, but the cache is not updated. The documentation on local state management is rather chaotic, as it discusses 10 things at a time, and requires jumping back and forth to "Caching" section. I wasn't able to find a good example of how to store a collection of simple objects in the cache, so that it feels like it was coming from the API actually.

Actual outcome:

How to reproduce the issue:

import { ApolloClient, InMemoryCache } from '@apollo/client';

const cache = new InMemoryCache();
const client = new ApolloClient({
  cache,
  resolvers: { /* ... */ },
});

// writeData doesn't exist
cache.writeData({
  data: {
    todos: [],
    visibilityFilter: 'SHOW_ALL',
    networkStatus: {
      __typename: 'NetworkStatus',
      isConnected: false,
    },
  },
});

Version

 System:
    OS: Linux 4.4 Ubuntu 18.04.4 LTS (Bionic Beaver)
  Binaries:
    Node: 13.3.0 - ~/.nvm/versions/node/v13.3.0/bin/node
    Yarn: 1.21.1 - ~/.nvm/versions/node/v13.3.0/bin/yarn
    npm: 6.13.1 - ~/.nvm/versions/node/v13.3.0/bin/npm
  npmPackages:
    @apollo/client: 3.0.0-rc.10 => 3.0.0-rc.10
    @apollo/link-ws: 2.0.0-beta.3 => 2.0.0-beta.3