react-native - 无法连接到 GraphQL API 服务器
问题描述
我正在运行一个在线 GraphQL API,当我尝试邮递员例如发出请求时,它似乎工作正常,但总是失败,使用 Apollo Link React-native,返回错误:TypeError:网络请求失败。
const cache = new InMemoryCache();
const token = this.props.account.accessToken || "";
const httpLink = createHttpLink({
uri: config.graphql_host,
credentials: "same-origin",
});
const websocket = new WebSocketLink({
uri: `ws://${config.ws_host}`,
options: {
reconnect: true,
connectionParams: { authorization: `Bearer ${token}` },
},
});
const middleware = new ApolloLink((operation, forward) => {
operation.setContext({ headers: { authorization: `Bearer ${token}` } });
return forward(operation);
});
const splitlink = split(
({ query }) => {
const definition = getMainDefinition(query);
return (
definition.kind === "OperationDefinition" &&
definition.operation === "subscription"
);
},
websocket,
httpLink
);
const client = new ApolloClient({
connectToDevTools: true,
link: concat(middleware, splitlink),
cache: cache,
defaultOptions: {
watchQuery: { fetchPolicy: "cache-and-network", errorPolicy: "all" },
query: { fetchPolicy: "cache-and-network", errorPolicy: "all" },
},
mutate: { errorPolicy: "all" },
});
包.json:
"@apollo/client": "^3.1.3",
"@apollo/react-hooks": "^3.1.5",
"apollo-link-http": "^1.5.17",
"apollo-cache-inmemory": "^1.6.6",
我得到如下错误:
Error: Network error: Network request failed
at new ApolloError (bundle.umd.js:92)
at Object.error (bundle.umd.js:1330)
at notifySubscription (Observable.js:140)
at onNotify (Observable.js:179)
at SubscriptionObserver.error (Observable.js:240)
at bundle.umd.js:1107
at Set.forEach (<anonymous>)
at Object.error (bundle.umd.js:1106)
at notifySubscription (Observable.js:140)
at onNotify (Observable.js:179)
解决方案
我找到了解决方案,服务器上安装的 SSL 无效,所以我不得不使用 Free Let's Encrypt SSL 并完美运行。事实上,该请求不断被拒绝。
推荐阅读
- javascript - 进行缓存的 Ajax 调用时如何避免竞争条件
- python-3.x - Python解析dict中的值
- amazon-web-services - 从 AWS Lightsail 运行 jupyter 笔记本时出现问题
- python - 获取 Keras 模型输出 wrt 输入的梯度,但最后一层是 SVM
- python - 使用查询集将过滤器应用于嵌套的反向外键关系
- linux - 在 Linux 内核中,task_struct 是在哪里初始化的?
- java - 如何使用 MSAL4J 获取守护程序的令牌?
- java - 设置 TextView 颜色的最有效方法
- javascript - 如何使用 observables 来更新上下文提供者以用于提供者组件(React/Firebase)?
- bash - 为什么从 shell 脚本创建 conda 环境会导致脚本提前终止,我该如何解决这个问题?