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

refetchQueries not working when using string array after mutation #9597

Open
ddkang opened this issue Apr 13, 2022 · 15 comments
Open

refetchQueries not working when using string array after mutation #9597

ddkang opened this issue Apr 13, 2022 · 15 comments
Labels
🔍 investigate Investigate further

Comments

@ddkang
Copy link

ddkang commented Apr 13, 2022

Intended outcome:
A query specified using a string identifier should be refetched when using refetchQueries.

Actual outcome:
The query is not refetched and old versions of the query data are used from the cache.

How to reproduce the issue:
One way to reproduce:

  • Using Hasura, create a table posts
  • Query 1: posts (fetches all posts)
  • Query 2: posts_by_pk (fetching a single post)
  • Mutation 1: update_posts_by_pk

Then, if mutation 1 has refetchQueries set to ['posts'] (or whatever the query is called), it won't actually refetch and returns the error:

Unknown query named "posts" requested in refetchQueries options.include array

Versions
System:
OS: Linux 5.4 Linux Mint 20 (Ulyana)
Binaries:
Node: 16.7.0 - ~/.nvm/versions/node/v16.7.0/bin/node
Yarn: 1.22.11 - ~/.nvm/versions/node/v16.7.0/bin/yarn
npm: 7.20.3 - ~/.nvm/versions/node/v16.7.0/bin/npm
Browsers:
Chrome: 100.0.4896.75
Firefox: 99.0
npmPackages:
@apollo/client: ^3.5.10 => 3.5.10
npmGlobalPackages:
apollo: 2.33.6

@jerelmiller
Copy link
Member

Hey @ddkang! Thanks for your patience 🙏 . Would you be willing to share a bit more info here or create a reproduction of the issue? What does your original query look like? I've used refetchQueries with strings in some personal projects with success. Any more context you can give would be greatly appreciated 🙏. Thanks!

@saheem128
Copy link

+1

@bignimbus bignimbus added 🏓 awaiting-contributor-response requires input from a contributor and removed 🏓 awaiting-contributor-response requires input from a contributor labels Jan 18, 2023
@github-actions
Copy link
Contributor

We're closing this issue now but feel free to ping the maintainers or open a new issue if you still need support. Thank you!

@github-actions github-actions bot closed this as not planned Won't fix, can't repro, duplicate, stale Feb 17, 2023
@lucidlive
Copy link

We're experiencing the same issue. If we pass the query with all variables it works. If we pass a string name for the query with no variables, it does not work and we're met with "Unknown query named ..."

This works

const [handlePublish] = useMutation(UPDATE_POST, {
    refetchQueries: [
      {
        query: GET_POSTS,
        variables: {
          orderBy: {},
          first: 50,
          last: null,
          after: null,
          filters: {
            published: true,
          },
        },
      }, 
    ],
  });

This does not work

const [handlePublish] = useMutation(UPDATE_POST, {
    refetchQueries: ["posts"],
});

@github-actions
Copy link
Contributor

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.
For general questions, we recommend using StackOverflow or our discord server.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Mar 25, 2023
@jerelmiller
Copy link
Member

This issue feels like it was closed and locked a bit prematurely, so I'll reopen for now.

@lucidlive appreciate the code snippet. Would you or @ddkang be willing to put together a small reproduction to show the issue? While your code snippet gives us a bit more of a clue, it would still be helpful to see what other things might be at play here. You can use our error template as a starting point.

I'll go ahead and reset the clock on this issue. If you can help us out with a reproduction, we'd be happy to take a look at it. Thanks!

@jerelmiller jerelmiller reopened this Mar 25, 2023
@apollographql apollographql unlocked this conversation Mar 25, 2023
@jerelmiller jerelmiller added 🏓 awaiting-contributor-response requires input from a contributor and removed 🏓 awaiting-contributor-response requires input from a contributor labels Mar 25, 2023
@barbalex
Copy link

barbalex commented Apr 2, 2023

I am suddenly running into a similar or same issue:

Unknown query named "BeobAssignLinesQuery" requested in refetchQueries options.include array

There are several additional mentions as a few queries are usually requeried together. But focusing on this one.

Query is here: https://github.com/barbalex/apf2/blob/8b5bce3a1585478e6b5a5b7c83b1a66ba328580d/src/components/Projekte/Karte/layers/BeobZugeordnetAssignPolylines/index.jsx

Refetches are here:

Thing is: This does not happen on the live version (yet). So it may be due to a recent change in @apollo/client. We updated yesterday to 3.7.11.

Edit:
I tried using v3.7.10 but same error.
These files have not been changed in at least two months. While everything worked fine. Until today.

Edit 2:
Could it be that apollo does not know the query if one of it's parents is lazy loaded and the refetch comes from a different component that is not in the same lazy loaded component tree?

Edit 3: It seems this happens in dev but not in production.

@github-actions github-actions bot removed the 🏓 awaiting-contributor-response requires input from a contributor label Apr 3, 2023
@phryneas
Copy link
Member

@barbalex I'm going to echo @jerelmiller's earlier comment - could you please try to create a minimal reproduction for this?

@barbalex
Copy link

barbalex commented May 2, 2023

I have absolutely no idea how to create a reproduction as this suddenly started happening in a complex app. Plus it only seems to happen in dev mode.

Sorry for not answering earlier - I was on vacation.

@mlazari
Copy link

mlazari commented Jun 14, 2023

it only seems to happen in dev mode

Probably it happens in production too but the warn is shown only on dev from

if (__DEV__ && queryNamesAndDocs.size) {

@beaucollins
Copy link
Contributor

beaucollins commented Aug 8, 2023

Plus it only seems to happen in dev mode.

We're finding this to happen when using <React.StrictMode>. Removing <React.StrictMode> makes my development environment behave the same way as production in respect to refetchQueries no longer working.

  • In development with <React.StrictMode>, the component is remounted which over-subscribes to the useQuery.
  • When useMutation(mutation, { refetechQueries: [...]) is called the QueryManager instance still has a whole bunch of queries in this.queries that in production without <React.StrictMode> are no longer there.
  • Once the mutation completes and it tries to get the list of queries to refetch the query is no longer there
    • attempting to populate includedQueriesById:
      const includedQueriesById = new Map<string, {
      oq: ObservableQuery<any>;
      lastDiff?: Cache.DiffResult<any>;
      diff?: Cache.DiffResult<any>;
      }>();
      if (include) {
      this.getObservableQueries(include).forEach((oq, queryId) => {
      includedQueriesById.set(queryId, {
      oq,
      lastDiff: this.getQuery(queryId).getDiff(),
      });
      });
      }
    • queryManager.queries does not contain the query in production due to the useQuery component being unmounted so there's no chance to match the query
      this.queries.forEach(({ observableQuery: oq, document }, queryId) => {
      if (oq) {
      if (include === "all") {
      queries.set(queryId, oq);
      return;
      }
      const {
      queryName,
      options: { fetchPolicy },
      } = oq;
      if (
      fetchPolicy === "standby" ||
      (include === "active" && !oq.hasObservers())
      ) {
      return;
      }
      if (
      include === "active" ||
      (queryName && queryNamesAndDocs.has(queryName)) ||
      (document && queryNamesAndDocs.has(document))
      ) {
      queries.set(queryId, oq);
      if (queryName) queryNamesAndDocs.set(queryName, true);
      if (document) queryNamesAndDocs.set(document, true);
      }
      }
      });

@FutureKode
Copy link

Is there a fix for this? Whats the workaround?

@phryneas
Copy link
Member

I believe the exact behaviour that @beaucollins described above should already have been solved with Apollo Client somewhere around version 3.8, as we changed the subscription timing in useQuery to account for StrictMode.

If that problem is still occuring, we really need to see some minimal viable reproduction, or a Replay recording of it happening.

@saheem128
Copy link

It appears that the query needs to be mounted in order for this to work.

@anatooly
Copy link

anatooly commented Aug 7, 2024

All my RN app on this warnings :-)
Unknown query named "***" requested in refetchQueries options.include array

And I agree that issue on mount main query.
If query on Screen2 & mutation on Screen1

refetchQueries: ["posts"] - this always warning need change to this:
refetchQueries: [{ query: GET_POST_QUERY }] - all fine

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🔍 investigate Investigate further
Projects
None yet
Development

No branches or pull requests