Question about many relationships and automatic mapping

Hello

Im currently working on a cypher query and have questions about automatic mapping.
As an example I will use this part of a query I'm working on:

MATCH (c:Community)
WITH c {
.*,
__nodeLabels__:labels(c),
Community_DE_Language: [(c)-[:DE]->(cl:Language)|cl{.*, __nodeLabels__:labels(cl)}],
Community_FR_Language: [(c)-[:FR]->(cl:Language)|cl{.*, __nodeLabels__:labels(cl)}],
Community_IT_Language: [(c)-[:IT]->(cl:Language)|cl{.*, __nodeLabels__:labels(cl)}],
Community_EN_Language: [(c)-[:EN]->(cl:Language)|cl{.*, __nodeLabels__:labels(cl)}]
} as community
RETURN community

As it stands, it works just fine. After returning this from the query I use neo4jMappingContext.getRequiredMappingFunctionFor(Community.class) to generate the appropriate Java object.
I wonder though if it's at all possible to somehow "collect" the relations and their nodes (in this example "cRel" and "clTest") and append them inside the "WITH" clause.

I imagined that somewhat like this:

MATCH (c:Community)-[cRel]->(cltest:KnowledgeEntryLanguage)
WITH c {
.*,
__nodeLabels__:labels(c),
__relationships__:collect(cRel),
__relationshipNodes__:collect(cltest)
} as community
RETURN community

Of course in my example this isn't exactly needed, but it would prove handy on other parts of our code.

EDIT:
This question originally stems from the last query in this part of the SDN Documentation: Spring Data Neo4j

You got quite right with the assumption, but the query pattern (as described here Spring Data Neo4j) in the documentation is what you want to have.

The query would become something like:

MATCH (c:Community)-[cRel]->(cltest:KnowledgeEntryLanguage)
RETURN c, collect(cRel), collect(cltest)
1 Like

Hey, thank you for the quick response. Thanks to that I was able to make it work. The Problem initially was that I specifically picked the value out of the record. Where as I should have just provided the whole record to the mappingFunction.

Heres the "finished" query + mapping. For anyone looking for something like this.

String query = """
                MATCH (c:Community {id: $id})-[cRel]->(cl:KnowledgeEntryLanguage)
                RETURN c, collect(cRel), collect(cl)
                """;

        var mappingFunctionCommunity = this.neo4jMappingContext.getRequiredMappingFunctionFor(Community.class);

        return neo4jClient.query(query)
                .bind(id).to("id")
                .fetchAs(Community.class)
                .mappedBy((typeSystem, record) -> 
                        mappingFunctionCommunity.apply(typeSystem, record))
                .one();