react-apollo gql,TypeError:Object(...)不是函数

时间:2017-11-18 15:17:35

标签: javascript reactjs apollo

我有一个App组件,我将其包装到apollo提供程序中:

import React, { Component } from "react";
import { observer, Provider } from "mobx-react";
import { BrowserRouter as Router } from "react-router-dom";
import styled from "styled-components";
import { ThemeProvider } from "styled-components";

// graphQL
import { ApolloClient } from "apollo-client";
import { createHttpLink } from "apollo-link-http";
import { setContext } from "apollo-link-context";
import { InMemoryCache } from "apollo-cache-inmemory";

import Main from "./components/Main/Main";
import Navigation from "./components/Navigation/Navigation";

import { ApolloProvider } from "react-apollo";

const httpLink = createHttpLink({
  uri: "myUri"
});

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

const mozaikClient = new ApolloClient({
  link: authLink.concat(httpLink),
  cache: new InMemoryCache()
});

@observer
class App extends Component {
  render() {
    return (
      <ThemeProvider theme={theme}>
        <ApolloProvider client={mozaikClient}>
          <Provider {...this.props.stores}>
            <Router>
              <div className="app-container">
                <Navigation />
                <Main />
              </div>
            </Router>
          </Provider>
        </ApolloProvider>
      </ThemeProvider>
    );
  }
}

export default App;

然后我有我想要使用提供者数据的About组件:

import React, { Component } from "react";

import { gql, graphql } from "react-apollo";
class About extends Component {
  render() {
    return <div>About component.</div>;
  }
}

const landingPageQuery = gql`
  query landingPage {
    documents(types: [LANDING]) {
      items {
        id
        ... on LandingDocument {
          landingPortrait {
            id
            url
          }
          greeting
          content
        }
      }
    }
  }
`;

export default graphql(landingPageQuery)(About);

我收到以下错误:

  

About.js:10 Uncaught TypeError:Object(...)不是函数       at Object ../ src / components / About / About.js(About.js:10)       在 webpack_require (bootstrap 9e0e85e19ef23447e3e2:669)       在fn(bootstrap 9e0e85e19ef23447e3e2:87)       at Object ../ src / components / Main / routes.js(routes.js:1)       在 webpack_require (bootstrap 9e0e85e19ef23447e3e2:669)       在fn(bootstrap 9e0e85e19ef23447e3e2:87)       at Object ../ src / components / Main / Main.js(Contact.js:4)       在 webpack_require (bootstrap 9e0e85e19ef23447e3e2:669)       在fn(bootstrap 9e0e85e19ef23447e3e2:87)       at Object ../ src / App.js(zen-observable.js:498)       在 webpack_require (bootstrap 9e0e85e19ef23447e3e2:669)       在fn(bootstrap 9e0e85e19ef23447e3e2:87)       at Object ../ src / index.js(Skills.js:4)       在 webpack_require (bootstrap 9e0e85e19ef23447e3e2:669)       在fn(bootstrap 9e0e85e19ef23447e3e2:87)       在Object.0(styleVariables.js:17)       在 webpack_require (bootstrap 9e0e85e19ef23447e3e2:669)       在bootstrap 9e0e85e19ef23447e3e2:715       在bundle.js:719

此错误的来源和原因来自何处,这是什么意思?它指向我使用gql

定义查询的行

3 个答案:

答案 0 :(得分:9)

这似乎是一个已知问题 - 请参阅here。 尝试安装graphql-tag并从此库中导入gql

答案 1 :(得分:3)

如果您中的某些人仍然遇到相同的问题,请尝试使用apollo-boost

导入

import { gql } from 'apollo-boost'

答案 2 :(得分:2)

删除大括号。

代替:

import { gql } from 'graphql-tag';

import gql from 'graphql-tag';