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.

When creating a record directly against local cache, Apollo behaves unexpectedly if one of the optional fields is missing

See original GitHub issue

Intended outcome: To create a record locally which will be synced at a later time to the server. The goal is to read from cache, append to the cached array and save directly to cache.

Actual outcome: When creating the record locally as described here Apollo “empties out” my local query cache.

This issue only happens if I create a record locally which misses any of the fields specified by the original query.

How to reproduce the issue:

Use a query such as this to fetch data from server and into local cache:

  query fetchAssetsQuery {
    assets {
        contentState
        id
        createdAt
    }

Now create a new record locally which is missing the createdAt prop as such:

      let assets = client.readQuery({
        query: fetchAssetsQuery
      }).assets;

    client.writeQuery({
      query: fetchAssetsQuery,
      data: {
        assets: [{
          id: id,
        }, ...assets]
      }
    });

Finally, sync to the server as such:

      return mutate({
        variables: assetListInput: {
               assets: assets
        },
        updateQueries: {
          getUserAssets: (previousQueryResult, { mutationResult }) => {
            let currentAssets = previousQueryResult.assets;
            return { assets: currentAssets };
          }
        }
      })

This will result in the query turning into an empty array. To be more specific, updateQueries does not get executed here.

However, If I create the local record with the missing createdAt prop, this issue is resolved.

There are certain props that I’d like the server to create such as createdAt.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:8 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
Poincarecommented, Apr 20, 2017

If I’m understanding correctly, this is intended behavior since if you update the store with a result that’s missing createdAt, it’s going to break the existing query observer on the page associated with the query containing the createdAt field.

You could just set your createdAt field to a null-like value initially and then insert it once it hits the server and comes back.

0reactions
yattiascommented, Apr 20, 2017

Got it. Cool. Just wanted to make sure it’s being tracked.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Reading and writing data to the cache - Apollo GraphQL Docs
If the cache is missing data for any of the query's fields, readQuery returns null . It does not attempt to fetch data...
Read more >
@apollo/client | Yarn - Package Manager
Apollo Client is a fully-featured caching GraphQL client with integrations for ... It allows you to easily build UI components that fetch data...
Read more >
How to invalidate cached data in Apollo and handle updating ...
Thankfully there's a very simple workaround that should work in most cases. When we have access to the cache object we can call...
Read more >
Understanding Client-Side GraphQl With Apollo-Client In ...
Just by setting up Apollo Client in your app, you get an intelligent cache ... So, if we created some types with some...
Read more >
21.1.0 API Reference - hapi.dev
plugins is an object where each key is a plugin name and the value is the configuration. Note the difference between server.settings.plugins which...
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