【问题标题】:Can I call a graphql mutation inside an Error Link in the apollo client [duplicate]我可以在 apollo 客户端的错误链接中调用 graphql 突变吗?
【发布时间】:2020-02-14 15:59:14
【问题描述】:

嗨,这是我的 Apollo 客户端的一部分

const errorLink = onError(({ graphQLErrors, networkError, operation }) => {
  if(graphQLErrors) {
    graphQLErrors.forEach(({ message, location, path }) => {
      if (message === 'Pleage Re-Sign In') {
        cookies.remove('userToken')
        cookies.remove('hshashes')
        MoveME('/')
      }
      if (message === 'Unathorized') {
        return MoveME('/')
      }
      if (message === 'Access Denied') {
        return MoveME('/dashboard/home')
      }
      console.log(`[GraphQL Error] Message: ${message}, location: ${location}, path: ${path}`)
    })
  }

  if(networkError){
    console.log(`
      [network error ${networkError.message}] Operation: ${operation.operationName}
    `)
  }
})

我想知道是否可以向服务器调用 Mutation 以在我在后端创建的数据库中记录日志。我可以在应用程序中调用突变吗?通常我会使用 Apollo-React-hooks 中的“useMutation”,但这只能在 React 组件中调用。我已经通过将用户移动到另一个页面来完成此操作,例如:“localhost:3000/test”在该组件中我尝试使用 useEffect 并使用空数组的第二个参数调用 useEffect 中的突变,以便它调用一次然后将用户重定向到我想要的位置。但这不好。有没有其他方法可以做到这一点,这是最佳做法?

英语不好请见谅 :)

【问题讨论】:

    标签: javascript reactjs graphql


    【解决方案1】:

    是的,Apollo-React-hooks 只能在 React 组件内部调用

    尝试使用 apollo-boost

    1.创建文件index.js

    import ApolloClient from 'apollo-boost';
    import * as constant from 'constant';
    
    const client = new ApolloClient({
      uri: `${constant.backendUrl}graphql`,
    });
    
    function graphqlQuery({ variables, query }) {
      return client.query({
        fetchPolicy: 'no-cache',
        variables, // your variables
        query, // your query schema
      });
    }
    
    function graphqlMutation({ variables = null, mutation }) {
      return client.mutate({
        variables,
        mutation,
      });
    }
    
    export { client as default, graphqlQuery, graphqlMutation };
    

    2.如何使用

    import { graphqlMutation } from 'index.js';
    import { gql } from 'apollo-boost';
    
    /**
     * Create user
     * @param {Object} data
     * @param {string} data.email
     * @param {string} data.firstName
     * @param {string} data.lastName
     * @param {string} data.gender
     * @param {number} data.height
     * @param {number} data.weight
     * @param {string} data.birthDate
     */
    function createUser(data) {
      const createUser = gql`
        mutation createUser($userInput: UserInput) {
          createUser(userInput: $userInput) {
            status
            message
          }
        }
      `;
      const variables = {
        userInput: data,
      };
      return graphqlMutation({ variables, mutation: createUser });
    }
    
    export { createUser };
    

    现在你可以在任何你想要的地方使用这个 createUser 函数

    【讨论】:

      猜你喜欢
      • 2017-09-14
      • 1970-01-01
      • 2021-10-06
      • 2019-02-02
      • 2018-05-21
      • 1970-01-01
      • 2019-11-02
      • 2021-10-12
      • 2018-09-03
      相关资源
      最近更新 更多