GithubHelp home page GithubHelp logo

No longer works about apollo-refetch-queries HOT 6 OPEN

TSMMark avatar TSMMark commented on September 26, 2024
No longer works

from apollo-refetch-queries.

Comments (6)

asselstine avatar asselstine commented on September 26, 2024 1

Thanks @TSMMark!

It's funny, I think we actually patched this in our application too. Hopefully one of us can get around to this soon :)

from apollo-refetch-queries.

adamtysonsmith avatar adamtysonsmith commented on September 26, 2024 1

Not sure if this helps but I hacked together a hook pretty quickly, it does not handle variables yet though

export const useRefetchByQueryNames = () => {
  const client = useApolloClient()
  const queries: Map<string, any> | undefined = R.path(
    ['queryManager', 'queries'],
    client
  )

  return (queryNames: string[]) => {
    if (queries) {
      let refetchFns = []

      queries.forEach(q => {
        const name = R.path(['observableQuery', 'queryName'], q)
        if (name && queryNames.includes(name as string)) {
          // @ts-ignore
          refetchFns.push(q.observableQuery.refetch.bind(q.observableQuery))
        }
      })

      refetchFns.forEach((fn: Function) => {
        fn()
      })
    }
  }
}

from apollo-refetch-queries.

amannn avatar amannn commented on September 26, 2024 1

Yep, that's true, but it's the wanted behaviour in my case.

If you're looking for only invalidation without refetching, you could try this:

const client = useApolloClient();

function invalidateCache() {
  client.queryManager.queries.forEach((query, queryId) => {
    client.queryManager.invalidate(queryId);
  });
}

No idea if that works properly, but it uses some things I found in the QueryManager class. Maybe it's helpful to you!

from apollo-refetch-queries.

TSMMark avatar TSMMark commented on September 26, 2024

AFAICT queryIdsByName is no longer exposed by Apollo. So I reimplemented queryIdsByName as a function. This fixes the internals of this library's refetchQueries functions, while reducing the amount of code changes necessary in the other internal functions of the library.

At @Vydia, we never actually looped back and migrated our application code to use this package, so I'm working from my original issue comment code, but this is working for us now:

export const getObservableQueriesByName = (client: ApolloClient, queryName: string): Array<Object> => {
  const queryIdsByName = getQueryIdsByName(client)
  const { queryManager: { queries } } = client
  const queryIds = queryIdsByName[queryName] || []
  return map(queryIds, (queryId: string) => queries.get(queryId).observableQuery)
}

export const getAllObservableQueries = (client: ApolloClient): Array<Object> => {
  const queryIdsByName = getQueryIdsByName(client)
  const { queryManager: { queries } } = client
  const queryIds = flatten(values(queryIdsByName))
  return map(queryIds, (queryId: string) => queries.get(queryId).observableQuery)
}

export const getQueryIdsByName = (client: ApolloClient): $Shape<{| [string]: string[] |}> => {
  const { queryManager: { queries } } = client
  const queryIdsByName = {}

  // `queries` contains a Map instance. I'm not sure the best way to transform a Map - is there a better way to do this loop?
  queries.forEach((value, key) => {
    (queryIdsByName[value.observableQuery.queryName] = (queryIdsByName[value.observableQuery.queryName] || [])).push(key)
  })

  return queryIdsByName
}

I'll try to find some time to submit a PR to this package, and to migrate our application codebase at @Vydia to use this package.

from apollo-refetch-queries.

amannn avatar amannn commented on September 26, 2024

@adamtysonsmith If I'm not mistaken, it does handle variables.

Previous variables are reused if you don't provide new ones:

https://github.com/apollographql/apollo-client/blob/d5d313846308b3c97330a6b1bd4e06aed73fd894/packages/apollo-client/src/core/ObservableQuery.ts#L315-L321

I made a function that invalidates all currently active queries:

const client = useApolloClient();

function invalidateCache() {
  client.queryManager.queries.forEach(query => {
    if (!query.observableQuery) return;
    query.observableQuery.refetch();
  });
}

from apollo-refetch-queries.

TSMMark avatar TSMMark commented on September 26, 2024

@amannn That looks cool but that could be a very expensive operation as it looks like it actually refetches all the queries, not only invalidating them right? Do you know of a way to ONLY invalidate without refetching? And let queries hit the network later, on demand, as needed?

from apollo-refetch-queries.

Related Issues (1)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.