Update state with Apollo Client

大城市里の小女人 提交于 2019-12-10 22:36:34

问题


I am trying to use ApolloClient with Vuex but I stumbled upon a problem I can't resolve.

I call ApolloClient.query() function inside a vuex action but it seems like the function always returns a resolved promise from the first action call. So, after the function got fired once, no request goes to the server and I get the same result as when I load the page (although the actual data in database gets changes).

If I take a look at Apollo Dev Tools, there's also no changes to the Apollo state applied, which leads me to the belief that I missed some core concept of ApolloClient work. Could you give me an idea what should I do or read about?

Vuex action:

getTeams: ({ commit }) => new Promise(async (fulfill, reject) => {
   try {
     const response = await ApolloClient.query({ query: teamsQuery });
     const { teams } = response.data || {};
     commit(types.SET_TEAMS, teams);
     fulfil(teams);
   } catch (error) {
     reject(error);
   }
})

The query (I guess this is not helpful at all):

export const teamsQuery = gql`
   query Teams {
      teams {
        id
        name
      }
   }
`;

ApolloClient setup, this one is also as simple as it could be:

const link = new HttpLink({
    uri: 'http://localhost:8080/graphql',
});

const apolloClient = new ApolloClient({
   link,
   cache: new InMemoryCache(),
   connectToDevTools: true,
});

来源:https://stackoverflow.com/questions/48491160/update-state-with-apollo-client

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!