Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.
03-09-2022 08:36 AM
Hi I have a Neo4j graph where User
s can create Post
s and can also follow Movie
s or Show
s or add these things to List
s.
With posts I can get the most recent Post
s by User
s followed other User
s 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:
I can't figure out the structure that might make sense here. Happy to clear anything up.
03-10-2022 11:11 AM
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
03-10-2022 06:43 PM
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 User
s 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.
All the sessions of the conference are now available online