你能在GraphQL模式中的@cypher指令中使用RETURN AS吗



假设我有一个简单的模式,只有一个Person类型:

// schema.graphql
type Person {
Name: String,
Age: Int,
parents: [Person!]! @relationship(type: "PARENT", direction: OUT)
}

我想要一个查询,通过遍历";家长";两次恋爱。在Cypher中,我会写这样的东西:

MATCH (child:Person)-[:PARENT]->[parent:Person]-[:PARENT]->(grandparent:Person)
RETURN child.Name AS child_name, grandparent.Name AS grandparent_name

对于我的graphQL查询,是否可以在@cypher指令中使用此密码查询?目标是做";遍历";在cypher中,获取祖父母的名字并直接将其与孩子的名字一起返回,而不必遍历GraphQL响应来获取它,如果我刚刚返回孩子,我需要这样做。

我的想法是为这个查询返回的内容定义一个新的类型,比如:

// schema.graphql
type Child_Grandparent_Pair{
child_name: String,
grandparent_name: String
}

然后这样写我的GraphQL查询:

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

但这似乎不起作用。当我尝试查询我的新查询类型时,我从neo4j得到一个错误,即返回类型没有定义。希望这对我的努力有意义。有更好的方法吗?

返回与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
+            RETURN { child_name: child.name, grandparent_name: grandparent.name }
"""
)
}

相关内容

最新更新