我想返回单个对象(不是数组)。当我尝试返回单个对象时,graphql 服务器返回 null。但是在尝试返回数组时似乎工作正常。
这是我的架构示例:
type Author{
authorID: String
label: String
posts: [Post]
}
type Post {
postID: String
label: String
author: Author
}
type Query {
getAuthorById (authorID: String!): Author #-> this does not work
getAuthorById (authorID: String!): [Author] #-> this works
}
但是,当我尝试运行此查询“ getAuthorById (authorID: String!) ”时,我得到以下结果:
{
"data": {
"getAuthorById": {
"label": null,
"authorID": null
}
}
但是,它似乎只有在我尝试返回一个数组时才有效(当我尝试像这样更改类型查询的架构时):
type Query {
getAuthorById (authorID: String!): [Author]
}
这是我的 resolver.js:
Query: {
getAuthorById(_, params) {
let session = driver.session();
let query = `MATCH (a:Author{ authorID: $authorID}) RETURN a ;`
return session.run(query, params)
.then( result => {
return result.records.map( record => {
return record.get("a").properties
}
)
}
)
},
}
我需要的是返回一个像这样的对象: getAuthorById (authorID: String!): Author
// 而不是这样的数组-> getAuthorById (authorID: String!): [Author]
那么,有人可以让我知道我在这里做错了什么吗?我只需要返回单个对象而不是数组....在此先感谢