Skip to content

Instantly share code, notes, and snippets.

@tovbinm
Last active March 6, 2022 14:38
Show Gist options
  • Save tovbinm/f76bcbf56ea8e2e3740e237b6c2f2ab9 to your computer and use it in GitHub Desktop.
Save tovbinm/f76bcbf56ea8e2e3740e237b6c2f2ab9 to your computer and use it in GitHub Desktop.
FaunaDB Relations: GraphQL schemas, mutations and resulting documents
****************************************************************************
**** FaunaDB Relations: GraphQL schemas, mutations and resulting documents *
****************************************************************************
**** One to One Relation ***************************************************
SCHEMA:
type User { name: String! car: Car }
type Car { plate: String! owner: User }
MUTATION:
mutation Create {
createUser(data: {
name: "John"
car: { create: { plate: "123" } }
}) { _id }
}
DATA:
User: 256744630919365138 - { "name": "John", "car": Ref(Collection("Car"), "256744630915170834") }
Car: 256744630915170834 - { "plate": "123" }
****** !!! IMPORTANT !!! **********************************
For 1-to-1 relations the relation has to be created
based on the lexicographical ordering of the document type
***********************************************************
**** One to Many Relation **************************************************
SCHEMA:
type User { name: String! cars: [Car!] @relation }
type Car { plate: String! owner: User! }
MUTATION:
mutation Create {
createUser(data: {
name: "John"
cars: { create: [ { plate: "123" }, { plate: "456" } ] }
}) { _id }
}
DATA:
User: 256744630919365138 - { "name": "John" }
Car: 256745717407678996 - { "plate": "123", "owner": Ref(Collection("User"), "256745717403484692") }
Car: 256745717413970452 - { "plate": "456", "owner": Ref(Collection("User"), "256745717403484692") }
**** Many to One Relation **************************************************
SCHEMA:
type User { name: String! }
type Car { plate: String! owner: User! }
MUTATION:
mutation Create {
createUser(data: { name: "John" }) { _id }
}
mutation Create {
createCar(data: {
plate: "123"
owner: { connect: "256747323268268564" }
}) { _id }
}
mutation Create {
createCar(data: {
plate: "456"
owner: { connect: "256747323268268564" }
}) { _id }
}
DATA:
User: 256747323268268564 - { "name": "John" }
Car: 256747403892228628 - { "plate": "123", "owner": Ref(Collection("User"), "256747323268268564") }
Car: 256747411646448148 - { "plate": "456", "owner": Ref(Collection("User"), "256747323268268564") }
**** Many to Many Relation *************************************************
SCHEMA:
type User { name: String! drives: [Car!] @relation }
type Car { plate: String! drivers: [User!] @relation }
MUTATION:
mutation Create {
createUser(data: { name: "John"
drives: {
create: [ { plate: "123" }, { plate: "456" }]
}
}){ _id }
}
mutation Create {
createUser(data: { name: "Emily"
drives: { connect: 256750932898873875 }
}){ _id }
}
DATA:
User: 256750932893630995 - { "name": "John" }
User: 256751020079579668 - { "name": "Emily" }
Car: 256750932898873875 - { "plate": "123" }
Car: 256750932907262483 - { "plate": "456" }
car_drivers: 256750932900971027 - {
"userID": Ref(Collection("User"), "256750932893630995"),
"carID": Ref(Collection("Car"), "256750932898873875")
}
car_drivers: 256750932908311059 - {
"userID": Ref(Collection("User"), "256750932893630995"),
"carID": Ref(Collection("Car"), "256750932907262483")
}
car_drivers: 256751020083773972 - {
"userID": Ref(Collection("User"), "256751020079579668"),
"carID": Ref(Collection("Car"), "256750932898873875")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment