Thingamajig
Thingamajig

Reputation: 4465

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

Hi I am looking to query my Neo4j database using Neo4j's Graphql library and the values I'd like to return look roughly like this:

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

I can probably do without using the range column, but would need the count(r.rating) and avg(r.rating) lists.

Is it possible to query for this data, as I usually expect to either return a type like Int, String, etc. or a custom Node type? If so what data type would I try to set in my type definitions for my cypher query to return? Essentially the "WHAT_GOES_HERE_AS_TYPE" part of this:

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)
    """),

Thank you in advance!

Upvotes: 0

Views: 146

Answers (1)

Tomaž Bratanič
Tomaž Bratanič

Reputation: 6524

You need to define a custom type and exclude all the auto-generated queries:

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

Then you can use it in the custom query definition. Note that the query should return a map object, so the Cypher statement will be a bit different:

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)}
    """)

You are returning multiple rows, so the CustomType should be wrapped in square brackets to represent a list.

Upvotes: 1

Related Questions