How to delete the objects with references

Hypi supports lightweight Referential Integrity on array fields. If there is an existing reference between two data types through a field, the records cannot be deleted without removing the reference.

clearArrayReferences parameter from the delete function is set to unlink two objects and remove the data. It is used to delete the objects with references.

Now, let’s see how to use the clearArrayReferences parameter with delete function.

We will work with the following schema. Here we are reusing data from ReadObject from the previous post. deleteArray is a data type that holds one to many references with ReadObject through fld1. fld1 is an array of type ReadObject.

type ReadObject {
   field1: String
   field2: Int
}

type deleteArray {
    fld1: [ReadObject]
}

Let’s create a deleteArray object. We have used hypi.id of the previously generated ReadObject to insert data in fld1. Check this post on how to create a reference between two objects using hypi.id

mutation {
  upsert(
    values: {
      deleteArray: [
        {
          fld1: [
            { hypi: { id: "01FH5MKT5P1F31Z0G1G64ASE7M" } }
            { hypi: { id: "01FH5MKT5XJZJBEHSMR3ZH6752" } }
          ]
        }
      ]
    }
  ) {
    id
  }
}
#result
{
  "data": {
    "upsert": [
      {
        "id": "01FHTAK5GD45R88RVR2GCBKB63"
      }
    ]
  }
}

Now if we try to delete this object without setting the clearArrayReferences parameter, it shows an error. As the reference exists between deleteArray and ReadObject, the record could not be deleted

mutation {
  delete(type: deleteArray, arcql: "hypi.id = '01FHTAK5GD45R88RVR2GCBKB63'")
}
#result
{
  "data": null,
  "errors": [
    {
      "message": "There are at least 2 references to the data being deleted. A sample of references found is included.",
      "extensions": {}
    },
    {
      "message": "deleteArray.fld1 links to ReadObject. Use unlink to remove the reference before deleting",
      "extensions": {}
    },
    {
      "message": "deleteArray.fld1 links to ReadObject. Use unlink to remove the reference before deleting",
      "extensions": {}
    },
    {
      "message": "Cannot return null for non-nullable type: 'Int' within parent 'Mutation' (/delete)",
      "path": [
        "delete"
      ]
    }
  ]
}

Setting parameter clearArrayReferences to true will remove the object.

mutation {
  delete(type: deleteArray, arcql: "hypi.id = '01FHTAK5GD45R88RVR2GCBKB63'",clearArrayReferences:true)
}
#result
{
  "data": {
    "delete": 1
  }
}

Setting clearArrayReferences to true only removes references to data in array fields as the name implies and it does not delete the referenced objects in the array. Only the object specified by the ‘arcql’ field will get deleted.

Check the POSTMAN collection for the delete requests in different programming languages! Click </> and choose the programming language of your choice.
Don’t forget to insert your own Authorization key and Hypi Domain under Headers to test the results!

Run in Postman