neo4j-graphql-js 自定义 return @cypher 查询类型
neo4j-graphql-js custom return type for @cypher query
我正在尝试扩展使用 neo4j-graphql-js
自动生成的 graphql 模式
这是我的 graphql 模式
typeDefs
type Person {
_id: Long!
addresslocation: Point!
confirmedtime: DateTime!
healthstatus: String!
id: String!
name: String!
performs_visit: [Visit] @relation(name: "PERFORMS_VISIT", direction: OUT)
visits: [Place] @relation(name: "VISITS", direction: OUT)
VISITS_rel: [VISITS]
}
type Place {
_id: Long!
homelocation: Point!
id: String!
name: String!
type: String!
part_of: [Region] @relation(name: "PART_OF", direction: OUT)
visits: [Visit] @relation(name: "LOCATED_AT", direction: IN)
persons: [Person] @relation(name: "VISITS", direction: IN)
}
type Visit {
_id: Long!
duration: String!
endtime: DateTime!
id: String!
starttime: DateTime!
located_at: [Place] @relation(name: "LOCATED_AT", direction: OUT)
persons: [Person] @relation(name: "PERFORMS_VISIT", direction: IN)
}
type Region {
_id: Long!
name: String!
places: [Place] @relation(name: "PART_OF", direction: IN)
}
type Country {
_id: Long!
name: String!
}
type Continent {
_id: Long!
name: String!
}
type VISITS @relation(name: "VISITS") {
from: Person!
to: Place!
duration: String!
endtime: DateTime!
id: String!
starttime: DateTime!
}
现在我扩展 Person 来执行自定义查询,为此我使用 @cypher
指令
typeDefs2
type Person {
potentialSick: [Person] @cypher(statement: """
MATCH (p:this)--(v1:Visit)--(pl:Place)--(v2:Visit)--(p2:Person {healthstatus:"Healthy"})
return *
""")
}
我通过合并两个 typeDef 创建了模式,它按预期工作
export const schema = makeAugmentedSchema({
typeDefs: mergeTypeDefs([typeDefs, typeDefs2]),
config: {
debug: true,
},
});
问题
可以从我的自定义查询 return 自定义类型(在 graphql 中映射)potentialSick
?
我的目标是return一个类似于这个的类型
type PotentialSick {
id: ID
name: String
overlapPlaces: [Place]
}
重叠的地方是我的 neo4j 查询中的 pl
MATCH (p:this)--(v1:Visit)--(pl:Place)--(v2:Visit)--(p2:Person {healthstatus:"Healthy"})
我意识到 neo4j-graphql-js
是一个查询生成器,所以我可以通过使用 graphql 仅使用主模式来获取我的数据。我的查询将是:
{
Person(filter: { healthstatus: "Sick" }) {
id
visits {
_id
persons(filter: { healthstatus: "Healthy" }) {
_id
}
}
}
}
对于需要 @cyper
的更复杂查询,考虑到这一原则,我可以扩展每种类型的基本模式并依赖 graphql 功能
举个例子
type Person {
potentialSick: [Place] @cypher(statement: """
MATCH path =(this)-[:VISITS]->(place:Place)<-[:VISITS]-(p2:Person {healthstatus:"Healthy"})
return place
""")
potentialSick return places
然后为了获得访问那个地方的人我可以使用 graphql
{
Person(filter: { healthstatus: "Sick" }) {
id
potentialSick {
persons (filter: { healthstatus: "Healthy" }){
_id
}
}
}
}
我正在尝试扩展使用 neo4j-graphql-js
这是我的 graphql 模式
typeDefs
type Person {
_id: Long!
addresslocation: Point!
confirmedtime: DateTime!
healthstatus: String!
id: String!
name: String!
performs_visit: [Visit] @relation(name: "PERFORMS_VISIT", direction: OUT)
visits: [Place] @relation(name: "VISITS", direction: OUT)
VISITS_rel: [VISITS]
}
type Place {
_id: Long!
homelocation: Point!
id: String!
name: String!
type: String!
part_of: [Region] @relation(name: "PART_OF", direction: OUT)
visits: [Visit] @relation(name: "LOCATED_AT", direction: IN)
persons: [Person] @relation(name: "VISITS", direction: IN)
}
type Visit {
_id: Long!
duration: String!
endtime: DateTime!
id: String!
starttime: DateTime!
located_at: [Place] @relation(name: "LOCATED_AT", direction: OUT)
persons: [Person] @relation(name: "PERFORMS_VISIT", direction: IN)
}
type Region {
_id: Long!
name: String!
places: [Place] @relation(name: "PART_OF", direction: IN)
}
type Country {
_id: Long!
name: String!
}
type Continent {
_id: Long!
name: String!
}
type VISITS @relation(name: "VISITS") {
from: Person!
to: Place!
duration: String!
endtime: DateTime!
id: String!
starttime: DateTime!
}
现在我扩展 Person 来执行自定义查询,为此我使用 @cypher
指令
typeDefs2
type Person {
potentialSick: [Person] @cypher(statement: """
MATCH (p:this)--(v1:Visit)--(pl:Place)--(v2:Visit)--(p2:Person {healthstatus:"Healthy"})
return *
""")
}
我通过合并两个 typeDef 创建了模式,它按预期工作
export const schema = makeAugmentedSchema({
typeDefs: mergeTypeDefs([typeDefs, typeDefs2]),
config: {
debug: true,
},
});
问题
可以从我的自定义查询 return 自定义类型(在 graphql 中映射)potentialSick
?
我的目标是return一个类似于这个的类型
type PotentialSick {
id: ID
name: String
overlapPlaces: [Place]
}
重叠的地方是我的 neo4j 查询中的 pl
MATCH (p:this)--(v1:Visit)--(pl:Place)--(v2:Visit)--(p2:Person {healthstatus:"Healthy"})
我意识到 neo4j-graphql-js
是一个查询生成器,所以我可以通过使用 graphql 仅使用主模式来获取我的数据。我的查询将是:
{
Person(filter: { healthstatus: "Sick" }) {
id
visits {
_id
persons(filter: { healthstatus: "Healthy" }) {
_id
}
}
}
}
对于需要 @cyper
的更复杂查询,考虑到这一原则,我可以扩展每种类型的基本模式并依赖 graphql 功能
举个例子
type Person {
potentialSick: [Place] @cypher(statement: """
MATCH path =(this)-[:VISITS]->(place:Place)<-[:VISITS]-(p2:Person {healthstatus:"Healthy"})
return place
""")
potentialSick return places
然后为了获得访问那个地方的人我可以使用 graphql
{
Person(filter: { healthstatus: "Sick" }) {
id
potentialSick {
persons (filter: { healthstatus: "Healthy" }){
_id
}
}
}
}