'Neo4J - How to query a graph and get the result as a tree with duplicates?

I`m working on a prototype of a community and have 4 nodes that are related as the image below. enter image description here

sample data:

MERGE (a:User{key: 1})
MERGE (b:Tags{key: 2})
MERGE (c:Post{key: 3})
MERGE (d:Comment{key: 4})
MERGE (e:Comment{key: 5})
MERGE (f:Comment{key: 6})
MERGE (g:User{key: 7})
MERGE (h:User{key: 8})
MERGE (i:Post{key: 9})
MERGE (j:Tags{key: 10})
MERGE (k:Post{key: 11})
MERGE (l:Comment{key: 12})


MERGE (a)-[:CREATE]-(b)
MERGE (a)-[:CREATE]-(c)
MERGE (a)-[:REACT]-(c)
MERGE (a)-[:CREATE]-(d)
MERGE (a)-[:REACT]-(d)
MERGE (b)-[:RELATED]-(c)
MERGE (d)-[:REPLY]-(c)
MERGE (d)-[:REPLY]-(d)
MERGE (h)-[:REACT]-(c)
MERGE (g)-[:REACT]-(c)
MERGE (h)-[:CREATE]-(j)
MERGE (j)-[:RELATED]-(c)
MERGE (g)-[:CREATE]-(i)
MERGE (e)-[:REPLY]-(i)
MERGE (f)-[:REPLY]-(i)
MERGE (h)-[:CREATE]-(k)
MERGE (l)-[:REPLY]-(k)
MERGE (a)-[:REACT]-(l)

I want to retrieve all the activities for some user

This is my current query

 MATCH reactions = (u:User{key:7})-[r]->(s) 
 WITH collect(reactions) as reactList  
 CALL apoc.convert.toTree(reactList, false) 
 YIELD value 
 RETURN value

The thing is that i want to retrieve the inner relations, so if an user REACT to a post i want to get the post and also get the user that created the post, and the tags that are related to that post.

My current response is

{
 "CREATE": [
   {
     "_type": "Post",
     "_id": 8,
     "key": 9
   }
 ],
 "_type": "User",
 "_id": 6,
 "REACT": [
   {
     "_type": "Post",
     "_id": 2,
     "key": 3
   }
 ],
 "key": 7
}

Where i have the user properties spread on the object and a Key representing each relation containing the nodes.

I expect to have that kind of response but with the inner relations inside (1 level deep)

{
  "REACT": [
    {
      "createdAt": "1649343307",
      "REACT.createdAt": "1649358772",
      "deletedAt": "1649425482",
      "createdBy": "e7a6ca14-2bd7-4212-a445-7cfc0588593c",
      "_type": "comment",
      "commentBody": "undefined undefined asdasdasdasdasdasd",
      "commentID": "1528ac92-0ff8-4fab-b86f-a61c50039373",
      "_id": 308,
      "deletedBy": "e7a6ca14-2bd7-4212-a445-7cfc0588593c",
      "updatedAt": "1649343307",
      "CREATE": [
        {
          "userFirstName": "someOthername",
          "userLastName": "someOthername",
          "userID": "e7a6ca14-2bd7-4212-a445-7cfc0588593c",
          "_id": 32
        }
      ]
    },
    {
      "createdAt": "1648923859",
      "topicKind": "text",
      "topicID": "7ede6654-e462-4f7d-bdf7-10a54f4a3c4a",
      "createdBy": "ac014441-7387-450c-ba4f-f94da233f169",
      "updatedAt": "1648923859",
      "topicBody": "Você são simplesmente uns merdas mesmo! Essa bosta desse programa de lixo!",
      "topicTitle": "Preparing tests",
      "RELATED": [
        {
          "identity": 1,
          "labels": ["tag"],
          "properties": {
            "createdAt": "1648923848",
            "tagID": "cd84cc95-7927-44ed-b2da-cf9e14044a39",
            "createdBy": "ac014441-7387-450c-ba4f-f94da233f169",
            "tagTitle": "Bull Market",
            "updatedAt": "1648923848",
            "tagDescription": "O melhor do mercado financeiro em uma única carteira de investimentos!"
          }
        }
      ],
      "CREATE": [
        {
          "userFirstName": "someOthername",
          "userLastName": "someOthername",
          "userID": "ac014441-7387-450c-ba4f-f94da233f169",
          "_id": 32
        }
      ]
    }
  ],
  "CREATE": [
    {
      "createdAt": "1649201536",
      "commentGuardDetails": "",
      "createdBy": "c36dfac7-a003-4c17-a494-ac40d4b32b76",
      "CREATE.createdAt": "1649201536",
      "_type": "comment",
      "commentBody": "acvadcdsh",
      "commentID": "9c70af3a-35c2-4ab0-a9c6-33a518e7a103",
      "_id": 191,
      "commentGuard": "approved",
      "updatedAt": "1649201536"
    }
  ],
  "userFirstname": "someName",
  "userEmail": "[email protected]",
  "_id": 95,
  "updatedAt": 1649378316
}

Thanks



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source