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.

previousResult is undefined when using updateQuery in fetchMore

See original GitHub issue

Intended outcome: The previousResult variable should contain the previous data.

Actual outcome: The previousResult is undefined

How to reproduce the issue: I followed the tutorial on this page using the Relay-style cursor pagination. After switching everything to the latest stable apollo version the previousResult variable contains the data.

fetchMore({
          variables: {
            cursor: comments.pageInfo.endCursor
          },
          updateQuery: (previousResult, { fetchMoreResult }) => {
            const newEdges = fetchMoreResult.comments.edges;
            const pageInfo = fetchMoreResult.comments.pageInfo;

            // Apollo v3 Beta
           // previousResult is undefined

           // Apollo latest stable version
           // previousResult contains the data

            return newEdges.length
              ? {
                  // Put the new comments at the end of the list and update `pageInfo`
                  // so we have the new `endCursor` and `hasNextPage` values
                  comments: {
                    __typename: previousResult.comments.__typename,
                    edges: [...previousResult.comments.edges, ...newEdges],
                    pageInfo
                  }
                }
              : previousResult;
          }
        })

Versions v3.0.0-beta.20

Issue Analytics

  • State:open
  • Created 4 years ago
  • Reactions:3
  • Comments:17 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
toddtarsicommented, Mar 3, 2020

@Thomazella - Did you add a console logger in the area I recommended above? It sucks to add a logger there, but it helped me track down some issues that weren’t escaping in an obvious way. If using npm to install it, you’ll have to add it to the transpiled code, but it isn’t so bad. Also, feel free to ignore this if not using the InMemoryCache.

1reaction
nikhedoniacommented, Oct 21, 2020

I’m experiencing the same issue in @apollo/client@3.2.2 when using useQuery. Any updates on this issue? I’ve noticed also the error is not deterministic and seems to be related to network latency.

@soltysaleksandr workaround works but it would be nice if it would work out of the box

Read more comments on GitHub >

github_iconTop Results From Across the Web

When using fetchMore with useQuery previous data is undefined
I use fetchMore function for pagination with useQuery hook. Previous data is always undefined in the updateQuery in here.
Read more >
Pagination - Apollo GraphQL Docs
By default, fetchMore will use the original query , so we just pass in new variables. Once the new data is returned from...
Read more >
Reactive Query | Vue Apollo
Each key will be mapped with a '$' in the GraphQL document, for example foo ... Transform the previous result with new data...
Read more >
GraphQL Pagination with Apollo Client in React - Educative.io
fetchMore ; variables; updateQuery. You may also want to shield away from the conditional renderings in the FetchMore component, which happens because of...
Read more >
Vue Apollo "Typename" Is Undefined In Updatequery - ADocLib
The fetchMore method takes a map of variables to be sent with the new query.Here we're setting the offset to feed.length so that...
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