@@ -190,14 +190,22 @@ Statement toStatement(NodeDescription<?> nodeDescription) {
190190 Relationship relationships = Cypher .anyNode ().relationshipBetween (Cypher .anyNode ()).named (relationshipIds );
191191 return Cypher .match (rootNodes )
192192 .where (Functions .id (rootNodes ).in (Cypher .parameter (rootNodeIds )))
193+ .with (Functions .collect (rootNodes ).as (Constants .NAME_OF_ROOT_NODE ))
193194 .optionalMatch (relationships )
194195 .where (Functions .id (relationships ).in (Cypher .parameter (relationshipIds )))
196+ .with (Constants .NAME_OF_ROOT_NODE , Functions .collectDistinct (relationships ).as (Constants .NAME_OF_SYNTHESIZED_RELATIONS ).asExpression ())
195197 .optionalMatch (relatedNodes )
196198 .where (Functions .id (relatedNodes ).in (Cypher .parameter (relatedNodeIds )))
197199 .with (
198- rootNodes .as (Constants .NAME_OF_TYPED_ROOT_NODE .apply (nodeDescription ).getValue ()),
199- Functions .collectDistinct (relationships ).as (Constants .NAME_OF_SYNTHESIZED_RELATIONS ),
200- Functions .collectDistinct (relatedNodes ).as (Constants .NAME_OF_SYNTHESIZED_RELATED_NODES ))
200+ Constants .NAME_OF_ROOT_NODE ,
201+ Cypher .name (Constants .NAME_OF_SYNTHESIZED_RELATIONS ).as (Constants .NAME_OF_SYNTHESIZED_RELATIONS ),
202+ Functions .collectDistinct (relatedNodes ).as (Constants .NAME_OF_SYNTHESIZED_RELATED_NODES ).asExpression ()
203+ )
204+ .unwind (Constants .NAME_OF_ROOT_NODE ).as (rootNodeIds )
205+ .with (
206+ Cypher .name (rootNodeIds ).as (Constants .NAME_OF_TYPED_ROOT_NODE .apply (nodeDescription ).getValue ()).asExpression (),
207+ Cypher .name (Constants .NAME_OF_SYNTHESIZED_RELATIONS ),
208+ Cypher .name (Constants .NAME_OF_SYNTHESIZED_RELATED_NODES ))
201209 .orderBy (queryFragments .getOrderBy ())
202210 .returning (
203211 Constants .NAME_OF_TYPED_ROOT_NODE .apply (nodeDescription ).as (Constants .NAME_OF_SYNTHESIZED_ROOT_NODE ),
0 commit comments