我正在使用apollo graphql服务器 . 我有一个非常无序的查询 .
query feedQuery {
allPostsFromAllBlogs
{
id
blogId
title
text
likes
createdAt
}
}
我想对结果进行排序(类型为[Post]) . 这是我用来做的那个查询:
query feedQuery {
allPostsFromAllBlogs( orderBy: {field: createdAt, direction:DESC})
{
id
blogId
title
text
likes
createdAt
}
}
哪个在graphiql中产生这个输出:
{
"errors": [
{
"message": "Unknown argument \"orderBy\" on field \"allPostsFromAllBlogs\" of type \"Query\".",
"locations": [
{
"line": 2,
"column": 25
}
]
}
]
}
继承架构:
scalar DateTime
type Query {
blogInfo(id: String): Blog
post(id: String): Post
topPost(blogId: String): Post
allPosts(blogId: String): [Post]
allPostsFromAllBlogs: [Post]
topPostFromAllBlogs: [BlogWithPost]
comments(postId: String): Comments
allBlogs: [Blog]
}
type Mutation {
createPost(title: String,
content: String,
type: String,
blogId: String,
avatar: String): Post
}
type Blog {
id: String
title: String
description: String
authorName: String
avatar: String
}
type Post {
id: String
blogId: String
title: String
content: String
likes: Int
avatar: String
createdAt: DateTime
type: String
}
type BlogWithPost {
id: String
title: String
description: String
authorName: String
avatar: String
post: Post
}
type Comment {
commenterId: String
text: String
}
type Comments {
postId: String
comments: [Comment]
}
我不确定诊断问题需要哪些其他信息,但我认为它的查询语法错误 .
为什么不识别orderBy?是否有不同的或更好的方法来排序结果?
1 回答
问题是因为
allPostsFromAllBlogs
没有任何输入类型(即过滤器) . 从架构中可以看出:字段
allPosts
的输入类型为blogId: String
,但字段allPostsFromAllBlogs
未实现任何输入类型 .您必须将字段
allPostsFromAllBlogs
更改为:而且您必须更改该字段的解析器 .