Cedalio
Search
K

Relations

Cedalio simplifies the management of relationships between models by automatically generating the necessary types, connections, and input types based on your schema configuration.

Relations

Cedalio supports the following types of relationships:
  • One to One
  • One to Many / Many to One
  • Many to Many

One to One

The example below shows the One to One relation between User and Profile.
Example Schema:
type User @model {
name: String
profile: Profile
}
type Profile @model {
bio: String
user: User
}

One to Many / Many to One

The example below shows the One to Many relation between Category and Post.
Example Schema:
type Category @model {
name: String
posts: [Post]
}
type Post @model {
title: String
category: Category
}

Many to Many

The example below shows the Many to Many relation between Category and Posts.
A Post has many Category entries, and each Category has many Posts.
Example Schema:
type Category @model {
name: String
posts: [Post]
}
type Post @model {
title: String
category: [Category]
}

Linking

Cedalio automatically generates input types for mutations that allow you to link and unlink models.
Link
Unlink
Schema
mutation {
postCreate(input: { title: "Hello Grafbase", category: { link: "..." } }) {
post {
title
category {
name
}
}
}
}
mutation {
postUpdate(by: { id: "..." }, input: { category: { unlink: "..." } }) {
post {
title
category {
id
}
}
}
}
type Post @model {
title: String!
category: Category
}
type Category @model {
name: String!
posts: [Post]
}

Nested Mutations

Cedalio automatically generates mutations that allow you to create child nodes at the time of creating the parent.
Create new category
Schema
mutation {
postCreate(
input: {
title: "Hello Cedalio"
category: { create: { name: "Announcements" } }
}
) {
post {
title
category {
name
}
}
}
}
type Post @model {
title: String!
category: Category
}
type Category {
name: String!
posts: [Post]
}
Non-nullable Relations
Cedalio supports defining relations as non-nullable, ensuring that certain fields must have values.
Example Schema with non-nullable Relations:
type Category @model {
name: String
posts: [Post!]!
}
type Post @model {
title: String
category: Category!
}

Naming Relations

You can provide custom names for relations when referencing the same model multiple times using the @relation directive.
type Order @model {
billingAddress: Address @relation(name: "billing")
shippingAddress: Address @relation(name: "shipping")
}
type Address @model {
line1: String!
line2: String
zip: String
phone: PhoneNumber
}