Search code examples
cypherspring-data-neo4j

Rendering a dynamic list of labels from a List<String>


I need to do something similar to:

@Query("MATCH (n:`:#{literal(#label)}`) WHERE n.entityId=$entityId  RETURN n")
Mono<VisibilityGroup> findNode(UUID entityId, String label);

but specifying a list of labels in OR:

Mono<VisibilityGroup> findNode(UUID entityId, List<String> labels);

if label is {"A","B"} this should generate:

MATCH (n:A|B) WHERE...

What is the correct syntax to achieve this?


Solution

  • For now this would be only possible if you concatenate the List<String> to a String with | separator.

    String labels = String.join("|", List.of("A", "B"));
    

    or

    String labels = new StringJoiner("|")
            .add("A")
            .add("B")
            .toString();
    

    The literal function will create a [A,B] value which is incompatible to the expected pattern in the pattern (obviously). In the longer run it might make sense to evaluate if it's possible to integrate a more specialised keyword (like #{labels(..)}) in Spring Expression Language containing queries for Spring Data Neo4j.