urql exchange-auth 在使用 useQuery 从不同服务器获取时创建两个请求

问题描述

我正在尝试从另一个身份验证服务器获取用户,并且我正在使用 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,因为没有在客户端设置它,请求会正常执行。 createClient:

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 时,请求仅发送 1 次,但发送到第一个不是我需要的服务器。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)