【发布时间】:2017-12-07 09:13:16
【问题描述】:
在我的解析器中说我有以下内容:
Query: {
author: (root, args) => API.getAuthor(args.authorId),
},
Author: {
book: (author, args) => API.getBook(author.id, args.bookId),
},
Book: {
// Here, I can't get the author ID
chapter: (book, args) => API.getChapter(authorId???, book.id, args.chapterId),
}
从上面的例子中我的问题很清楚,我怎样才能从更高的几个级别访问变量? 我希望能够提出如下请求:
author(authorId: 1) {
id
book(bookId: 31) {
id
chapter(chapterId: 3) {
content
}
}
}
而我获取特定章节的连接器也需要作者的ID。
【问题讨论】:
-
你不能,这是故意的
-
这本书没有'author_id'字段吗?
-
@whitep4nther 哦该死的,为什么?不,在我的真实案例中,book 没有 author_id 字段。
-
那是因为Book实体也可以包含在其他对象中。您现在拥有
author { book { chapter } },但您也可以拥有library { book { chapter } }。每个对象都负责用自己的数据获取他的字段,这使得整个事情可以组合。不过我有一个解决方案的想法,所以我正在写一个答案。
标签: graphql relayjs graphql-js apollo react-apollo