我是 GraphQL 新手,不知道如何在子字段上实现查询。
例如,假设我们有以下类型和查询:
type Author {
joinedDate: String,
quote: String
}
type Post {
id: String,
author: Author,
text: String,
timeStamp: String,
location: String
}
type Query {
posts(authorId: String): [Post]
}
然后,客户可以提出如下请求:
// gets everything
{
posts(authorId: Steve)
}
// just gets time, text, and location
{
posts(authorId: Steve) {
timeStamp
text
location
}
}
然后可以像这样实现根对象:
const root = {
posts: (authorId) => {
return dataService.getPosts(authorId)
}
}
我的问题是您将如何在子字段上实现查询/过滤器。例如:
// just gets time, text, and location for specific date range
{
posts(authorId: Steve) {
timeStamp(minDate: 01012015, maxDate: 10102018)
text
location
}
}
我将如何定义该根方法?我是否需要在posts
root 方法中手动过滤完整的帖子列表?
const root = {
// would this method even have access to the date args?
posts: (authorId, minDate, maxDate) => {
const newList = filterTheList(
dataService.getPosts(authorId),
minDate,
maxDate
)
return newList
}
}
谢谢阅读!