GraphQL Entity Relationships
One-To-One (1:1) Relationships
In One-To-One relation, one entity instance is related to only one instance of another entity. One side of the relationship should always derive.
Database tables:
One-To-Many (1:n) Relationships
In One-To-Many relation, one entity instance is related to multiple instance of the other entity.
Database table for the Post
entity:
The only difference between 1:1
and 1:n
is the unique constraint that 1:1
has.
Many-To-Many (n:n) Relationships
Many-To-Many is a relationship where one entity instance is related to many instance of other entity and vice-versa. In this relationship one side of the relation must derive.
A junction table is created for n:n relationship.
Database tables:
Reverse Lookups
Defining reverse lookups on an entity allows you to query other side of the relation. Use @derivedFrom
directive to add reverse lookup to an entity.
Example If we want to access a user's posts
from the user entity we should add a derived field to User
entity:
Relationships In Mappings
Each GraphQL entity has a corresponding typeorm entity and we use these entities to perform CRUD operations.
Example
We will create a new post for an existing user:
Last updated