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

refetchQueries not working when using string array after mutation

Open ddkang opened this issue 2 years ago • 15 comments

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

ddkang avatar Apr 13 '22 23:04 ddkang

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!

jerelmiller avatar Nov 17 '22 22:11 jerelmiller

+1

saheem128 avatar Nov 24 '22 14:11 saheem128

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[bot] avatar Feb 17 '23 05:02 github-actions[bot]

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"],
});

lucidlive avatar Feb 22 '23 14:02 lucidlive

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[bot] avatar Mar 25 '23 00:03 github-actions[bot]

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 avatar Mar 25 '23 04:03 jerelmiller

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:

  • https://github.com/barbalex/apf2/blob/8b5bce3a1585478e6b5a5b7c83b1a66ba328580d/src/components/Projekte/Daten/Beobzuordnung/saveTpopIdToDb.js#L219
  • https://github.com/barbalex/apf2/blob/8b5bce3a1585478e6b5a5b7c83b1a66ba328580d/src/components/Projekte/Karte/layers/BeobNichtBeurteilt/Marker.jsx#L83
  • https://github.com/barbalex/apf2/blob/8b5bce3a1585478e6b5a5b7c83b1a66ba328580d/src/components/Projekte/Karte/layers/BeobZugeordnet/Marker.jsx#L91
  • https://github.com/barbalex/apf2/blob/8b5bce3a1585478e6b5a5b7c83b1a66ba328580d/src/modules/createNewTpopFromBeob.js#L244
  • https://github.com/barbalex/apf2/blob/8b5bce3a1585478e6b5a5b7c83b1a66ba328580d/src/modules/createNewPopFromBeob/index.js#L180

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.

barbalex avatar Apr 02 '23 14:04 barbalex

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

phryneas avatar Apr 25 '23 11:04 phryneas

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.

barbalex avatar May 02 '23 20:05 barbalex

it only seems to happen in dev mode

Probably it happens in production too but the warn is shown only on dev from https://github.com/apollographql/apollo-client/blob/4d192165965dbc8475d3d02a5d01dda7df03d790/src/core/QueryManager.ts#L867

mlazari avatar Jun 14 '23 11:06 mlazari

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: https://github.com/apollographql/apollo-client/blob/69563f157b54a0cd23958a609bdf9e016a1a25e1/src/core/QueryManager.ts#L1289-L1302
    • 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 https://github.com/apollographql/apollo-client/blob/69563f157b54a0cd23958a609bdf9e016a1a25e1/src/core/QueryManager.ts#L812-L841

beaucollins avatar Aug 08 '23 01:08 beaucollins

Is there a fix for this? Whats the workaround?

FutureKode avatar Feb 15 '24 20:02 FutureKode

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.

phryneas avatar Feb 16 '24 09:02 phryneas

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

saheem128 avatar Apr 12 '24 18:04 saheem128

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

anatooly avatar Aug 07 '24 09:08 anatooly