首页 > 解决方案 > 无法连接到 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)

标签: react-nativegraphqlapollo

解决方案


我找到了解决方案,服务器上安装的 SSL 无效,所以我不得不使用 Free Let's Encrypt SSL 并完美运行。事实上,该请求不断被拒绝。


推荐阅读