如何在 Neo4j Graphql 中查询多个非节点值?

How can I query multiple non-node values in Neo4j Graphql?

您好,我想使用 Neo4j 的 Graphql 库查询我的 Neo4j 数据库,我想要 return 的值大致如下所示:

range   | count(r.rating)       | avg(r.rating)
[0, 1]  | 2                     | 0.65
[1, 2]  | 2                     | 1.75
[2, 3]  | 1                     | 2.2
[3, 4]  | 1                     | 3.1
[4, 5]  | 3                     | 4.666666666666667

我可能可以不使用范围列,但需要 count(r.rating) 和 avg(r.rating) 列表。

是否可以查询此数据,正如我通常期望的 return Int、String 等类型或自定义节点类型?如果是这样,我会尝试在我的密码查询的类型定义中将什么数据类型设置为 return?本质上是“WHAT_GOES_HERE_AS_TYPE”部分:

rating_values: WHAT_GOES_HERE_AS_TYPE @cypher(statement: """
        UNWIND [[0,1], [1,2], [2,3], [3,4], [4,5]] as range
        MATCH (t:Thing)-[]->(r:Rating)
        WHERE r.value > range[0] and r.value <= range[1]
        RETURN range, count(r.value), avg(r.value)
    """),

提前致谢!

您需要定义自定义类型并排除所有自动生成的查询:

type CustomType @exclude(operations: [READ, CREATE, UPDATE, DELETE]) {
  range: [Int],
  count: Int,
  avg: Float
}

然后您可以在自定义查询定义中使用它。注意查询应该return一个map对象,所以Cypher语句会有点不同:

rating_values: [CustomType] @cypher(statement: """
        UNWIND [[0,1], [1,2], [2,3], [3,4], [4,5]] as range
        MATCH (t:Thing)-[]->(r:Rating)
        WHERE r.value > range[0] and r.value <= range[1]
        RETURN {range: range, count: count(r.value), avg: avg(r.value)}
    """)

您正在 return 设置多行,因此 CustomType 应括在方括号中以表示列表。