react-apollo icon indicating copy to clipboard operation
react-apollo copied to clipboard

"Skip" property of useQuery hook has no effect

Open limekiln opened this issue 5 years ago • 42 comments

Intended outcome:

Using the skip property of useQuery, I expect the query not to be executed as long as skip === true.

Actual outcome:

No matter which value skip has, the query is always executed.

Version

System:

  • OS: Linux 5.0 Ubuntu 19.04 (Disco Dingo)
  • Binaries:
    • Node: 12.3.0 - /usr/local/bin/node
    • npm: 6.11.2 - /usr/local/bin/npm
  • Browsers:
    • Firefox: 69.0
  • npmPackages:
    • apollo-cache-inmemory: ^1.6.3 => 1.6.3
    • apollo-client: ^2.6.4 => 2.6.4
    • apollo-link-http: ^1.5.16 => 1.5.16
    • react-apollo: ^3.1.0 => 3.1.0

limekiln avatar Sep 13 '19 07:09 limekiln

Same issue here, when setting skip to true, the query is still executed

wooller avatar Sep 13 '19 14:09 wooller

Would one of you be able to provide a small runnable reproduction for this? Thanks!

hwillson avatar Sep 16 '19 01:09 hwillson

@hwillson I just tried a really simple version in codesandbox and it works fine. It could be related to the fact that im using an AWS-appsync client instead of an apollo-client. So im hesitant to post my exact code.

here is the sandbox of it working anyway https://codesandbox.io/embed/funny-field-1oeyk

maybe @limekiln can provide his set up if its more inline with a regular apollo setup

wooller avatar Sep 17 '19 14:09 wooller

+1, I am still having this issue. onCompleted doesn't fire as expected, but looking at my 'network' tab in the inspector, I'm still seeing a request to my GraphQL endpoint. @apollo/client 3.0.0-beta4.

jthistle avatar Dec 09 '19 17:12 jthistle

This is still an issue for me too, and I am using "@apollo/react-hooks": "^3.1.3"

All you need to do is use skip: true, and you will notice that it still executes. I've currently bypassed this by using pollInterval instead ex: pollInterval: true ? 0 : 5000

honglish avatar Dec 09 '19 21:12 honglish

I can confirm I have the same issue.

"@apollo/react-hooks": "^3.1.2" "react-apollo": "^2.5.5"

DennisBaekgaard avatar Dec 13 '19 08:12 DennisBaekgaard

Sorry for the late reply and not supplying a code example. For my current version setup, everything related to the skip property seems to work fine now. I will post the information again for anyone who is still facing this issue:

System:

  • OS: Linux 5.0 Ubuntu 19.04 (Disco Dingo)
  • Binaries:
    • Node: 13.5.0 - /usr/local/bin/node
    • npm: 6.13.4 - /usr/local/bin/npm
  • Browsers:
    • Firefox: 71.0
  • npmPackages:
    • @apollo/react-hooks: 3.1.3 => 3.1.3
    • apollo-cache-inmemory: ^1.6.5 => 1.6.5
    • apollo-client: ^2.6.8 => 2.6.8
    • apollo-link: ^1.2.13 => 1.2.13
    • apollo-link-error: ^1.1.12 => 1.1.12
    • apollo-link-http: ^1.5.16 => 1.5.16
    • react-apollo: 3.1.3 => 3.1.3

limekiln avatar Dec 19 '19 11:12 limekiln

Still having this issue with:

 "@apollo/react-common": "^3.1.3",
 "@apollo/react-components": "^3.1.3",
 "@apollo/react-hooks": "^3.1.3",
 "apollo-cache-inmemory": "^1.6.5",
 "apollo-client": "^2.6.8",
 "apollo-link": "^1.2.13",
 "apollo-link-error": "^1.1.12",
 "apollo-link-http": "^1.5.16",
 "react-apollo": "^3.1.3",

useQuery(QUERY, { variables, skip: true }) still runs the query.

tagamma avatar Feb 17 '20 14:02 tagamma

Hello, If the issue is still relevant, you might try to import useQuery from "react-apollo" rather than "@apollo/react-hooks". I can confirm that skipping works when imported from this package.

imeugenia avatar Mar 06 '20 13:03 imeugenia

@imeugenia which version of react-apollo? just tried 3.1.3 and didn't work

FacundoGFlores avatar Mar 18 '20 21:03 FacundoGFlores

@imeugenia @FacundoGFlores I'm also having this issue. Perhaps it's a bug in 3.1.3 since that's the same version I'm using.

Using "react-apollo" doesn't fix the issue for me either.

vjsingh avatar Mar 23 '20 15:03 vjsingh

The issue exists with fetchPolicy as cache-and-network. However, it works as expected with fetchPolicy as network-only

trdaya avatar Apr 03 '20 10:04 trdaya

@trdaya I'm actually trying to use it for a query with the default fetchPolicy, and it's not working. Version 3.1.3 as I mentioned earlier

vjsingh avatar Apr 04 '20 18:04 vjsingh

I'm on 3.1.5 and also experiencing the same problem. Have tried changing fetchPolicy from cache-and-network to network-only as well as making sure the import is from react-apollo.

nathan-omaorg avatar Apr 15 '20 20:04 nathan-omaorg

I'm bumping into this after client.resetStore(). The query in question skips successfully up until I do a store reset, then makes an invalid query because it's querying when it's supposed to be skipping. Are the rest of you doing any resetStore?

Curious if the query refetch logic that is run on store reset doesn't contain the necessary logic to obey the skip option?

DanSchoppe avatar May 01 '20 21:05 DanSchoppe

In QueryManager::reFetchObservableQueries I updated this conditional to squelch refetch for queries that have a truthy options.skip:

        if (
          fetchPolicy !== 'cache-only' &&
          (includeStandby || fetchPolicy !== 'standby') &&
          !get(observableQuery, ['options', 'skip']) // <----- Here
        ) {
          observableQueryPromises.push(observableQuery.refetch());
        }

This did fix the error I was bumping into. I couldn't figure out where this change should go (ObservableQuery::refetch?). But hopefully this is a clue as to what's going wrong.

DanSchoppe avatar May 01 '20 22:05 DanSchoppe

I'm facing the same error. I am also getting a network error 400: "variable $jwt of non-null type String! must not be null."
The whole idea of skipping a query is therefore invalid and breaking my code ...

derLalla avatar May 23 '20 19:05 derLalla

Yes, I'm hitting the same on @apollo/client: 3.0.0-beta.50 https://github.com/apollographql/apollo-client/issues/6190

The whole idea of skipping a query is therefore invalid and breaking my code ...

@derLalla Agree 😒

davydkov avatar May 29 '20 21:05 davydkov

I am facing the same issue on apollo-client: ^2.6.8

278kunal avatar May 31 '20 02:05 278kunal

same problem with @apollo/client: 3.0.0-beta.50

ziimakc avatar Jun 03 '20 12:06 ziimakc

Same problem with @apollo/react-hooks: "~3.1.5" and apollo-client: "~2.6.4". I initially tried to use useLazyQuery for my case but I could never get it to show the loading state; it was like the query had already been executed regardless. I get the loading state just fine with useQuery and I thought I'd try the skip option, but just saw this behavior that the query is executed anyway even if skip: true.

mdecurtins avatar Jun 04 '20 15:06 mdecurtins

If i set skip to true - it works, but if i use dynamic variable skip: !state.startDate it's not

ziimakc avatar Jun 14 '20 16:06 ziimakc

I'm using @apollo/client: ^3.0.0-rc-4.

Use cases:

  1. Pass true directly, and it correctly skips.
  2. Pass false directly, and it correctly triggers an HTTP request.
  3. Pass a variable with a true value, and it correctly skips.
  4. Pass a variable with a false value, and it correctly triggers an HTTP request.
  5. Pass a variable with false, and then change it to true: it should skip, but it doesn't.

Small runnable reproduction:

https://codesandbox.io/s/skip-does-not-work-rx2rj?file=/src/App.js

I have an array of currencies, and I only want to fetch the currencies which indexes are odd.

Expected

Only queries for odd indexes should trigger an HTTP request.

Actual

After setting the variable to false once, it ignores when I change it to true, so all queries trigger an HTTP request afterwards.

Screenshots

Running console.log(data, currency, skip) correctly shows what I'm expecting:

image

USD is even, so it skips, and data is undefined. BRL is odd, so it doesn't skip, and data is returned. EUR is even, so it skips, and data is undefined. CAD is odd, so it doesn't skip, and data is returned. AUD is even, so it skips, and data is undefined. GBP is odd, so it doesn't skip, and data is returned.

Unfortunately, Apollo triggers an HTTP request anyway, independent of skip:

image

image

image

image

image

The only one it respects is the first time I pass true (USD), but EUR and AUD it sends an HTTP request anyway, even if I'm telling it to skip, and even it returning me undefined for data.

buzinas avatar Jun 15 '20 20:06 buzinas

No news on this? We still have this issue in the latest RC9, are forced to use a pre-beta.50 version.

Taraluktus avatar Jun 26 '20 07:06 Taraluktus

It actually broke in 3.0.0-beta.46. Last working release is 3.0.0-beta.45!

chillyistkult avatar Jun 26 '20 15:06 chillyistkult

No updates on this? Is there a workaround? The following workaround suggested in #6190 doesn't work for us since we need to use refetch:

fetchPolicy: skip ? 'cache-only' : 'cache-first'

vjsingh avatar Jun 29 '20 20:06 vjsingh

Two other issues regarding skip are tracked in the apollo-client repo for the 3.0 milestone, e.g.: https://github.com/apollographql/apollo-client/issues/6342

Taraluktus avatar Jun 29 '20 20:06 Taraluktus

@Taraluktus Thanks for the response. If that's the case, this issue should be closed, and that other issue should encompass the client-side problem as well, and be referenced here as the canonical issue.

vjsingh avatar Jun 29 '20 20:06 vjsingh

For those like me...

According to @apollo/client tests, skip only works with polling, see here:

https://github.com/apollographql/apollo-client/blob/master/src/react/hooks/tests/useQuery.test.tsx#L514

So for me, where I was doing:

useQuery(QUERY, { variables, skip: true })

...the query was still made, and that is intended, skip, skips on poll.

zkwentz avatar Jul 07 '20 16:07 zkwentz

the query was still made, and that is intended, skip, skips on poll.

There are two wrong assumptions here:

  • If so, the docs should explain that, I shouldn't need to look at the tests code to assume how it's supposed to work.
  • Also, that's not really true, if you pass skip: true, it doesn't make the query. It only makes the query if you pass a dynamic value to skip, and between renders you change from false to true. So, it's definitely a bug.

This is a big issue and people logged it in many different repos, and the Apollo team should at least give us a response. I tried to debug their code to come up with a PR, but it's too deep, hard to grasp, so I can't help if there aren't any guidelines. For now, I'm just using a hack to work-around the bug.

buzinas avatar Jul 07 '20 16:07 buzinas