当试图返回单个对象,而不是数组在graphql中为neo4j db
问题描述:
我想返回一个单一的对象(而不是数组)。当我尝试返回单个对象时,graphql服务器返回null。但似乎工作当尝试返回一个数组时很好。当试图返回单个对象,而不是数组在graphql中为neo4j db
这里是我的架构例如:
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:字符串)”,我得到以下结果:
{
"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:字符串!):作者
//而不是像这样的数组 - > getAuthorById(authorID:String!):[作者]
所以,有人可以让我知道我在这里做错了什么?所有我需要的是返回单个对象,而不是阵列....提前致谢
答
问题是在你的解析器,具体而言,你是从解析器返回result.records.map()
的结果。 map()
计算结果为阵列(在这种情况下,将内部函数的result
每个元素
相反,你可以只抢了先Record
的Result
流:
.then(result => {
return result.records[0].get("a").properties
}
)
感谢@William,它的工作..最后,请你指出一些具体的链接,我可以详细了解这些属性和neo4j文档,因为我已经阅读了大部分neo4j博客,但仍然没有得到你所提供的有用的见解,再次感谢! –
伟大的!Neo4j JavaScript驱动程序的文档在这里:http://neo4j.com/docs/api/javascript-driver/current/ –