我实际上正在使用 Neo4j DB 并使用 neo4j_movie_recommendations 代码中的示例来试验 GraphQL 和 GraphiQL。我有一个非常奇怪的问题,似乎与 javascript 或 GraphQL 相关。我做了一个简单的查询,而我可以看到解析器(使用 console.log)可以返回数据,GraphQL 返回一个“空”值。以下是代码段:
const resolveFunctions = {
Query: {
allVoters(_, params) {
let session = driver.session();
let query = "MATCH (voter:Voter) RETURN voter;"
return session.run(query, params)
.then(result => {
return result.records.map(record => {
console.log("Voter: " + JSON.stringify(record))
return record.get("voter").properties
})
})
},
},
Voter: {
memberOf(voter) {
let session = driver.session(),
params = { voterid: voter.voterid },
query = `
MATCH (v:Voter)-[:MEMBER_OF]->(g:Group)
WHERE v.voterid = $voterid
RETURN g.name AS groupname;
`
return session
.run(query, params)
.then(result => {
return result.records.map(record => {
console.log("Group Name " + record.get("groupname"))
return record.get("groupname")
})
})
},
console.log 显示正确的值,但 GraphiQL 显示名称、电子邮件..正确,但 memberof 字段显示为“null”。谁能看到我在这里做错了什么?
这是架构......
export default `
type Voter {
voterid : String!
email : String!
name : String!
nickname : [String]
address : String
altAddress : String
dob : String
profession : String
telephone : String
gender : String
skills : [String]
votesIn : PD
livesIn : PD
memberOf : [Group]
}
type Group {
name : String
location : String
created : String
partof : Division
}
type PD {
number : String
location : String
created : String
description : String
partof : Division
}
type Division {
name : String
created : String
belongsto : Constituency
}
type Constituency {
name : String
created : String
reportsto : Region
}
type Region {
name : String
created : String
reportsto : Exec
}
type Exec {
name : String
created : String
}
type Query {
allVoters: [Voter]!
getVoter(voterid: String!): Voter
}
schema {
query: Query
}
`;
编辑...为 getvoter 添加解析器解析器代码:
getVoter(_, params) {
let session = driver.session();
let query = "MATCH (voter:Voter {voterid: $voterid}) RETURN voter;"
return session.run(query, params)
.then(result => {
return result.records.map(record => {
return record.get("voter").properties
})
})
},