我的查询在接力失败,我不知道为什么?

时间:2017-02-13 22:07:28

标签: reactjs relayjs graphql-js

我有这个简单的查询在我的Graphql中运行正常,但我无法使用中继传递数据到组件,我不知道为什么:(

{
  todolist { // todolist returns array of objects of todo
    id
    text
    done
  }
}

这是我的代码,试图使用relay传递组件中的数据:

class TodoList extends React.Component {
  render() {
    return <ul>
      {this.props.todos.todolist.map((todo) => {
        <Todo todo={todo} />
      })}
    </ul>;
  }
}
export default Relay.createContainer(TodoList, {
  fragments: {
    todos: () => Relay.QL`
      fragment on Query {
        todolist {
          id
          text
          done
        } 
      }
    `,
  },
});

最后我的架构

const Todo = new GraphQLObjectType({
    name: 'Todo',
    description: 'This contains list of todos which belong to its\' (Persons)users',
    fields: () => {
        return {
            id: {
                type: GraphQLInt,
                resolve: (todo) => {
                    return todo.id;
                }
            },
            text: {
                type: GraphQLString,
                resolve: (todo) => {
                    return todo.text;
                }
            },
            done: {
                type: GraphQLBoolean,
                resolve: (todo) => {
                    return todo.done;
                }
            },
        }
    }
});

const Query = new GraphQLObjectType({
    name: 'Query',
    description: 'This is the root query',
    fields: () => {
        return {
            todolist: {
                type: new GraphQLList(Todo),
                resolve: (root, args) => {
                    return Conn.models.todo.findAll({ where: args})
                }
            }
        }
    }
});

这段代码看起来很简单,我看不出为什么这不起作用我有这个错误Uncaught TypeError: Cannot read property 'todolist' of undefined,但是我配置了todolist并且我可以在我的graphql中查询,你可以看到查询的结构是一样的,我不知道为什么这不起作用?

1 个答案:

答案 0 :(得分:1)

todolist应该是Query上的连接类型。此外,您的ID应该是中继全局ID。您将无法访问您的对象&#39;继电器中的原始本地id字段。

import {
  connectionArgs,
  connectionDefinitions,
  globalIdField,
} from 'graphql-relay';

// I'm renaming Todo to TodoType
const TodoType = new GraphQLObjectType({
  ...,
  fields: {
    id: uidGlobalIdField('Todo'),
    ...
  },
});

const {
  connectionType: TodoConnection,
} = connectionDefinitions({ name: 'Todo', nodeType: TodoType });

// Also renaming Query to QueryType
const QueryType = new GraphQLObjectType({
  ...,
  fields: {
    id: globalIdField('Query', $queryId), // hard-code queryId if you only have one Query concept (Facebook thinks of this top level field as being a user, so the $queryId would be the user id in their world)
    todos: { // Better than todoList; generally if it's plural in Relay it's assumed to be a connection or list
      type: TodoConnection,
      args: connectionArgs,
    },
  },
});

// Now, to be able to query off of QueryType
const viewerDefaultField = {
  query: { // Normally this is called `viewer`, but `query` is ok (I think)
    query: QueryType,
    resolve: () => ({}),
    description: 'The entry point into the graph',
  }
};

export { viewerDefaultField };

上述内容尚未完全完成(您可能还需要在一个或多个类型上设置节点接口,这将需要节点定义),但它应该回答您的基本问题并开始使用。

学习这是一个巨大的巨大痛苦,但是一旦你努力学习它就会开始变得有意义,并且你会开始喜欢RESTful通话。