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.

RTK-Query updateQueryData callback is not called

See original GitHub issue

I am trying to manually add query data cache for each item in list, after loading whole list. It’s simple table view, after which I would like to have instant load on each details view for any of the items in the list… And I can’t make it work, seems like updateQueryData callback is not called.

Keep in mind that at the moment when getAll function gets called, there isn’t any getById queryes in cache! I am not sure if that’s issue here…

This is my api code for getAll(list view):

getAll: builder.query({
      query: () => `getAll`,
      providesTags: (result) =>
        result
          ? 
            [
              ...result.map(({ id }) => ({ type: "Fruits", id })),
              { type: "Fruits", id: "LIST" },
            ]
          : [{ type: "Fruits", id: "LIST" }],
      async onQueryStarted(_, { dispatch, queryFulfilled }) {
        try {
          const { data } = await queryFulfilled;
          //here I am getting my list...
          data.map((fruit) => {
            dispatch(
              fruitApi.util.updateQueryData("getById", fruit.id, (draft) => {
                console.log("hi mom");
                return Object.assign(draft, fruit);
              })
            );
          });
        } catch (e) {
          console.log(e);
        }
      },
    }),

And this is my getById(details view) api code:

getById: builder.query({
      query: (id) => `getById?id=${id}`,
      providesTags: (result) =>
        result
          ? [{ type: "Fruits", id: result.id }] : [],
    }),

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:13 (2 by maintainers)

github_iconTop GitHub Comments

4reactions
WingsDevelopmentcommented, Apr 17, 2022

Okay I got it, there was a problem with the type in this case after converting id to string in line before it started working… I guess adding this at least to documentation (correct me if I am wrong but I haven’t found anything about this), or loging some warning/error would be nice… 😄 But I’m glad I got it to work… Thanks for help!

1reaction
phryneascommented, Apr 16, 2022

updateQueryData only updates existing data for existing cache entries. It is not meant to add new cache entries.

Read more comments on GitHub >

github_iconTop Results From Across the Web

RTK Query's updateQueryData gives an incorrect draft
The problem is that when I'm trying to update the cache, using updateQueryData, after making an API call to update one of the...
Read more >
API Slices: Utilities
If no existing cache entry is found, the updateRecipe callback will not run. Example 1​. const patchCollection = dispatch(
Read more >
Manual Cache Updates
RTK Query > Usage > Manual Cache Updates: Updating cached data ... will not be called, and no patches or inversePatches will be...
Read more >
Redux Essentials, Part 8: RTK Query Advanced Patterns
It takes three arguments: the name of the endpoint to update, the same cache key value used to identify the specific cached data,...
Read more >
Usage With TypeScript
We strongly recommend using TypeScript 4.1+ with RTK Query for best results. If you encounter any problems with the types that are not...
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