将变量传递给 Neo4j apoc.do.case 过程中使用的查询

Pass variable to query used in a Neo4j apoc.do.case procedure

在此查询中,我想检查 $accountId$userId 参数的有效性,如果它们有效,则创建一个附加到用户和帐户节点的新项目。

optional match (u:User {id:$userId})
optional match (a:Account {id:$accountId})
call apoc.do.case([
   not exists((u)-[:ACCOUNT_ADMIN]->(a)),
   "return {error:'user is not admin of the specified account'} as result",
   "match (u:User {id:$userId})-[:ACCOUNT_ADMIN]->(a:Account {id:$accountId})
      create (a)-[:CONTAINS]->(:Project {name:$projectName})<-[:PROJECT_ADMIN]-(u)
      return {result:'project created'} as result",
   {accountId:$accountId, userId:$userId, projectName:$projectName}
)
yield value
return value.result as result

它似乎有效,但并没有让我觉得很优雅。特别令人讨厌的是,在传递给 apoc.do.caseelse 查询中,我必须执行另一个 match 来重新定位我已经位于外部 optional matches 的节点。

有没有办法重用我已经位于 apoc.do.case else 中的 optional match 中的 ua 变量] 查询?

你试过吗?除了参数,我认为你应该使用 apoc.do.when

optional match (u:User {id:$userId})
optional match (a:Account {id:$accountId})
call apoc.do.when(
   not exists((u)-[:ACCOUNT_ADMIN]->(a)),
   "return {error:'user is not admin of the specified account'} as result",
   "with $a  AS a,$u AS u create (a)-[:CONTAINS]->(:Project {name:$projectName})<-[:PROJECT_ADMIN]-(u) return {result:'project created'} as result",
   {u:u,a:a, projectName:$projectName}
)
yield value
return value.result as result