【问题标题】:creatHttpLink from asyncStorage, Apollo, React Native来自 asyncStorage、Apollo、React Native 的 creatHttpLink
【发布时间】:2021-02-05 22:15:47
【问题描述】:

"react-apollo": "3.1.5",

“react-native”:“https://github.com/expo/react-native/archive/sdk-38.0.2.tar.gz”

“世博”:“38.0.0”

我正在尝试从本地 asyncStorage 获取我的 url(主要是因为我的 url 在登录后从两个 url 中挑选并分配给 asyncStorage)。 这是我已经尝试过的,但我一直得到未定义的数据

const asyncHttpLink = setContext(async (_, {headers, ...context}) => {
  const url = await getUrlFromAsyncStorage();
  return createHttpLink({
    uri: url ? url : '',
    ...context,
  });
});

然后我将它分配到我的 Appolo 中

const client = new ApolloClient({
  ... other property as cache, type etc. ...
  link: ApolloLink.from([
    ... other links ...
    asyncHttpLink,
  ]),
});

响应时,我得到所有属性,除了始终未定义的数据

[
  "variables",
  "refetch",
  ... other 7 ...,
  "error",
  "called",
  "data", <== always undefined
  "client",
]

但是如果我使用标准的 createHttpLink,一切都可以正常工作

const httpLink = createHttpLink({
  uri: desiredUrl,
});

有人追过同样的情况吗?

【问题讨论】:

    标签: javascript react-native apollo react-apollo


    【解决方案1】:

    我找到了解决办法:

    const customFetch = async (_: string, options: RequestInit) => {
      const url = await getUrlFromAsyncStorage();
      return url ? fetch(url, options) : fetch('');
    };
    
    const asyncHttpLink = createHttpLink({
      fetch: customFetch,
    });
    

    【讨论】:

      猜你喜欢
      • 2016-05-16
      • 2019-11-03
      • 1970-01-01
      • 2019-01-30
      • 1970-01-01
      • 2019-11-23
      • 1970-01-01
      • 2023-03-03
      • 1970-01-01
      相关资源
      最近更新 更多