0

我正在尝试从另一个身份验证服务器获取用户,并且我正在使用 useQuery:

const registerRef = useRef();
const [registerUser, registerMut] = useQuery({ query: RegisterDocument, pause: !registerRef.current, 
      variables: registerRef.current, context: useMemo(() => ({url: 'http://localhost:8056/graphql'}), [])});

但是 authExchange 正在执行该请求两次。我知道它来自 authExchange,因为没有在客户端上设置它,请求就会正常执行。创建客户端:

export const createClient = (ssrExchange) => ({
    url: 'http://localhost:8099/graphql',
    exchanges: [dedupExchange, cacheExchange, ssrExchange, authExchange({
        getAuth, addAuthToOperation}), fetchExchange]})

const getAuth = async ({ authState }) => {
    if (!authState) {
      const token = localStorage.getItem('token');
      const refreshToken = localStorage.getItem('refreshToken');
      if (token && refreshToken) {
        return { token, refreshToken };
      }
      return null;
    }
  
    return null;
};

const addAuthToOperation = ({ authState, operation }) => {
    if (!authState || !authState.token) {
      return operation;
    }
  
    const fetchOptions =
      typeof operation.context.fetchOptions === 'function'
        ? operation.context.fetchOptions()
        : operation.context.fetchOptions || {};
  
    return makeOperation(operation.kind, operation, {
      ...operation.context,
      fetchOptions: {
        ...fetchOptions,
        headers: {
          ...fetchOptions.headers,
          Authorization: authState.token,
        },
      },
    });
  };

当我在不创建 useQuery 的情况下使用 useMutation 时,请求只发送一次,但发送到第一台服务器,这不是我需要的。

4

0 回答 0