hey guys, I have what is basically a nested data s...
# prisma-whats-new
h
hey guys, I have what is basically a nested data structure that I would like to represent normalised. This is why I’d like to create a relationship between two fields in the same type. Let me know if this is doable or if there’s any alternative approach
Copy code
type Sample {
  id: ID! @unique
  graph: Graph!
  content: String!
  parent: Sample! @relation(name: "IsThisCrazy")
  children: [Sample!]! @relation(name: "IsThisCrazy")
}
r
Hi... I have a project that makes heavy use of graph data like this
we created a Relation model that connects 2 Nodes
Copy code
type AssetNode @model {
  id: ID! @isUnique
  dependencies: [AssetRelation!]! @relation(name: "NodeAOnAssetRelation")
  dependents: [AssetRelation!]! @relation(name: "NodeBOnAssetRelation")
}

type AssetRelation @model {
  id: ID! @isUnique
  nodeA: AssetNode @relation(name: "NodeAOnAssetRelation")
  nodeB: AssetNode @relation(name: "NodeBOnAssetRelation")
}
it makes it very convenient to query the whole graph like this:
Copy code
allAssetRelations {
    dependencies { nodeB { id } }
    dependents { nodeA { id } }
  }
I'm not sure if your direct parent/children relations will work or not. I definitely thought about doing it that way, but went with modeling the relation/edge itself because we need to store other data on it
hth
h
hey, thanks for the quick reply
that’s an interesting approach that I need to better understand
r
yeah, I'm happy to talk more about it... it took awhile to get to this point, but having great success with it
its still mindbending sometimes with the queries and traversing the graph
h
before moving to prisma, I was doing something in the
children()
resolver
like mapping over the ids and returning the actual node data
yeah, it’s a bit hard to understand for me 🙂
r
what are you trying to model, ultimately?
h
a graph structure 🙂
r
then I recommend modelling the nodes and the edges as separate models, it will make it sane
something like this
but how do I know which are the nodes at the edges?
r
so each edge (I call AssetRelation in my model), has two endpoints nodeA and nodeB. In my project it is dependencies, so nodeB is a dependency of nodeA
i guess that is a subtlety, maybe. Relations are directed in my project
h
oh, I see what you mean by edges, I was confusing them with leaf nodes 🙄
r
right, nodes boxes, edges lines 🙂
the nice thing about having edges as a separate model is you can delete that edge and then you have just partitioned your graph
or you can unlink one node and link another to manipulate the tree
h
😐
that is awesome indeed
r
which looks like the kind of thing you might want to do in your project (i am guessing from the drawing)
h
aye, that would be awesome to have
alright, I’ll give it a shot
r
awesome, good luck! ping me if you have any q's, happy to talk more
h
awesome, thank you very much 🙂