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

<Query fetchPolicy="cache-and-network" onCompleted={...}> doesn't fire #4921

Closed
sfcgeorge opened this issue Jun 5, 2019 · 4 comments
Closed

Comments

@sfcgeorge
Copy link

Intended outcome:

onCompleted callback should be run when a query completes.

Actual outcome:

The callback doesn't fire when using fetchPolicy="cache-and-network".

How to reproduce the issue:

<Query 
  query={someQuery}
  fetchPolicy="cache-and-network"
  onCompleted={data => console.log("you won't see this")}
>

^ Removing the fetch policy it fires correctly.

Versions

├─ apollo-cache-inmemory@1.6.1
├─ apollo-cache-persist@0.1.1
├─ apollo-cache@1.3.1
├─ apollo-client@2.6.1
├─ apollo-link-batch-http@1.2.11
├─ apollo-link-batch@1.1.12
├─ apollo-link-http-common@0.2.13
├─ apollo-link-http@1.5.14
├─ apollo-link@1.2.11
├─ apollo-utilities@1.3.1
├─ react-apollo@2.5.6
@jasonpaulos
Copy link
Contributor

@sfcgeorge Can you provide a small runnable reproduction that shows this issue? I'm not seeing the problem on my end.

@maapteh
Copy link

maapteh commented Jul 9, 2019

When using latest i get an TS error, it says: Type '"cache-and-network"' is not assignable to type '"cache-first" | "network-only" | "cache-only" | "no-cache" | "standby" | undefined'.ts(2345) in my Query component.

        "apollo-cache-inmemory": "^1.5.1",
        "apollo-client": "^2.5.1",
        "apollo-link": "^1.2.11",
        "apollo-link-batch-http": "^1.2.11",
        "apollo-link-error": "^1.1.10",
        "apollo-link-http": "^1.5.14",
        "apollo-link-logger": "^1.2.3",
        "apollo-link-schema": "^1.2.2",
        "apollo-link-state": "^0.4.2",
        "graphql": "^14.3.0",
        "react-apollo": "2.5.5",

I found the types here, somehow cache-and-network got lost and seems to be part of the watchQuery only?

It seems to be done in this commit cf069bc#diff-88b34d7add139a9c0e7d4c2b1944a8dc Related to PR #3130 personally i read the DOCS sometimes but it seems to be outdated even the comments in the code itself.

For the TS, we are using the onCompleted callback and that still works.

@jasonpaulos
Copy link
Contributor

@maapteh There is no mistake with the code. The commit you are referencing contains a link to this issue thread explaining why the changes were made. In short, the cache-and-network policy cannot be used for a one-off query call since that method can only return a single result. Instead, the watchQuery method should be used, which returns a stream of results. Hope this helps!

@hwillson
Copy link
Member

Please try a recent version of @apollo/client and let us know if this issue is still happening. Thanks!

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Feb 16, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

4 participants