dehaze

Insert

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

Insert a single objectlink

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 mutationlink

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
      }
    }
  }
}

Insert an object with a JSON fieldlink

Example: Inserting a pokemon with stats (a JSON field):

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

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

Have a technical question?

Improve the docs!