I am a bit confused. When I create a relation (one...
# prisma-whats-new
s
I am a bit confused. When I create a relation (one-to-many) why is there need to create fields in both "types". Coming from an SQL world, I expect them to relate with the ID key.
b
@shubhamjain Same as in SQL you can also declare a relation only from one side 🙂
'(...) Most of the time, the same @relation directive appears twice in a schema file, to denote both sides of the relation' ( https://www.graph.cool/docs/reference/schema/directives-aeph6oyeez/#relation-fields ) So depending on whether it's a 1:1, 1:n or n:n relationship you can denote it one sided, just like in SQL.
n
Hey, there's actually more to it than that
Let me clarify
Let's say we have a one-to-many relation between
Post
and
Comment
because GraphQL allows hierarchical queries, it's actually best practice to expose "inline" access to related nodes instead of only the id
so to make this query possible
Copy code
query {
  allPosts {
    id
    comments {
      id
    }
  }
}
if we only had an comment id field on
Post
, this query would not be possible
s
Oh.. that makes sense.
✅ 1
n
circling back to what @busk said:
b
Ah, I learned something new as well. Thanks a lot @nilan! 🙂
s
So that allows us to query any attribute of comment right?
Copy code
query {
  allPosts {
    id
    comments {
      id
      title
    }
  }
}
n
the section you quoted actually tries to convey a different thing
yes @shubhamjain! Sorry should have made this more clear
that's one of the biggest paradigm shifts that GraphQL brings with it
the language might be more complicated than necessary...
but feel free to add/remove fields to the query on the left side, execute it and see what happens with the result 🙂
@busk about what you posted:
relations are typically between two different types
sometimes they are on the same type (we call that a self-relation)
and yet again sometimes, it's not only on the same type, but also on the same FIELD
a typical example would be a
User
type with the
friends: [User!]! @relation(name: "Friendship")
relational field
that's a many-to-many. it can also be a one-to-one, for example a
spouse: User! @relation(name: "Spouses")
only in these two cases, the
@relation
tag appears only once 🙂
b
@nilan can I also store info "inside the relation", same as in SQL? For example whether a friendship has already been accepted?
n
that's a great question!
if you want to do that, you could add a "meta type" in between the two related types that is able to store meta data
I actually spoke about this topic in detail together with @feramon in a recent webinar:

https://www.youtube.com/watch?v=JWl_ga3Ohvw▾

b
thanks, I'll take a look at that webinar! 🙂