我正在使用 neo4j dB,并且我已经设置了 apollo graphql 服务器(使用 graphql-server-express)。假设我的模式有 3 种类型,即“国家”、“州”和“人”,其中 1 个国家可以有多个州,1 个州可以有多个人。
//示例schema.js
import { makeExecutableSchema } from 'graphql-tools';
import resolvers from './resolvers';
const typeDefs = `
type Country {
id: Int!
name: String
state: [State]
people: [People]
}
type State {
id: Int!
name: String
countryID: CountryID
people: [People]
}
type People {
id: Int!
name: String
SSN: String
stateid:StateID
countryid:CountryID
}
type Query {
Countries: [Country]
States: [State]
Peoples: [People]
}
schema {
query: Query
}
`;
export default makeExecutableSchema({
typeDefs: typeDefs,
resolvers,
});
那么,我应该如何在 resolver.js 文件中编写我的解析器函数,以帮助我从上述任何类型中正确获取数据?
我尝试在 resolver.js 文件中使用以下查询(使用 Cypher 查询语言查询 Neo4j 数据库),但出现类型错误,我无法修复它。
//示例resolver.js 文件。
let neo4j = require('neo4j-driver').v1;
let driver = neo4j.driver("bolt://localhost", neo4j.auth.basic("neo4j",
"******"));
const resolver = {
Query: {
Countries(_, params) {
let session = driver.session();
let query = "MATCH (country:Country) RETURN country;"
return session.run(query, params)
.then( result => { return result.records.map(record => { return
record.get("country").properties })})
},
},
State:{
state(State) {
let session = driver.session(),
params = {countryid: Country.id},
query = `
MATCH (s:State-[:PRESENT]->(c:Country)
WHERE s.countryid = $countryid
RETURN s;
`
return session.run(query, params)
.then( result => { return result.records.map(record => { return
record.get("state").properties })})
},
},
};
export default resolver;