Search code examples
graphqlhasura

how to insert data into two related tables


I have this json header/detail that I send to hassura graphql. I would like to use variables as objects and arrays of objects to organize the code.

mutation insertData(
  $presidente: bigint,
  $vocal1: bigint,
  $vocal2: bigint,
  $ano: Int,
  $libro: Int,
  $folio: Int,
  $ffolio: Int,
  $fecha: date,
  $c_alumno_p_f: Int,
  $institucion: bigint,
  $id_carrera: bigint,
  $id_mesa: bigint,
  $id_materia: bigint,
  $detalle : [detalle_acta_regulares_insert_input!]!
){
  insert_actas_regulares(
    objects:[
      {
        presidente: $presidente, 
        vocal1: $vocal1, 
        vocal2: $vocal2, 
        ano: $ano, 
        libro: $libro, 
        folio: $folio, 
        ffolio: $ffolio, 
        fecha: $fecha, 
        c_alumno_p_f: $c_alumno_p_f, 
        institucion: $institucion, 
        id_carrera: $id_carrera, 
        id_mesa: $id_mesa, 
        id_materia: $id_materia,
        detalle_acta_regulares:{
          data:
           $detalle
        }
      }
    ]
  ){
    affected_rows
  }
}

The variables I use are these: 

{
  "presidente": 107, 
  "vocal1": 196, 
  "vocal2": 208, 
  "ano": 2022, 
  "libro": 2, 
  "folio": 1, 
  "ffolio": 2, 
  "fecha": "2022-11-07", 
  "c_alumno_p_f": 3, 
  "institucion": 17, 
  "id_carrera": 5, 
  "id_mesa": 40863, 
  "id_materia": 11347,
  "detalle": [
    {
    "id_alumno": 2186,
    "escrito": 4,
    "oral":0,
    "definitivo": 4
  },
    {
      "id_alumno": 9869,
      "escrito": 8,
      "oral":0,
      "definitivo": 8
    }
  ]
}

How should I structure the query to send the header as an object too?

I read the documentation, but I don't understand how to mount the structures


Solution

  • It is usually much easier to simplify your mutation and make use of a single variable.

    There are definitely typos, but you get the idea:

      mutation insertData($objects: [actas_regulares_insert_input!]!) {
        insert_actas_regulares(objects: $objects) {
          affected_rows
        }
      }
    

    And this is your variable payload:

    {
      "objects": [{
        "presidente": 107,
        "vocal1": 196,
        "vocal2": 208,
        "ano": 2022,
        "libro": 2,
        "folio": 1,
        "ffolio": 2,
        "fecha": "2022-11-07",
        "c_alumno_p_f": 3,
        "institucion": 17,
        "id_carrera": 5,
        "id_mesa": 40863,
        "id_materia": 11347,
        "detalle_acta_regulares": {
          "data": [
            {
              "id_alumno": 2186,
              "escrito": 4,
              "oral": 0,
              "definitivo": 4
            },
            {
              "id_alumno": 9869,
              "escrito": 8,
              "oral": 0,
              "definitivo": 8
            }
          ]
        }
      }]
    }
    

    I hope that helps!