GraphQL和ReactJS未定义导入的<query>组件

时间:2018-09-06 02:00:53

标签: javascript reactjs graphql graphql-tag

我试图在ReactJS中渲染我的HOC函数,但这给了我一个错误。以下是一些摘要(出于空间和清晰度的考虑,我已删除了无关的内容):

在我的app.js组件中,我是这样导入我的HOC的:

import React, { Component } from "react";
import "./App.css";
import { Route, Switch, BrowserRouter as Router } from "react-router-dom";
import Home from "../Home/Home";
import StoriesHOC from "../Story/GetAllStories/Stories";

export default class App extends Component {
 render() {
   return (
  <Router>
    <div>
      <Navigation />
      <Switch>
        <Route exact path="/" component={Home} />
        <Route
          path="/all_stories"
          render={() => {
            return <StoriesHOC />;
          }}
        />
         </Switch>
       </div>
    </Router>
   );
 }
}

然后进入<StoriesHOC />

import React from "react";
import StoryShow from "../StoryShow/StoryShow";
import { Query, Fetching, Error } from "react-apollo";
import { getStories } from "../../Queries/Queries";

function Stories({ stories, getStoriesQuery }) {
  return (
    <div>
      <div className="stories_header">
       <h2>All Stories</h2>
      </div>
     <div className="stories-container">
       {stories.map(story => (
         <StoryShow
          key={story.id}
          story={story}
          getStoriesQuery={getStoriesQuery}
      />
      ))}
     </div>
    </div>
   );
  }

 export default function StoriesHOC(props) {
   return (

   <Query query={getStories}>
  {({ loading, error, data }) => {
      console.log(data)
  if (error) return <Error />
  if (loading || !data) return <Fetching />

  return <Stories 
          {...props} 
          getStoriesQuery={getStories} 
          stories={(data && data.stories) || []}/> }}
  </Query>
 );
}

编辑:这是StoryShow组件,因为它也正被导出为默认功能。

  import React from "react";
  import { Link } from 'react-router-dom'

  export default function StoryShow({
   story: {id, title, author, tagline, summary} = {}
   }) {
  return (
    <div>
      <h1>{title}</h1>
      <h2>{tagline}</h2>
      <h3>{author}</h3>
      <p>{summary}</p>
      <Link exact to={'/story/' + id}>Read More</Link>
   </div>
  )
 }

实际的GraphQL查询import { getStories } from "../../Queries/Queries";在另一个文件中进行,其格式如下:

 import gql from "graphql-tag";

 export const getStories = gql`
   query getStories {
      stories {
        id
        title
        author
        tagline
      }
     }
   `;

但是,Chrome浏览器中显示的实际错误是:

 Element type is invalid: expected a string (for built-in components) 
 or a class/function (for composite components) but got: undefined. You 
 likely forgot to export your component from the file it's defined in, 
 or you might have mixed up default and named imports.

 Check the render method of `StoriesHOC`.

稍后它会在StoriesHOC函数中较早显示的<Query query={getStories}>处给出该行。 话虽这么说,看来我的问题仅源于此行,但是尽管我一直在努力解决该问题,但我最好的猜测只是我在做什么GraphQL查询中出现错误,但是当我在后端检查IDE时,它可以完美呈现。

还有一个问题,试图弄清楚是否正确地将导入的功能导出到我的app.js文件中,但是我知道,因为我使用export default function myFunc导出,因此应将其导入为当前状态

任何帮助以及建议或批评将不胜感激。预先谢谢你!

2 个答案:

答案 0 :(得分:0)

如果组件具有剥离的代码/内容

export default function StoriesHOC(props) {
  return (
    <Query query={getStories}>
      {({ data }) => {
        console.log(data)
        return <div>fake content</div>
      }
    </Query>
  )
}

正确渲染,然后查询就可以了,而被拆下的零件有问题。逐一恢复它们,以查找/隔离真实问题的根源。

答案 1 :(得分:0)

我发现了我的问题,这是react-apollo, graphql-tag, and apollo-boost.的npm安装过时了。听起来很明显,这本来应该是我的第一眼,但我实际上是在两周前更新了它们!

对于路过的人,以下是对我有用的软件包版本的摘录:

"dependencies": {
  "apollo-boost": "^0.1.15",
  "cors": "^2.8.4",
  "express": "^4.16.3",
  "graphql": "^0.13.2",
  "graphql-server-express": "^1.4.0",
  "graphql-tag": "^2.9.2",
  "react": "^16.5.0",
  "react-apollo": "^2.1.11",
  "react-dom": "^16.4.2",
  "react-router-dom": "^4.3.1",
  "react-scripts": "1.1.4"
 }