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.

Is it possible to manually update the cache with a *nested* object from a subscription update?

See original GitHub issue

On Alec’s recommendation, I previously setup a subscription that updates when new records are created, the result were then added manually to the cache. Example: https://github.com/AlecAivazis/houdini/issues/94#issuecomment-859989593. Like in this example the query and its results were flat (single level) and the manual prepend to the cache worked well.

I have since updated my schema with nested queries. Something like this:

const { data } = subscription(
graphql`
	subscription NewRecipe($id: String!) {
		queryNewRecipe(id: $id) {
			id
			versions(first: 1) {
				title
				id
			}
			sourceRecipe {
				id
				domain
			}
		}
	}
`,
{
	id: $session.user.id
}
);

$: {
    cache.list('Active_Recipes').prepend(
	    {
		    id: { type: 'ID', keyRaw: 'id' },
		    versions: [
			    {
				    title: { type: 'String', keyRaw: 'title' },
				    id: { type: 'ID', keyRaw: 'id' },
			    }
		    ],
		    sourceRecipe: {
			    id: { type: 'ID', keyRaw: 'id' },
			    domain: { type: 'String', keyraw: 'domain' }
		    }
	    },
	    $data.queryNewRecipe
    );
}

Unfortunately, now the manual prepend to the cache is failing with TypeError: key is not iterable. When I inspect cache.js it looks like the code doesn’t really handle nested schemas like this, as the keys and values are showing as undefined inside the insertSubscribers method.

Assuming I understand the limitation correctly, how would you now recommend I manually update the cache with the incoming data to given its structure?

The full error:

Uncaught (in promise) TypeError: key is not iterable
    at Cache.evaluateKey (cache.js?t=1629053326512:713)
    at Cache.insertSubscribers (cache.js?t=1629053326512:665)
    at ListHandler.addToList (list.js:107)
    at ListHandler.prepend (list.js:33)
    at Object.$$self.$$.update (Subscriptions.svelte:26)
    at update (index.mjs?v=8f41bf68:1046)
    at flush (index.mjs?v=8f41bf68:1018)

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:22

github_iconTop GitHub Comments

1reaction
AlecAivaziscommented, Aug 20, 2021

fantastic this is super helpful. I’m going to go ahead and close the ticket since your issue has been resolved. Thanks again!

1reaction
AlecAivaziscommented, Aug 19, 2021

I believe @pixelmund is using 141 so I dont think there is some kind of incompatibility. I’ll try to find the time to update the example app this week

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to update nested Apollo Cache 3+ levels deep?
try cache.modify() (link to api) available from @apollo/client > v3. Easy process: identify object to modify; specify fields to modify(incl ...
Read more >
Advanced topics on caching in Apollo Client
This article describes special cases and considerations when using the Apollo Client cache. Bypassing the cache. Sometimes you shouldn't use the cache for...
Read more >
How to update nested Apollo Cache 3+ levels deep? - Reddit
My question: Is there a better way of updating deeply nested cache items? Or should I query differently so that the response is...
Read more >
Update cache of nested object after mutation when it's part of ...
If at some point, I'll decide to update a single passenger, the only way to update cache is to do that over updateQueries,...
Read more >
Cache Updates | urql Documentation
Cache updates are not possible outside updates 's functions. If we attempt to store the cache in a variable and call its methods...
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