Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Configuring the cache key of related data #35

Open
zaidjan-devrev opened this issue Dec 6, 2024 · 3 comments
Open

Configuring the cache key of related data #35

zaidjan-devrev opened this issue Dec 6, 2024 · 3 comments
Labels
question Further information is requested

Comments

@zaidjan-devrev
Copy link

I have a API that for some fields returns a summary object for that field. I have another API for which some react query key has already been configured. Can I configure react query in such a way that the react query cache key used for the response of the API and the summary object are the same. Since they represent the same object

@klis87
Copy link
Owner

klis87 commented Dec 6, 2024

Please provide some example, it will be easier for me to understand the issue. Thanks!

@klis87 klis87 added the question Further information is requested label Dec 6, 2024
@zaidjan-devrev
Copy link
Author

zaidjan-devrev commented Dec 6, 2024

I have an API that returns the following data, say, issue.get(id)

{ assignee: { details: { id: 'profile_202', level: 'standard' }, id: 'user_101' }, components: [{ id: 'comp_303', subcomponent: { id: 'sub_404', status: 'active' } }], id: 'obj_123', parent: { id: 'parent_456', metadata: { id: 'meta_789', reference: 'REF-001' }, type: 'container' }, status: { current: { id: 'state_606', name: 'in_progress' }, id: 'status_505' }, type: 'primary', };

Now this API data is built up of multiple objects, for example, assignees, components, status, parent, etc.
For each of these objects, I also have APIs the fetch the correct data. something like assignee.get(id)

For the issue.get(id), the react cache key is [issues, id], and for the assignee.get(id), the react cache key is [assignees, id].

Now when I normlize this data, I want normy to cache the assignee data to the react cache key for the assignee object, which is [assignees, id].

Is this behaviour configurable?

@klis87
Copy link
Owner

klis87 commented Dec 6, 2024

Now when I normalize this data, I want normy to cache the assignee data to the react cache key for the assignee object

Normy does not affect at all the way how react query cache data per key. Normy has own cache. React query data is assumed to be denormalized, and normy normalizes it.

I suspect that your use case is hard to translate to generic things which could be added to this library. Normy is very flexible, albeit it is opinionated for what it does, and all it does is adding normalizing feature to data fetching libraries.

Your use case seems different, as you have API which returns normalized data already. I am not sure this will help you, but you could:

  1. use select option in useQuery, for example you could denormalize data there, for instance by using https://github.com/klis87/normy/tree/master/packages/normy-react-query#getObjectById-and-getQueryFragment-arrow_up
  2. you can turn off normalization per query by meta.normalize, maybe you should do this for queries which return normalized data? as it does not make sense to normalize them
  3. if this is not possible to achieve what you need, perhaps you need to build your own abstraction, perhaps using normy primitives. For example you could check https://github.com/klis87/normy/blob/master/packages/normy/src/denormalize.spec.ts and https://github.com/klis87/normy/blob/master/packages/normy/src/normalize.spec.ts to understand how actually normy works under the hood. you can see that on those primitives, the core for data fetching case is built - https://github.com/klis87/normy/blob/master/packages/normy/src/create-normalizer.spec.ts . But really, it is all about those 2 normalize and denormalize functions, the rest is just a glue.

If you need some help to understand some parts, pls let me know and I will try to help.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants