发送查询Apollo客户端时出现网络错误

时间:2019-06-03 05:08:54

标签: reactjs apollo apollo-client

我正在使用apollo客户端设置一个新的前端,但出现错误:

Error: Network error: link.request is not a function
    at new ApolloError (bundle.esm.js:63)
    at ObservableQuery../node_modules/apollo-client/bundle.esm.js.ObservableQuery.getCurrentResult (bundle.esm.js:159)
    at ObservableQuery../node_modules/apollo-client/bundle.esm.js.ObservableQuery.currentResult (bundle.esm.js:130)
    at Query._this.getQueryResult (react-apollo.esm.js:227)
    at finish (react-apollo.esm.js:348)
    at Query../node_modules/react-apollo/react-apollo.esm.js.Query.render (react-apollo.esm.js:352)
    at finishClassComponent (react-dom.development.js:14741)
    at updateClassComponent (react-dom.development.js:14696)
    at beginWork (react-dom.development.js:15644)
    at performUnitOfWork (react-dom.development.js:19312)

我已经尝试过查询和变异,并且都发送此错误。我也尝试设置身份验证中间件,但最终遇到了另一个错误

import React from "react";
import ReactDOM from "react-dom";

import App from "./App";
import "./styles.scss";
import { ApolloProvider } from "react-apollo";
import { ApolloClient, HttpLink, InMemoryCache } from "apollo-boost";
import { createHttpLink } from "apollo-link-http";
import { setContext } from "apollo-link-context";

const httpLink = createHttpLink({
  uri: "http://localhost:8080/graphql/"
});

const authLink = setContext((_, { headers }) => {
  // get the authentication token from local storage if it exists
  const token = localStorage.getItem("token");
  // return the headers to the context so httpLink can read them
  return {
    headers: {
      ...headers,
      authorization: token ? `Bearer ${token}` : ""
    }
  };
});

const client = new ApolloClient({
  // link: authLink.concat(HttpLink),
  link: HttpLink,
  cache: new InMemoryCache(),
  connectToDevTools: true
});

ReactDOM.render(
  <ApolloProvider client={client}>
    <App />
  </ApolloProvider>,
  document.getElementById("app")
);

任何帮助将不胜感激

1 个答案:

答案 0 :(得分:0)

解决了。问题出在进口。而不是apollo-boost的ApolloClient,它应该是

import { ApolloClient } from "apollo-client";