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.

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.

Example:

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:
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"
          }
        ]
      }
    }
  }
}

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.

Example:

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:
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"
          }
        ]
      }
    }
  }
}