GraphQL Cookbook

Updating an object through the Parse GraphQL API

Problem

You want to update an existing object in your database through the Parse GraphQL API.

Solution

Using the parse GraphQL, there are two different ways to update an existing object in your database:

  • Using generic mutation - this is the mutation that you must use if you want to set fields that do not belong to your object’s class yet.
  • Using class mutation - this is the recommended mutation if your object’s class already has all fields that you want to update.

Using generic mutation

When you use the update generic mutation, Parse Server behaves like a schemaless database. It means that you do not need to define all fields of your object beforehand. You just need to send the fields that you want to update, and Parse Server will not only store it, but also learn from it, and automatically create any new field in this object’s class.

Therefore, the objects’ update generic mutation is the method that you must use for updating an existing object if you want to set fields that do not belong to your object’s class yet. You can actually use this mutation for updating any existing object, but we recommend using the class mutation if your object’s class already has all fields that you want to update.

Example:

This example will only work if you use a className and an objectId of an existing object. You can create an object using the creating an object recipe.

1
2
3
4
5
6
7
mutation UpdateObject {
  objects {
    update(className: "Hero", objectId: "ffyOBOTk85", fields: { height: 5.6 }) {
      updatedAt
    }
  }
}
Result:
1
2
3
4
5
6
7
8
9
{
  "data": {
    "objects": {
      "update": {
        "updatedAt": "2019-07-15T05:57:14.416Z"
      }
    }
  }
}

Using class mutation

Once you have already created your object’s class in your application’s schema (for instance, using the creating an object recipe), Parse Server instantly adds to your GraphQL API a new update<ClassName> mutation to update an existing object of this class.

Therefore, the object’s class mutation is the recommended method for updating an existing object if your object’s class already has all fields that you want to update. Since this mutation knows your class’ data, it will automatically make available for you additional features like code auto-complete and validation. You also don’t need to specify the data types when sending dates, pointers, relations, files, geo points, polygons, or bytes through the class update mutation.

Example:

This example will only work if you use a class’ mutation and objectId of an existing object. You can create an object using the creating an object recipe. The object’s class must have all fields that you are trying to update. You can create new fields using the generic mutation.

1
2
3
4
5
6
7
mutation UpdateHero {
  objects {
    updateHero(objectId: "jJH0aQQjfs", fields: { height: 3.6 }) {
      updatedAt
    }
  }
}
Result:
1
2
3
4
5
6
7
8
9
{
  "data": {
    "objects": {
      "updateHero": {
        "updatedAt": "2019-07-15T05:51:25.572Z"
      }
    }
  }
}