# How to query chain relationships in Neo4j

**URL:** <https://community.neo4j.com/t/how-to-query-chain-relationships-in-neo4j/58370>\
**Category:** Neo4j Graph Platform\
**Tags:** migrated\
**Created:** [January 13, 2023, 8:21am UTC](https://community.neo4j.com/t/how-to-query-chain-relationships-in-neo4j/58370 "2023-01-13T08:21:18Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![naveen420](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/naveen420/32/37883_2.png) [@naveen420](https://community.neo4j.com/u/naveen420)\
**Post date:** [January 13, 2023, 8:21am UTC](https://community.neo4j.com/t/how-to-query-chain-relationships-in-neo4j/58370/1 "2023-01-13T08:21:18Z")

</div>

Hi Everyone,  
I'm new to Neo4j and I'm working on poc where there are multiple services that can read and write to each other and modeled with \<CAN\_READ\_FROM(_INCOMING_):CAN\_WRITE\_TO(_OUTGOING_)\> directed relationships as shown in the image below. Now I want to query a workflow or chain relationships(paths) between nodes, so that they will return the all the similar paths. I have tried with apoc.path.pathConfig, but I haven't got the desired results(I'm not sure whether I have tried it right). Need suggestions on query, which will return the below paths.  
Updated:

 ![workfow_arch(3).png](https://us1.discourse-cdn.com/flex021/uploads/neo4jcommunity/original/3X/3/c/3c0992b74c739446526664c6baa169e7d4cfe906.png "workfow\_arch(3).png")

A,B,C,D,E are nodes that represent different services which can read and write from other service as shown above.  
Workflow is a series of nodes which can read and write from each other. Since the nodes are related to each other there can be multiple paths from each node to the destination based on the relationships mapped.

So, If I query the below multiple relationships or chain relationships between different nodes as below

**_MATCH (A)-[:CAN\_READ\_FROM]-\>(D)-[:CAN\_WRITE\_TO]-\>(C)-[:CAN\_READ\_FROM]-\>(B)-[:CAN\_WRITE\_TO]-\>(C)_**

Need to get suggestions for other paths as below  
A-E-C-B-C  
A-B-C-B-C

As, Service E and B can also read from A and write to C

If I query the paths between A-E-C-B-C

Need to get results as below :  
A-D-C-B-C  
A-B-C-B-C  
[@glilienfield](https://community.neo4j.com/t5/user/viewprofilepage/user-id/9010).

---

<div class="post-metadata">

**Author:** ![naveen420](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/naveen420/32/37883_2.png) [@naveen420](https://community.neo4j.com/u/naveen420)\
**Post date:** [January 13, 2023, 2:19pm UTC](https://community.neo4j.com/t/how-to-query-chain-relationships-in-neo4j/58370/2 "2023-01-13T14:19:58Z")

</div>

Thank you very much for your response. I have update my question. Please let me know, If it is still not clear. Thanks

---

<div class="post-metadata">

**Author:** ![glilienfield](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/glilienfield/32/27534_2.png) [@glilienfield](https://community.neo4j.com/u/glilienfield)\
**Post date:** [January 13, 2023, 1:19pm UTC](https://community.neo4j.com/t/how-to-query-chain-relationships-in-neo4j/58370/3 "2023-01-13T13:19:09Z")

</div>

sorry, I don’t understand. What is a workflow? What do you mean when you state “query the paths between (A-D-C-B-C)”. How do the resulting workflows correspond to the query path?

---

<div class="post-metadata">

**Author:** ![glilienfield](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/glilienfield/32/27534_2.png) [@glilienfield](https://community.neo4j.com/u/glilienfield)\
**Post date:** [January 13, 2023, 7:08pm UTC](https://community.neo4j.com/t/how-to-query-chain-relationships-in-neo4j/58370/4 "2023-01-13T19:08:57Z")

</div>

That is better. All of your example workflows are five long. Is this the requirement? I assumed that. I think this should work. It will find all paths of length five where the node as related by the two relationship types. I noticed all your workflows consisted of relationships in the same direction, so the query assumes that too.

```auto
MATCH p=(:Service)-[:CAN_WRITE_TO|CAN_READ_FROM*5]->(:Service)
RETURN [i in nodes(p) | i.name] as nodes

```

Note, cypher will not traverse the same relationships more than once on the same path. This is to avoid infinite loops. As such, your first example where the path traverses from 'B' to 'C' with a 'CAN\_WRITE\_FROM' relationships will not be in the results. The apoc path methods let you configure if the uniqueness is at the node level or relationship level.

---

<div class="post-metadata">

**Author:** ![naveen420](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/naveen420/32/37883_2.png) [@naveen420](https://community.neo4j.com/u/naveen420)\
**Post date:** [January 14, 2023, 8:54am UTC](https://community.neo4j.com/t/how-to-query-chain-relationships-in-neo4j/58370/5 "2023-01-14T08:54:37Z")

</div>

Thank you for your response
