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.

Nested keys required?

See original GitHub issue

Hello, with React query i naturally have this scenario:

-list of customers by account filtered by order by

how i map this to cache keys is:

`function customersByAccountId(accountId: number) { return [“customerListByAccountId”, accountId]; }

function customersByAccountIdFiltered(accountId: number, queryFilter: QueryFilter) { return […customersByAccountId(accountId), “list”, { queryFilter }]; }`

This way, when i edit an attribute for a customer, i can: update the in memory query for customersByAccountIdFiltered (which will change the attribute on the current screen, without reload)

and invalidate customersByAccountId (with refetchActive: false,) which will invalidate all other cached filtered entries.

How do i map this with query-key-factory?

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:11 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
kimbubacommented, Sep 16, 2022

np,

here is another example: I have a list of accounts and each account has customers i want to do different lists per customers, as they are calling different endpoints. I could put everything as in a filter for this simple example but i may not want to.

API:

- customersListByAccountInvited(1, "desc");
- customersListByAccountNotInvited(2, "asc");

The first gives the list of all customers non invited ordered by desc. The second gives the list of all customers non invited ordered by asc.

This would naturally map as: ["customers",1,"invited", {sort:"desc"}] And: ["customers",1,"non_invited", {sort:"asc"}]

With 1, the accountId

Now say i’m adding a customer to the first cache. I would setQuery on["customers",1,"invited", {sort:"desc"}]and add a user “John” I now need to invalidate the following queryClient.invalidateQueries(["customers",1], { refetchActive: false }); This way:

  • The current list has updated data without refetching
  • The second list is invalidated and will be reloaded

Is there a way to map this?

Something like this would not give me what i want to accomplish:

customers: {
    listInvited: (accountId: number, sort: {"asc"|"desc"}) => [accountId, { sort }],
    listNonInvited: (accountId: number, sort: {"asc"|"desc"}) => [accountId, { sort }],
  },

As there would be no way to invalidate

queryClient.invalidateQueries(["customers",1], { refetchActive: false });

0reactions
lukemoralescommented, Oct 25, 2022

@kimbuba just letting you know, v1.0 has been released with support for nested keys: https://github.com/lukemorales/query-key-factory/releases/tag/v1.0.0

Read more comments on GitHub >

github_iconTop Results From Across the Web

Python | Add keys to nested dictionary
This method is used in cases where more than one keys need to be added to the nested dictionaries. The update function accepts...
Read more >
javascript - nested key property requirements in React
Keys help React identify which items have changed, are added, or are removed. Keys should be given to the elements inside the array...
Read more >
TypeScript Utility: keyof nested object
In this blog post, we will learn how to build a TypeScript util type, that exposes all the key paths of an object,...
Read more >
Python - Accessing Nested Dictionary Keys - YouTube
Working with .json data is a very common task, no matter if you're coming from the data science or the web development world....
Read more >
Python Nested Dictionary (With Examples)
We can shrink or grow nested dictionary as need. Like Dictionary, it also has key and value. Dictionary are accessed using key.
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