Table of Contents
type User {
name:String
referredBy: Referral @relation(direction: OUT)
referred:[Referral] @relation(direction: IN)
}
type Referral @relation (name:"REFERRED_BY", from:"user", to: "referredBy" ) {
user:User
referredBy:User
referralDate:String
}
GraphQL-Query
{user(name:"Jane") {
name
referredBy { referralDate referredBy {name} }
referred { referralDate user {name} }
} }
Cypher params
{
"userName" : "Jane"
}
Cypher
MATCH (user:User)
WHERE user.name = $userName
CALL {
WITH user
OPTIONAL MATCH (user)-[userReferredBy:REFERRED_BY]->(userReferredByReferredBy:User)
RETURN userReferredBy {
.referralDate,
referredBy: userReferredByReferredBy {
.name
}
} AS userReferredBy LIMIT 1
}
CALL {
WITH user
MATCH (user)<-[userReferred:REFERRED_BY]-(userReferredUser:User)
RETURN collect(userReferred {
.referralDate,
user: userReferredUser {
.name
}
}) AS userReferred
}
RETURN user {
.name,
referredBy: userReferredBy,
referred: userReferred
} AS user