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

fetchMore sets networkStatus to 1 #6907

Closed
gergof opened this issue Aug 26, 2020 · 5 comments
Closed

fetchMore sets networkStatus to 1 #6907

gergof opened this issue Aug 26, 2020 · 5 comments

Comments

@gergof
Copy link

gergof commented Aug 26, 2020

Intended outcome:

When calling the fetchMore function of a query the networkStatus should be set to 3 and remain 3 for the entire duration of the request as in Apollo Client 2.

Actual outcome:

After I call fetchMore on a query that has { notifyOnNetworkStatusChange: true, fetchPolicy: 'cache-and-network' } as its parameters the networkStatus is set to 3 for a moment of time, but then it becomes 1:

[Wed Aug 26 2020 14:10:09.560]    7
[Wed Aug 26 2020 14:10:10.640]    3
[Wed Aug 26 2020 14:10:10.879]    1
[Wed Aug 26 2020 14:10:10.886]    1
[Wed Aug 26 2020 14:10:11.259]    7

How to reproduce the issue:

I couldn't reproduce the issue in the sandbox, but this is my setup: https://codesandbox.io/s/happy-bhabha-rjyt0.

I'm using react-native with the following code:

const { data, refetch, fetchMore, networkStatus } = useQuery(
	query,
	{
		variables,
		notifyOnNetworkStatusChange: true,
		fetchPolicy: 'cache-and-network'
	}
)

// .....

const onEndReached = useCallback(() => fetchMore({
	variables: {
		after: lastId
		count: 30
	}
}), [fetchMore])

Versions

System:
OS: Linux 4.19 Debian GNU/Linux 10 (buster) 10 (buster)
Binaries:
Node: 14.8.0 - /usr/bin/node
npm: 6.14.8 - /usr/bin/npm
Browsers:
Firefox: 68.11.0esr
npmPackages:
@apollo/client: ^3.1.3 => 3.1.3
apollo-cache-persist: ^0.1.1 => 0.1.1

@DanielMarkiel
Copy link

@gergof In my case, useQuery is actually making a second request when networkStatus changes to 1 with the default variables from the initial request.
Do you observe similar behavior?

@apollo/client: 3.1.3

@gergof
Copy link
Author

gergof commented Aug 27, 2020

@DanielMarkiel Yes, it does the same thing for me as well:

# Loading the top of the list
[GQL] [Thu, 27 Aug 2020 08:14:16 GMT]  Projects  -  {"count":20}
# Scrolling down to load more items
[GQL] [Thu, 27 Aug 2020 08:14:31 GMT]  Projects  -  {"count":20,"after":"42"}
[GQL] [Thu, 27 Aug 2020 08:14:32 GMT]  Projects  -  {"count":20}

@Alex153
Copy link

Alex153 commented Aug 28, 2020

They made a change on the behavior of the fetch policies cache-and-network and network-only. I think when the cache of the query changes (using fetchMore for example) it automatically refetches unless you specify nextFetchPolicy option.

Have you tried using nextFetchPolicy: 'cache-first' ?

The doc is not up to date yet but there are some comments on issues like #6760 (comment), #6819 (comment)...

I think we might soon configure it globally if that's the kind of behavior you prefer : #6833 (comment)

@DanielMarkiel
Copy link

@Alex153 Thanks for explanation! You've saved my day.
I've set nextFetchPolicy: 'cache-first' in defaultOptions and it looks that this line restored the behavior I wanted to achieve.

@gergof
Copy link
Author

gergof commented Sep 1, 2020

Thank you @Alex153. This solves the problem.

@gergof gergof closed this as completed Sep 1, 2020
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Feb 15, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants