GraphQL Cookbook

Finding objects through the Parse GraphQL API

Problem

You want to find objects from your database through the Parse GraphQL API.

Solution

Using the parse GraphQL, there are two different ways to find objects from your database:

  • Using generic query - this is the query that you can use to find objects of any class.
  • Using class query - this is the recommended query that you should use to find objects 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 find 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 retrieve. You just need to send the object’s className, and Parse Server will return all fields of the found objects.

Therefore, the objects’ find generic query is the query that you can use for finding objects of any class. If you want to find objects of a specific class, we recommend using the class query.

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

1
2
3
4
5
6
query FindObject {
  find(className: "Hero") {
  count,
  results      
  }
}
Result Parse 3.8.0:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
{
  "data": {
    "find": {
      "count": 2,
      "results": [
        {
          "objectId": "rR8jmFRnkS",
          "name": "Luke Skywalker",
          "createdAt": "2019-11-04T12:42:40.723Z",
          "updatedAt": "2019-11-04T12:42:40.723Z"
        },
        {
          "objectId": "tUEcddcgno",
          "name": "R2-D2",
          "createdAt": "2019-11-04T12:44:10.951Z",
          "updatedAt": "2019-11-04T12:44:10.951Z"
        }
      ]
    }
  }
}

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

1
2
3
4
5
6
7
8
query FindObject {
  objects {
    find(className: "Hero") {
      count,
      results      
    }
  }
}
Result Parse 3.7.2:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
{
  "data": {
    "objects": {
      "find": {
        "count": 2,
        "results": [
          {
            "objectId": "ffyOBOTk85",
            "name": "Luke Skywalker",
            "createdAt": "2019-07-15T01:25:20.875Z",
            "updatedAt": "2019-07-15T01:25:20.875Z"
          },
          {
            "objectId": "jJH0aQQjfs",
            "name": "R2-D2",
            "createdAt": "2019-07-15T02:22:04.982Z",
            "updatedAt": "2019-07-15T02:22:04.982Z"
          }
        ]
      }
    }
  }
}
Example 3.9.0 and later:

Parse 3.9.0 and later 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 find<ClassName> query to find objects of this class.

Therefore, the object’s class query is the recommended method for finding objects 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.

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

1
2
3
4
5
6
7
8
9
10
11
12
query FindHero {
  heroes{
    count,
    edges{
      node{
        name
        createdAt
        updatedAt
      }
    }
  }
}
Result Parse 3.10.0 and later:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
{
  "data": {
    "heroes": {
      "count": 3,
      "edges": [
        {
          "node": {
            "name": "Luke Skywalker",
            "createdAt": "2020-02-06T13:02:33.652Z",
            "updatedAt": "2020-02-06T13:02:33.652Z"
          }
        },
        {
          "node": {
            "name": "R2-D2",
            "createdAt": "2020-02-06T13:13:26.678Z",
            "updatedAt": "2020-02-06T13:13:26.678Z"
          }
        }
      ]
    }
  }
}

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

1
2
3
4
5
6
7
8
9
10
11
query FindHero {
  heroes{
    count,
    results {
      id,
      name,
      createdAt,
      updatedAt
    }
  }
}
Result Parse 3.9.0:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
{
  "data": {
    "heroes": {
      "count": 2,
      "results": [
        {
          "id": "CkhurmMjZW",
          "name": "Luke Skywalker",
          "createdAt": "2019-11-04T12:37:22.462Z",
          "updatedAt": "2019-11-04T12:37:22.462Z"
        },
        {
          "id": "n5GrpEi0iL",
          "name": "R2-D2",
          "createdAt": "2019-11-04T12:45:00.882Z",
          "updatedAt": "2019-11-04T12:45:00.882Z"
        }
      ]
    }
  }
}

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

1
2
3
4
5
6
7
8
9
10
11
query FindHero {
  heroes{
    count,
    results {
      objectId,
      name,
      createdAt,
      updatedAt
    }
  }
}
Result Parse 3.8.0:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
"data": {
    "objects": {
      "findHero": {
        "count": 2,
        "results": [
          {
            "objectId": "ffyOBOTk85",
            "name": "Luke Skywalker",
            "createdAt": "2019-07-15T01:25:20.875Z",
            "updatedAt": "2019-07-15T01:25:20.875Z"
          },
          {
            "objectId": "jJH0aQQjfs",
            "name": "R2-D2",
            "createdAt": "2019-07-15T02:22:04.982Z",
            "updatedAt": "2019-07-15T02:22:04.982Z"
          }
        ]
      }
    }
  }
}

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

1
2
3
4
5
6
7
8
9
10
11
12
13
query FindHero {
  objects {
    findHero {
      count,
      results {
        objectId,
        name,
        createdAt,
        updatedAt
      }
    }
  }
}
Result Parse 3.7.2:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
"data": {
    "objects": {
      "findHero": {
        "count": 2,
        "results": [
          {
            "objectId": "ffyOBOTk85",
            "name": "Luke Skywalker",
            "createdAt": "2019-07-15T01:25:20.875Z",
            "updatedAt": "2019-07-15T01:25:20.875Z"
          },
          {
            "objectId": "jJH0aQQjfs",
            "name": "R2-D2",
            "createdAt": "2019-07-15T02:22:04.982Z",
            "updatedAt": "2019-07-15T02:22:04.982Z"
          }
        ]
      }
    }
  }
}