Jedi Schmedi
Jedi Schmedi

Reputation: 818

useLazyQuery not firing in Promise all

I'm trying to refetch the data in from a apollo server but cant get it to work. I do two mutaions and push them into an array. I then want to, when they finsih, get the data and re-populate a graph.

    Promise.all(promises)
    .then(result => {
      console.log(result)
      console.log(id)
      return getGraph({
        variables: {
          id: id
        },
        fetchPolicy: 'network-only'
      });
    })
    .catch();

I can see that it logs the result and correct ID. Also changed to network only so it wont look in cache. Any suggestions on what might be wrong?

Br

Upvotes: 1

Views: 5388

Answers (1)

skyboyer
skyboyer

Reputation: 23705

I assume getGraph is fetcher - callback returned by useLazyQuery.

Unfortunately it does not return Promise but returns void(according to Apollo docs). There is existing issue in Github on that but I'm not sure if that ever will be implemented.

See, for hooks it's much more natural to return some data on each render. At one render you have some flag isLoading = true and next render you are getting isLoading = false and some non-empty data. If you introduce Promise that can be resolved or rejected regardless of component's rendering cycle things become really messy.

If you don't like that approach(or really need to sync some query with anoter one) as a workaround you can run query on client accessed through ApolloConsumer.

[UPD] as @Jedi Schmedi wrote in comments, fetcher actually returns Promise but in more recent version(^3.1.2) of apollo. So it could be a workaround although I'm not sure if that API is not stable and can be changed OR docs are misleading.

Upvotes: 2

Related Questions