Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/ssh/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 我可以在apollo客户端的错误链接中调用graphql变异吗_Javascript_Reactjs_Graphql - Fatal编程技术网

Javascript 我可以在apollo客户端的错误链接中调用graphql变异吗

Javascript 我可以在apollo客户端的错误链接中调用graphql变异吗,javascript,reactjs,graphql,Javascript,Reactjs,Graphql,嗨,这是我的阿波罗客户的一部分 const errorLink = onError(({ graphQLErrors, networkError, operation }) => { if(graphQLErrors) { graphQLErrors.forEach(({ message, location, path }) => { if (message === 'Pleage Re-Sign In') { cookies.remove('

嗨,这是我的阿波罗客户的一部分

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}
    `)
  }
})
我想知道我是否可以调用服务器的一个变种,在我在后端创建的数据库中创建一个日志。我可以在应用程序中调用这里的变异。通常我会使用Apollo React钩子中的“UseVariation”,但这只能在React组件中调用。我已经通过将用户移动到另一个页面来实现了这一点,例如:“localhost:3000/test”,在该组件中,我尝试使用useEffect并使用空数组的第二个参数调用useEffect中的变异,以便它调用一次,然后将用户重定向到我想要的位置。但这并不好。是否有其他方法可以做到这一点,这是最佳实践


sry代表蹩脚英语:)

是的,Apollo React挂钩只能在React组件内部调用

尝试使用阿波罗助推器

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函数