【发布时间】:2017-07-02 01:14:22
【问题描述】:
我有一个简单的查询,它在我的 Graphql 中运行良好,但我无法使用中继将数据传递给组件,我不知道为什么:(
{
todolist { // todolist returns array of objects of todo
id
text
done
}
}
这是我尝试使用中继在组件中传递数据的代码:
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 中查询,你可以看到查询的结构是一样的,我不知道为什么这不起作用?
【问题讨论】:
标签: reactjs relayjs graphql-js