GraphQL Cookbook

Creating an object through the Parse GraphQL API

Problem

You want to create a new object in your database through the Parse GraphQL API.

Solution

Using the parse GraphQL, there are two different ways to create a new object in your database:

  • Using generic mutation - this is the mutation that you must use if you do not have already created your object’s class.
  • Using class mutation - this is the recommended mutation if you have already created your object’s class.

Version Information

Depending on the version of Parse you choose to run, the GraphQL queries, mutations and results will be slightly different.
Please choose the correct example along with the Parse version you are running.

Using generic mutation

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

Therefore, the objects’ create generic mutation is the method that you must use for creating a new object if you do not have already created your object’s class. You can also use this mutation for creating an object of pre-existing classes, but, for these cases, we recommend using the class mutation.

In Parse 3.10.0 and later you must first create the Class itself:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
mutation CreateClass {
  createClass(input:{
    name: "Hero"
    schemaFields: {
      addStrings: [{name: "name"}]
      addNumbers: [{name: "height"}]
    }
  }){
    class{
      schemaFields{
        name
        __typename
      }
    }
  }
}
Result Parse 3.10.0 and later Class:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
{
  "data": {
    "createClass": {
      "class": {
        "schemaFields": [
          {
            "name": "objectId",
            "__typename": "SchemaStringField"
          },
          {
            "name": "updatedAt",
            "__typename": "SchemaDateField"
          },
          {
            "name": "createdAt",
            "__typename": "SchemaDateField"
          },
          {
            "name": "name",
            "__typename": "SchemaStringField"
          },
          {
            "name": "height",
            "__typename": "SchemaNumberField"
          },
          {
            "name": "ACL",
            "__typename": "SchemaACLField"
          }
        ]
      }
    }
  }
}

And then create the Object:

1
2
3
4
5
6
7
8
mutation CreateObject{
  createHero(input: {fields: {name: "Luke Skywalker"}}){
    hero{
      id
      name
    }
  }
}
Result Parse 3.10.0 and later Object:

Notice the id property refers to the Global id in the Relay specification, not to confuse with the objectId from Parse.

1
2
3
4
5
6
7
8
9
10
{
  "data": {
    "createHero": {
      "hero": {
        "id": "SGVybzo5QjFPMUFxcXN1",
        "name": "Luke Skywalker"
      }
    }
  }
}

In Parse 3.9.0 you also must first create the Class itself:

1
2
3
4
5
6
7
8
9
10
11
12
13
mutation CreateClass {
  createClass(
    name: "Hero"
    schemaFields: {
      addStrings: [{name: "name"}]
      addNumbers: [{name: "height"}]
    }){
    schemaFields{
      name
      __typename
    }
  }
}
Result Parse 3.9.0 Class:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
{
  "data": {
    "createClass": {
      "schemaFields": [
        {
          "name": "objectId",
          "__typename": "SchemaStringField"
        },
        {
          "name": "updatedAt",
          "__typename": "SchemaDateField"
        },
        {
          "name": "createdAt",
          "__typename": "SchemaDateField"
        },
        {
          "name": "name",
          "__typename": "SchemaStringField"
        },
        {
          "name": "height",
          "__typename": "SchemaNumberField"
        },
        {
          "name": "ACL",
          "__typename": "SchemaACLField"
        }
      ]
    }
  }
}

And then create the Object:

In Parse 3.9.0 you must first create the Class itself:

1
2
3
4
5
6
7
8
mutation CreateObject{
  createHero(fields:{
    name: "Luke Skywalker"
  }){
    id
    createdAt
  }
}
Result Parse 3.9.0 Object:
1
2
3
4
5
6
7
8
{
  "data": {
    "createHero": {
      "id": "CkhurmMjZW",
      "createdAt": "2019-11-04T12:37:22.462Z"
    }
  }
}
1
2
3
4
5
6
7
8
mutation CreateObject {
  create(className: "Hero" fields:{
    name: "Luke Skywalker"
  }){
    objectId
    createdAt
  }
}
Result Parse 3.8.0:
1
2
3
4
5
6
7
8
9
10
{
  "data": {
    "objects": {
      "create": {
        "objectId": "ffyOBOTk85",
        "createdAt": "2019-07-15T01:25:20.875Z"
      }
    }
  }
}
1
2
3
4
5
6
7
8
mutation CreateObject {
  objects {
    create(className: "Hero", fields: { name: "Luke Skywalker" }) {
      objectId,
      createdAt
    }
  }
}
Result Parse 3.7.2:
1
2
3
4
5
6
7
8
9
10
{
  "data": {
    "objects": {
      "create": {
        "objectId": "Kr9aqnzfui",
        "createdAt": "2019-07-15T01:25:20.875Z"
      }
    }
  }
}

Using class mutation

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

Therefore, the object’s class mutation is the recommended method for creating a new object if you have already created your object’s class. 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 create mutation.

This example will only work if you have already created your object’s class. You can create a class using the generic mutation.

1
2
3
4
5
6
7
8
mutation CreateObject{
  createHero(input: {fields: {name: "R2-D2"}}){
    hero{
      id
      createdAt
    }
  }
}
Result 3.10.0 and later:

Notice the id property refers to the Global id in the Relay specification, not to confuse with the objectId from Parse.

1
2
3
4
5
6
7
8
9
10
{
  "data": {
    "createHero": {
      "hero": {
        "id": "SGVybzpVRm5TVDM1YnBp",
        "createdAt": "2020-02-06T13:13:26.678Z"
      }
    }
  }
}
1
2
3
4
5
6
7
8
mutation CreateObject{
  createHero(fields:{
    name: "R2-D2"
  }){
    id
    createdAt
  }
}
Result 3.9.0:
1
2
3
4
5
6
7
8
{
  "data": {
    "createHero": {
      "id": "n5GrpEi0iL",
      "createdAt": "2019-11-04T12:45:00.882Z"
    }
  }
}
1
2
3
4
5
6
mutation CreateHero {
  createHero(fields: { name: "R2-D2" }) {
    objectId,
    createdAt
  }
}
Result 3.8.0:
1
2
3
4
5
6
7
8
{
  "data": {
    "createHero": {
      "objectId": "tUEcddcgno",
      "createdAt": "2019-11-04T12:44:10.951Z"
    }
  }
}
1
2
3
4
5
6
7
8
mutation CreateHero {
  objects {
    createHero(fields: { name: "R2-D2" }) {
      objectId,
      createdAt
    }
  }
}
Result 3.7.2:
1
2
3
4
5
6
7
8
9
10
{
  "data": {
    "objects": {
      "createHero": {
        "objectId": "jJH0aQQjfs",
        "createdAt": "2019-07-15T02:22:04.982Z"
      }
    }
  }
}