dehaze

Insert

An insert request only requires the docs argument, which is nothing but the array of objects to be inserted.

Insert a single object

Example: Let’s say you have caught a Pikachu! This is how you would add it to your list of caught pokemons:

mutation {
  insert_caught_pokemons(
    docs: [
      {id: "1", name: "Pikachu", combat_power: 500}
    ]
  ) @postgres {
    status
    error
    returning {
      id
      name
    }
  }
}
const docToBeInserted = {id: "1", name: "Pikachu", combat_power: 500}

const { status } = await db.insert("caught_pokemons")
  .doc(docToBeInserted)
  .apply()

The result of the above mutation is as follows:

{
  "data": {
    "insert_caught_pokemons": {
      "returning": [
        {
           "id": "1",
           "name": "Pikachu"
        }
      ],
      "status": 200
    }
  }
}

Insert multiple objects of the same type in the same mutation

Example: Add 3 items received from Professor Oak to your list of items:

mutation {
  insert_items(
    docs: [
      {id: "1", type: "Pokeball"},
      {id: "2", type: "Potion"},
      {id: "3", type: "Antidote"}
    ]
  ) @postgres {
    status
    error
    returning {
      id
      type
    }
  }
}
const docsToBeInserted = [
  {id: "1", type: "Pokeball"},
  {id: "2", type: "Potion"},
  {id: "3", type: "Antidote"}
]

const { status } = await db.insert("items")
  .docs(docsToBeInserted)
  .apply()

Let’s say you have linked the pokemon table to the pokemons field of the trainer table via the @link directive. Then you can insert a trainer along with their pokemons:

mutation {
  insert_trainer(
    docs: [
      {
        id: "ash", 
        name: "Ash", 
        city: "Pallete Town",
        pokemons: [
          {
            id: "1",
            name: "Pikachu",
            combat_power: 200
          },
          {
            id: "2",
            name: "Charmender",
            combat_power: 150
          }
        ]
      }
    ]
  ) @postgres {
    status
    error
    returning {
      id
      name
      city
      pokemons {
        id
        name
        combat_power
      }
    }
  }
}

Have a technical question?

Improve the docs!