GraphQL Cookbook

Getting an object through the Parse GraphQL API

Problem

You want to get an existing object from your database through the Parse GraphQL API.

Solution

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

  • Using generic query - this is the query that you can use to get an object of any class.
  • Using class query - this is the recommended query that you should use to get an object of a specific 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 query

When you use the get generic query, Parse Server behaves like a schemaless database. It means that you do not need to specify which object’s fields you want to get. You just need to send the object’s className and objectId, and Parse Server will return all fields of this object.

Therefore, the objects’ get generic query is the query that you can use for getting an existing object of any class. If you want to get an existing object of a specific class, we recommend using the class query.

Example Parse 3.7.2:

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
query GetObject {
  objects {
    get(className: "Hero", objectId: "ffyOBOTk85")
  }
}
Result Parse 3.7.2:
1
2
3
4
5
6
7
8
9
10
11
12
{
  "data": {
    "objects": {
      "get": {
        "objectId": "ffyOBOTk85",
        "name": "Luke Skywalker",
        "createdAt": "2019-07-15T01:25:20.875Z",
        "updatedAt": "2019-07-15T01:25:20.875Z"
      }
    }
  }
}
Example Parse 3.8:

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
query GetObject {
  get(className: "Hero", objectId: "rR8jmFRnkS")
}
Result Parse 3.8:
1
2
3
4
5
6
7
8
9
10
{
  "data": {
    "get": {
      "objectId": "rR8jmFRnkS",
      "name": "Luke Skywalker",
      "createdAt": "2019-11-04T12:42:40.723Z",
      "updatedAt": "2019-11-04T12:42:40.723Z"
    }
  }
}
Example Parse 3.9 and 3.10:

Parse 3.9 and Parse 3.10 does not have the generic methods GET and FIND. You must use the specific methods below to retrieve objects.

Using class query

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 get<ClassName> query to get an existing object of this class.

Therefore, the object’s class query is the recommended method for getting an existing object of a specific class. Since this query knows your class’ data, it will automatically make available for you additional features like code auto-complete and validation.

Example Parse 3.7.2:

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

1
2
3
4
5
6
7
8
9
10
query GetHero {
  objects {
    getHero(objectId: "ffyOBOTk85") {
      objectId,
      name,
      createdAt,
      updatedAt
    }
  }
}
Result Parse 3.7.2:
1
2
3
4
5
6
7
8
9
10
11
12
{
  "data": {
    "objects": {
      "getHero": {
        "objectId": "ffyOBOTk85",
        "name": "Luke Skywalker",
        "createdAt": "2019-07-15T01:25:20.875Z",
        "updatedAt": "2019-07-15T01:25:20.875Z"
      }
    }
  }
}
Example Parse 3.8:

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

1
2
3
4
5
6
7
8
query GetHero {
  hero(objectId: "rR8jmFRnkS") {
    objectId,
    name,
    createdAt,
    updatedAt
  }
}
Result Parse 3.8:
1
2
3
4
5
6
7
8
9
10
{
  "data": {
    "hero": {
      "objectId": "rR8jmFRnkS",
      "name": "Luke Skywalker",
      "createdAt": "2019-11-04T12:42:40.723Z",
      "updatedAt": "2019-11-04T12:42:40.723Z"
    }
  }
}
Example Parse 3.9:

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

1
2
3
4
5
6
7
8
query GetHero {
  hero(id: "CkhurmMjZW") {
    id,
    name,
    createdAt,
    updatedAt
  }
}
Result Parse 3.9:
1
2
3
4
5
6
7
8
9
10
{
  "data": {
    "hero": {
      "id": "CkhurmMjZW",
      "name": "Luke Skywalker",
      "createdAt": "2019-11-04T12:37:22.462Z",
      "updatedAt": "2019-11-04T12:37:22.462Z"
    }
  }
}
Example Parse 3.10:

This example will only work if you use a class’ query and objectId or id of an existing object. You can create an object using the creating an object recipe.

1
2
3
4
5
6
7
8
query GetHero {
  hero(id: "SGVybzpVRm5TVDM1YnBp") {
    id,
    name,
    createdAt,
    updatedAt
  }
}
Result Parse 3.10:
1
2
3
4
5
6
7
8
9
10
{
  "data": {
    "hero": {
      "id": "SGVybzpVRm5TVDM1YnBp",
      "name": "R2-D2",
      "createdAt": "2020-02-06T13:13:26.678Z",
      "updatedAt": "2020-02-06T13:13:26.678Z"
    }
  }
}