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.

`writeFragment` writes to cache, but query doesn't read from it

See original GitHub issue

Hi everyone, I’ve been struggling with this one for a few days, though one ! I would like to know if this is expected behaviour or if there is something I’m missing here, or even a bug.

From within the update method of a mutation in a component, let’s call it <ComponentA />, I update the cache with writeFragment`.

                    store.writeFragment({
                      id: this.props.id,
                      fragment: SongData,
                      data: {
                        __typename: 'SongData',
                        song_id: songId,
                        length,
                      },
                    });

And SomeFragment looks like this :

                fragment SongData on Song {
                  __typename
                  song_id
                  length
                }

I then need this data from ComponentB /> and ComponentC />. The first has a query matching exactly this, so I can call writeQuery() instead and it works fine, data gets in the cache and I can access from this component using either cache-first, cache-and-network or cache-only fetch policy.

However, ComponentC /> cannot seem to get the data from the cache and this has been confirmed by using cache-first.

The thing is the query is quite large, it’s not just fragment SongData on Song. It also gets many other fields, amongst which a songs {} field containing a list of Song items.

I’ve tried everything and can’t seem to update one particular item in cache. I’ve also tried using a very specific dataidFromObject including typename method just in case I wasn’t targeting id properly, without success.

What are my solutions here ? ComponentA doesn’t have access to the full data that would allow to run writeQuery instead and write the whole query I’m using in ComponentC unfortunately.

On a higher level, what I’m trying to do here is to :

  1. Get this specific item only from cache initially
  2. Once I get the response from the GraphQL server, re-render with the whole list of items (and the cache-and-network policy, and cache-only for testing).

I usually wouldn’t post for issues but after a failing attempt in SO and a full week of reading related issues, I think this has its place here !

For reference, I am using https://github.com/trojanowski/react-apollo-hooks but this shouldn’t matter as it is just a wrapper

Thanks in advance for everyone helping 😃

Issue Analytics

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

github_iconTop GitHub Comments

3reactions
luciscommented, Apr 11, 2019

Try using these methods directly in the ’ client’ instance. I’ve had the same problem using cache.writeData, and when switched to client.writeData it worked. Looked it up on the code, and using the latter, Apollo will check if any query is subscribed on that data.

2reactions
nafisholehcommented, Jun 23, 2019

Thanks a lot, @luciannojunior your hints lead me to end my suffering

Read more comments on GitHub >

github_iconTop Results From Across the Web

ReadQuery does not get updates made by WriteFragment
Hi, On the browser, I am trying to use Apollo cache like a state container; would like to write some state to a...
Read more >
Apollo Client: writeFragment or readFragment when updating ...
This is counterintuitive as the name writeFragment implies that it's for writing to the cache and not reading from it, but it seems...
Read more >
Reading and writing data to the cache - Client (React)
Apollo Client provides the following methods for reading and writing data to the cache: readQuery and readFragment; writeQuery and writeFragment ...
Read more >
Reading and Writing Data to the Cache - Ferry Graphql
If your cache doesn't contain all of the data necessary to fulfill a specified query, readQuery returns null. It never attempts to fetch...
Read more >
Errors | urql Documentation
Invalid field: The field ??? does not exist on ??? , but the GraphQL ... writeFragment or cache.link with data that the cache...
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