0
votes

So I'm trying to construct a mutation for inserting/updating a record for a "Person" along with it's address, email, telephones information into multiple tables, using variables.

mutation insertPerson ($address: [adr_insert_input!]!, $emails: [emails_insert_input!]!) {
  insert_info(objects: [{
    f_name: "User1",
    l_name: "Test"
    address: {
        data: $address,
        on_conflict: {
          constraint: person_id_pk,
          update_column: [add_text_line1, zip_code]
        }
    },
    emails: {
      data: $emails,
      on_conflict: {
          constraint: person_id_pk,
          update_column: [email_text]
        }
    }
  }], on_conflict: {
    constraints: person_pk,
    update_columns: [f_name, l_name]
  }) {
   affected_rows
  }
}

and my variables are set-up as follows...

{
  "address": [{
    "add_text_line1": "123 Main Street",
    "zip_code": 50501
  }],
  "emails": [{
     "email_text": "[email protected]"
  }]
}

This works as expected for me (with multiple values in emails array too), but I need to move the f_name & l_name values (whole Person object) into a variable as well. How do I achieve that?

I tried the below mutation this way, but this resulted into two separate inserts & empty values being passed...

mutation insertPerson ($person: person_insert_input!, $address: [adr_insert_input!]!){
  insertData(objects: [
    $person,
    {
      address: { data: $address }
    }
  ]) { affected_rows }
}

This resulted to a two separate insertions... First person with empty address, then empty person with address.

How do I achieve the first mutation's result, while using Person Info as part of variables NOT hard-code it into the query itself?

Thank you!

1

1 Answers

1
votes

You will need to pass the insert_info objects as the variables

mutation insertPerson ($info_objects: [insert_info_insert_input!]!) {
  insert_info(objects: $info_objects, on_conflict: {
    constraints: person_pk,
    update_columns: [f_name, l_name]
  }) {
   affected_rows
  }
}

And your variables will be an array of the info_objects

{info_objects: [{
    f_name: "User1",
    l_name: "Test"
    address:  {data: [{
    "add_text_line1": "123 Main Street",
    "zip_code": 50501
  }]},  
        on_conflict: {
          constraint: person_id_pk,
          update_column: [add_text_line1, zip_code]
        }
    },
    emails: {
      data: [{
     email_text: "[email protected]"
  }],
      on_conflict: {
          constraint: person_id_pk,
          update_column: [email_text]
        }
    }
  }]}