【问题标题】:Is there a way to make mutation with no render return inside a function?有没有办法在函数内部进行没有渲染返回的突变?
【发布时间】:2019-07-06 15:10:18
【问题描述】:

我想从 apollo 客户端对 graphQL 后端中的某些内容进行变异,但是变异应该什么都不渲染,它只是后台工作,我想在构造函数中调用的函数中运行它,如下所示:

_callUpdateTokenMutation(token: String) {
    return <Mutation mutation={REFRESH_FCM_TOKEN} variables={token}>
      {updateFcmToken => (
        return true
      )}
    </Mutation>;
  }

我想知道是否有一种方法可以在没有 Mutation 标签的情况下运行它,例如:

client.mutation...

问题是客户端是在App.js中构建的,我想在组件中调用它,客户端是全局的吗?

提前致谢。

【问题讨论】:

    标签: react-native react-apollo apollo-client


    【解决方案1】:

    暂时,我最终使用 fetch 在服务器中运行突变,如下所示:

    async _callUpdateTokenMutation(token: String) {
        const authUser = await getToken();
    
        if (authUser) {
          fetch(`${global.endpoint}/graphql`, {
            method: "POST",
            headers: {
              Accept: "application/json",
              "Content-Type": "application/json",
              Authorization: authUser
            },
            body: JSON.stringify({
              query: `mutation { updateFcmToken(token: ${token}) { id } }`
            })
          })
            // .then(response => response.json())
            .then(responseJson => {
              console.log(responseJson);
            })
            .catch(error => {
              /* TODO:
               * Leer bien el mensaje desde el servidor y mostrar la alerta
               * que manda el servidor
               */
    
              console.log("HOME_FCM_ERROR", error);
            });
        }
      }
    

    但如果有人知道带有 Mutation 标签或客户端的东西的答案,那就太好了。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-02-01
      • 2021-04-16
      • 2021-06-10
      • 2016-08-01
      • 1970-01-01
      • 2018-04-05
      • 2020-03-10
      相关资源
      最近更新 更多