'Can you use RETURN AS within the @cypher Directive in your GraphQL Schema?
Say I have this simple schema with just a Person type:
// schema.graphql
type Person {
Name: String,
Age: Int,
parents: [Person!]! @relationship(type: "PARENT", direction: OUT)
}
And I want a query that gets me every person's name with grandparent's name, by traversing the "PARENT" relationship twice. In Cypher I would write something like this:
MATCH (child:Person)-[:PARENT]->[parent:Person]-[:PARENT]->(grandparent:Person)
RETURN child.Name AS child_name, grandparent.Name AS grandparent_name
Is it possible to use this cypher query within a @cypher directive for my graphQL query? The goal is to do the "traversing" in cypher to get the grandparent's name and return it directly with the child's name, instead of having to traverse through the GraphQL response to get it, which I would need to do if I just returned the child.
My idea was to define a new type for what this query would return, something like:
// schema.graphql
type Child_Grandparent_Pair{
child_name: String,
grandparent_name: String
}
And write my GraphQL query like so:
// schema.graphql
type Query{
getChildrenAndGrandparentsNames:[Child_Grandparent_Pair]
@cypher(
statement: "MATCH (child:Person)-[:PARENT]->[parent:Person]-[:PARENT]->
(grandparent:Person)
RETURN child.Name AS child_name, grandparent.Name AS grandparent_name"
}
}
But this doesn't seem to work. When I try to query on my new query type, I get an error from neo4j that the return type isn't defined. Hopefully this makes sense what I'm trying to do. Is there a better way?
Solution 1:[1]
Return a Object/Map that matches the shape of your GraphQL type:
type Query {
getChildrenAndGrandparentsNames: [Child_Grandparent_Pair]
@cypher(
statement: """
MATCH (child:Person)-[:PARENT]->[parent:Person]-[:PARENT]->(grandparent:Person)
- RETURN child.Name AS child_name, grandparent.Name AS grandparent_name
+ RETURN { child_name: child.name, grandparent_name: grandparent.name }
"""
)
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Dan Starns |
