question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to get model from cache by id and __typename

See original GitHub issue

I want to get model from the cache and update it having a model id and __typename. So, instead of:

    const data = proxy.readQuery({ query: TodoAppQuery });
    data.todos.push(createTodo);
    proxy.writeQuery({ query: TodoAppQuery, data });

do following:

    const model = proxy.readModel(__typename, id);
    model.todos.push(createTodo);
    proxy.writeModel(model);

This approach is much cleaner and readable, produces less boilerplate code and prevents confusion on what query to use to get and update the model (when I have multiple queries returning same models, for example TodoByIdQuery and TodoListQuery)

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:1
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

4reactions
tushar-singhcommented, Oct 25, 2018

I was also looking for this. Looks like you can do it using client.cache.data.get (at least if you’re using apollo-cache-inmemory)

<ApolloConsumer>
    {client => (
        <button
            onClick={() => {
                // get item from cache
                const item = client.cache.data.get(`TypeName:${id}`);
                
                // set new value
                client.cache.data.set(`TypeName:${id}`, {
                    ...item,
                    key: 'updated_value',
                });
                
                // update query components to reflect updated value
                client.cache.broadcastWatches();
            }}
        >
            update item
        </button>
    )}
</ApolloConsumer>

This approach probably has pitfalls that I’m not aware of, so use only if you must

0reactions
jedwards1211commented, Apr 25, 2019

Of if you use a custom dataIdFromObject function you can use that to get the cache keys

Read more comments on GitHub >

github_iconTop Results From Across the Web

Reading and writing data to the cache - Apollo GraphQL Docs
If a type in your cache uses a custom cache ID (or even if it doesn't), you can use the cache.identify method to...
Read more >
Updating the Store – Angular - GraphQL Code Generator
import { InMemoryCache } from 'apollo-cache-inmemory'; // If your database has unique IDs across all types of objects, you can use // a...
Read more >
GraphQL/Apollo Client: get data from cache if a query is a ...
const cache = new InMemoryCache({ cacheRedirects: { Query: { post: (_, {id}, {getCacheKey}) => getCacheKey({__typename: "Post", id}), }, }, });.
Read more >
Global Object Identification - GraphQL
The id field can be extracted out of the response safely, and can be stored for re-use via caching and refetching. Clients can...
Read more >
Tips and Tricks for working with Apollo Cache - Medium
When you make any graphQL query, by default Apollo caches the ... by combining its id or _id properties with the __typename defined...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found