Hi I have a Neo4j graph where Users can create Posts and can also follow Movies or Shows or add these things to Lists.
With posts I can get the most recent Posts by Users followed other Users just by ordering by date and that's a single feed, but I am looking to create another feed that is more of a catch all.
It would show any time a User follows another User , or a User adds something to a List, or follows another User, etc.
For this I am interested in seeing how I might be able to structure the graph and these relationships to even be able to create a GraphQL query that can return this stuff, and a node/object type that could be returned.
Do I add properties to relationships between nodes and try to sort by those? If so, if I'm trying to return these 'updates', how would I be able to generate a list of all of the most recent relationships when they're all of different kinds (like :FOLLOWS, :LIST_HAS_MOVIE, etc.?
Essentially I want to say:
- User A is following User B
- User A is following List 7
- Return for User A when User B follows another user or List 7 has a movie or show added to it
I can't figure out the structure that might make sense here. Happy to clear anything up.
1 Like
These updates you want your users to make (someone followed someone, or someone added things to a list) -- these updates you are currently modeling as relationships, but your use case would probably benefit by "Reifying" these relationships into proper nodes.
Instead of:
(:User)-[:FOLLOWED { when: datetime() }]->(:User)
(:User)-[:CREATED { when: datetime() }]->(:Post)
Think about this:
(:User)-[:INITIATED]->(:Action { type: 'post', when: datetime() })-[:ON]->(:Post)
(:User)-[:INITIATED]->(:Action { type: 'follow', when: datetime() })-[:ON]->(:User)
Now how do you build a feed? Dead simple.
MATCH (u:User)-[:INITIATED]->(a:Action)-[]->(t:Target)
RETURN a.type, labels(t), a.when
ORDER BY a.when DESC
For much more information on why this might make sense, check this article, the "Reifying relationships" heading Graph Data Modeling: All About Relationships | by David Allen | Neo4j Developer Blog | Medium
Hi @david_allen this sounds really interesting, but I'm curious how it might affect my other graphQL queries.
Say I'm just looking for followers, right now I just look for type User and call followers which is a list of Users with the :FOLLOWS relationship. It feels like direct relationships between Users and the other nodes could be stronger although I see the benefits of your approach. Definitely has me thinking.