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.

updateQuery merging different data when changing query while fetchMore is not yet finished

See original GitHub issue

Hi, I’ve read https://github.com/apollographql/apollo-client/issues/2499 and I don’t know if it this issue I’m having is related or not.

I am having this issue where prev and result on updateQuery(prev, result) having different context on the data to merge. It happens when I changed query (changing search term) while fetchMore is not yet finished, resulting in items with different query being merged.

How to reproduce the issue: Case:

  1. I am currently on search result page with term pillow already have some data, and calling fetchMore.
  2. Before fetchMore is done, I quickly changed the term with iphone.
  3. The iphone search query data are merged in updateQuery with pillow fetchMore result.

Sorry, I am currently unable to provide the repository to reproduce this. I will provide it later if it’s necessary.

Intended outcome: I think the intended outcome is obvious, the pillow fetchMore result should be merged according to its search term (fetched earlier in step 1).

This is the code I’ve been using:

fetchMore: (nextPage = 0, listview, additional_params) => {
    return data.fetchMore({
      variables: {
        page: nextPage,
        per_page: listview ? LIST_VIEW_PER_PAGE : GRID_VIEW_PER_PAGE,
        additional_params,
      },
      updateQuery: (prev,  res) => {
        console.log('prev: ', prev); 
        console.log('res: ', res);
        if (!res.fetchMoreResult) {
          return prev;
        }

        const newData = res.fetchMoreResult.search_results_product;

        return {
          ...prev,
          search_results_product: {
            ...prev.search_results_product,
            total_data: newData.total_data,
            items: [...prev.search_results_product.items, ...newData.items],
          },
        };
      },
    });
  },

Actual outcome: The data from pillow fetchMore are merged in updateQuery with iphone, resulting in wrong search result displayed.

Weirdly, the res.variables in updateQuery(prev, res) showing that the query is for iphone instead of the actual query for fetchMore, which is pillow.

Version

  • apollo-client@2.1.0

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:14
  • Comments:11 (2 by maintainers)

github_iconTop GitHub Comments

5reactions
protyzecommented, Feb 9, 2018

I’m experiencing the same behaviour. When the variables for the query are changed while fetchMore is in flight, the results of “fetchMore” (for the old variables) will be merged with the results of the query with current variables.

Example: You have a list with 2 tabs (changing tabs will change the “category”-variable for the query). So fetchMore is in progess while having tab 1 selected (category 1). Now the users switches to tab 2 and later the fetchMore results for tab 1 will be appended to tab 2.

The updateQuery-Function should provide the “previosResults” for the initial variables so we can merge accordingly.

4reactions
glenn-p-ryancommented, Oct 7, 2019

Any update on the status of this issue?

Read more comments on GitHub >

github_iconTop Results From Across the Web

fetchMore not updating the data - Help - Apollo GraphQL
The trick is to make sure the type returned from the merge function is exactly the same as the type of the data...
Read more >
graphql - How to convert apollo client fetchMore updateQuery ...
I have tried adding the merge in many different places however it always seems to break the application. Any help on how to...
Read more >
Advanced Topics on Caching – Angular
fetchMore can be used to update the result of a query based on the data returned by another query. Most often, it is...
Read more >
Cancellable apollo-client queries - Openhood Experiments
updateQuery merging different data when changing query while fetchMore is not yet finished · Cancel fetchMore network requests. After searching ...
Read more >
React with Apollo and GraphQL Tutorial - Robin Wieruch
Start by using the not-yet-implemented Profile component in your App ... Now we'll learn to query data with GraphQL and Apollo Client.
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