Skip to main content

Example: Container of Items

Creating the Models

First, create the SDL for the first model to be combined

type Ball @createModel(accountRelation: LIST, description: "A ball to display") {
creator: DID! @accountReference
red: Int
green: Int
blue: Int
radius: Float
}

You will then save this to a file, such as ball.graphql. You can then add the model and get the id.

composedb composite:create --output ball.json ball.graphql cat ball.json | jq '.models | keys_unsorted[0]'

Now we need a second model that will combine with the first model

type Obstacle @createModel(accountRelation: LIST, description: "An obstacle a ball can collide with") {
creator: DID! @accountReference
x: Int
y: Int
z: Int
length: Int
width: Int
height: Int
}

Next, we're going to combine the existing models into a new model

type Ball @loadModel(id: "<id of ball>") {
id: ID!
}

type Obstacle @loadModel(id: "<id of obstacle>"){
id: ID!
}

type Collision @createModel(accountRelation: LIST, description: "Collision between ball and object") {
ballID: StreamID! @documentReference(model: "Ball")
ball: Ball! @relationDocument(property: "ballID")
obstacleID: StreamID! @documentReference(model: "Obstacle")
obstacle: Ball! @relationDocument(property: "ballID")
x: Int
y: Int
z: Int
}

Save this to a file and add as above.

We can now merge all of these and deploy them as a composite.

composedb composite:merge ball.json obstacle.json collision.json --output=merged.json composedb composite:deploy merged.json composedb composite:compile merged.json runtime.json

Our composite is now ready to use. We can use it with graphiql

composedb graphql:server --graphiql runtime.json

Inserting Data

We can create an item with mutation

mutation CreateNewBall($i: CreateBallInput!){
createBall(input: $i){
document {
id
radius
}
}
}

and variables

{
"i": {
"content": {
"creator": "<some did key>",
"radius": 45,
"red": 10,
"green": 20,
"blue": 30
}
}
}

We can create a second object with a mutation

mutation CreateNewObstacle($i: CreateObstacleInput!){
createObstacle(input: $i){
document {
id
}
}
}

and variables

{
"i": {
"content": {
"creator": "<some did key>"
"x": 1
"y": 2
"z": 3
"length": 4
"width": 5
"height": 6
}
}
}

Finally we can define the resultant object from combining items

mutation CreateCollision($i: CreateCollisionInput!){
createCollision(input: $i){
document {
id
}
}
}

and variables

{
"i": {
"content": {
"ballID": "<id from ball mutation>",
"obstacleID": "<id from obstacle mutation>"
}
}
}

Query The Data

We can query for the combined item

query {
collisionIndex(first:5) {
edges {
node {
id
ball {
id
radius
}
obstacle {
id
}
}
}
}
}