React

Relational Query in React using Parse

Introduction

In this guide, you will perform relational queries in Parse and implement a React component using these queries. You will learn how to set up and query realistic data using Back4App and React.

Prerequisites

To complete this tutorial, you will need:

Goal

Query relational data stored on Back4App from a React App.

Step 1 - Understanding the Parse.Query class

Any Parse query operation uses the Parse.Query object type, which will help you retrieve specific data from your database throughout your app. It is crucial to know that a Parse.Query will only resolve after calling a retrieve method (like Parse.Query.find or Parse.Query.first), so a query can be set up and several modifiers can be chained before actually being called.

To create a new Parse.Query, you need to pass as a parameter the desired Parse.Object subclass, which is the one that will contain your query results. An example query can be seen below, in which a fictional Book subclass is being queried.

1
2
3
4
// This will create your query
let parseQuery = new Parse.Query("Book");
// The query will resolve only after calling this method
let queryResult = await parseQuery.find();

You can read more about the Parse.Query class here at the official documentation.

Step 2 - Save some data on Back4App

Let’s create an assortment of classes, which will be the target of our queries in this guide. The classes are: Author, Book, Publisher and BookStore, in which Book has a 1:N relation with Publisher and N:N with Author, and BookStore has an N:N relation with Book.

On Parse JS Console it is possible to run JavaScript code directly, querying and updating your application database contents using the JS SDK commands. Run the code below from your JS Console and insert the data on Back4App. Here is how the JS console looks like in your dashboard:

React Back4App

Go ahead and create the classes with the following example content:

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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
// Add objects and create tables
// Authors
const AuthorA = new Parse.Object('Author');
AuthorA.set('name', 'Aaron Writer');
await AuthorA.save();

const AuthorB = new Parse.Object('Author');
AuthorB.set('name', 'Beatrice Novelist');
await AuthorB.save();

const AuthorC = new Parse.Object('Author');
AuthorC.set('name', 'Casey Columnist');
await AuthorC.save();

// Publishers
const PublisherA = new Parse.Object('Publisher');
PublisherA.set('name', 'Acacia Publishings');
await PublisherA.save();

const PublisherB = new Parse.Object('Publisher');
PublisherB.set('name', 'Birch Distributions');
await PublisherB.save();

// Books
const BookA = new Parse.Object('Book');
BookA.set('title', 'A Love Story');
BookA.set('publisher', PublisherA);
BookA.set('publishingDate', new Date('05/07/1998'));
const BookARelation = BookA.relation("authors");
BookARelation.add(AuthorA);
await BookA.save();

const BookB = new Parse.Object('Book');
BookB.set('title', 'Benevolent Elves');
BookB.set('publisher', PublisherB);
BookB.set('publishingDate', new Date('11/31/2008'));
const BookBRelation = BookB.relation("authors");
BookBRelation.add(AuthorB);
await BookB.save();

const BookC = new Parse.Object('Book');
BookC.set('title', 'Can You Believe It?');
BookC.set('publisher', PublisherB);
BookC.set('publishingDate', new Date('08/21/2018'));
const BookCRelation = BookC.relation("authors");
BookCRelation.add(AuthorA);
BookCRelation.add(AuthorC);
await BookC.save();

// BookStore
const BookStoreA = new Parse.Object('BookStore');
BookStoreA.set('name', 'Books of Love');
const BookStoreARelation = BookStoreA.relation("books");
BookStoreARelation.add(BookA);
await BookStoreA.save();

const BookStoreB = new Parse.Object('BookStore');
BookStoreB.set('name', 'Fantasy Books');
const BookStoreBRelation = BookStoreB.relation("books");
BookStoreBRelation.add(BookB);
await BookStoreB.save();

const BookStoreC = new Parse.Object('BookStore');
BookStoreC.set('name', 'General Books');
const BookStoreCRelation = BookStoreC.relation("books");
BookStoreCRelation.add(BookA);
BookStoreCRelation.add(BookC);
await BookStoreC.save();

console.log('Success');

Step 3 - Query the data

Now that you have populated all the classes, we can now perform some relational queries in it. Let’s begin by filtering Book results by the publisher, searching for the ones that belong to the Publisher “Acacia Publishings” (or “Publisher A”) using the basic Parse.Query.equalTo method:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// Get PublisherA object
const PublisherAQuery = new Parse.Query('Publisher');
PublisherAQuery.equalTo('name', 'Acacia Publishings');
const PublisherA = await PublisherAQuery.first();

// Query Books with PublisherA
const bookQuery = new Parse.Query('Book');
bookQuery.equalTo('publisher', PublisherA);
let queryResults = await bookQuery.find();

// Let's show the results
for (let result of queryResults) {
  // You access `Parse.Objects` attributes by using `.get`
  console.log(result.get('title'));
};

Let’s now query which BookStore objects contain Book objects with publishing date greater than 01/01/2010, using an inner query with the Parse.Query.greaterThan method and then the Parse.Query.matchesQuery method:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
// Create inner Book query
const bookQuery = new Parse.Query('Book');
bookQuery.greaterThan('publishingDate', new Date('01/01/2010'));

// Query BookStore using inner Book query
const bookStoreQuery = new Parse.Query('BookStore');
bookStoreQuery.matchesQuery('books', bookQuery);
let queryResults = await bookStoreQuery.find();

// Let's show the results
for (let result of queryResults) {
  // You access `Parse.Objects` attributes by using `.get`
  console.log(result.get('name'));
};

Let’s now create a more complex relational query, looking for BookStore objects that have at least one Book written by Author “Aaron Writer” (or “AuthorA”), using equalTo and matchesQuery:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
// Get AuthorA object
const AuthorAQuery = new Parse.Query('Author');
AuthorAQuery.equalTo('name', 'Aaron Writer');
const AuthorA = await AuthorAQuery.first();

// Create inner Book query
const bookQuery = new Parse.Query('Book');
bookQuery.equalTo('authors', AuthorA);

// Query BookStore using inner Book query
const bookStoreQuery = new Parse.Query('BookStore');
bookStoreQuery.matchesQuery('books', bookQuery);
let queryResults = await bookStoreQuery.find();

// Let's show the results
for (let result of queryResults) {
  // You access `Parse.Objects` attributes by using `.get`
  console.log(result.get('name'));
};

Step 4 - Query from a React component

Let’s now use our example queries inside a component in React, with a simple interface having a list showing results and also buttons for calling the queries. This is how the component code is laid out, note the doQuery functions, containing the example code from before.

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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
import React, { useState } from 'react';
import Parse from 'parse/dist/parse.min.js';
import './App.css';
import { Button, Divider } from 'antd';
import { CloseOutlined, SearchOutlined } from '@ant-design/icons';

export const QueryRelational = () => {
  // State variable
  const [queryResults, setQueryResults] = useState();

  const doQueryA = async function () {
    // Get PublisherA object
    const PublisherAQuery = new Parse.Query('Publisher');
    PublisherAQuery.equalTo('name', 'Acacia Publishings');
    const PublisherA = await PublisherAQuery.first();

    // Query Books with PublisherA
    const bookQuery = new Parse.Query('Book');
    bookQuery.equalTo('publisher', PublisherA);

    try {
      let results = await bookQuery.find();
      setQueryResults(results);
      return true;
    } catch (error) {
      // Error can be caused by lack of Internet connection
      alert(`Error! ${error.message}`);
      return false;
    }
  };

  const doQueryB = async function () {
    // Create inner Book query
    const bookQuery = new Parse.Query('Book');
    bookQuery.greaterThan('publishingDate', new Date('01/01/2010'));

    // Query BookStore using inner Book query
    const bookStoreQuery = new Parse.Query('BookStore');
    bookStoreQuery.matchesQuery('books', bookQuery);

    try {
      let results = await bookStoreQuery.find();
      setQueryResults(results);
      return true;
    } catch (error) {
      // Error can be caused by lack of Internet connection
      alert(`Error! ${error.message}`);
      return false;
    }
  };

  const doQueryC = async function () {
    // Get AuthorA object
    const AuthorAQuery = new Parse.Query('Author');
    AuthorAQuery.equalTo('name', 'Aaron Writer');
    const AuthorA = await AuthorAQuery.first();

    // Create inner Book query
    const bookQuery = new Parse.Query('Book');
    bookQuery.equalTo('authors', AuthorA);

    // Query BookStore using inner Book query
    const bookStoreQuery = new Parse.Query('BookStore');
    bookStoreQuery.matchesQuery('books', bookQuery);

    try {
      let results = await bookStoreQuery.find();
      setQueryResults(results);
      return true;
    } catch (error) {
      // Error can be caused by lack of Internet connection
      alert(`Error! ${error.message}`);
      return false;
    }
  };

  const clearQueryResults = async function () {
    setQueryResults(undefined);
    return true;
  };

  return (
    <div>
      <div className="header">
        <img
          className="header_logo"
          alt="Back4App Logo"
          src={
            'https://blog.back4app.com/wp-content/uploads/2019/05/back4app-white-logo-500px.png'
          }
        />
        <p className="header_text_bold">{'React on Back4App'}</p>
        <p className="header_text">{'Relational Queries'}</p>
      </div>
      <div className="container">
        <div className="flex_between">
          <h2 className="heading">{'Query List'}</h2>
          <div className="flex">
            <Button
              onClick={() => doQueryA()}
              type="primary"
              className="heading_button"
              color={'#208AEC'}
              icon={<SearchOutlined />}
            >
              QUERY A
            </Button>
            <Button
              onClick={() => doQueryB()}
              type="primary"
              className="heading_button"
              color={'#208AEC'}
              icon={<SearchOutlined />}
            >
              QUERY B
            </Button>
            <Button
              onClick={() => doQueryC()}
              type="primary"
              className="heading_button"
              color={'#208AEC'}
              icon={<SearchOutlined />}
            >
              QUERY C
            </Button>
            <Button
              onClick={() => clearQueryResults()}
              type="primary"
              className="heading_button"
              color={'#208AEC'}
              icon={<CloseOutlined />}
            >
              CLEAR RESULTS
            </Button>
          </div>
        </div>
        <Divider />
        <div className="flex_between">
          <div className="flex_child">
            {/* Query list */}
            {queryResults !== undefined &&
              queryResults.map((result, index) => (
                <div className="list_item" key={`${index}`}>
                  <p className="list_item_title">{`${
                    result.get('name') !== undefined
                      ? result.get('name')
                      : result.get('title')
                  }`}</p>
                </div>
              ))}
            {queryResults !== undefined && queryResults.length <= 0 ? (
              <p>{'No results here!'}</p>
            ) : null}
          </div>
        </div>
      </div>
    </div>
  );
};
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
import React, { useState, FC, ReactElement } from 'react';
import './App.css';
import { Button, Divider } from 'antd';
import { CloseOutlined, SearchOutlined } from '@ant-design/icons';
const Parse = require('parse/dist/parse.min.js');

export const QueryRelational: FC<{}> = (): ReactElement => {
  // State variable
  const [queryResults, setQueryResults] = useState<Parse.Object[]>();

  const doQueryA = async function (): Promise<boolean> {
    // Get PublisherA object
    const PublisherAQuery: Parse.Query = new Parse.Query('Publisher');
    PublisherAQuery.equalTo('name', 'Acacia Publishings');
    const PublisherA: Parse.Object | undefined = await PublisherAQuery.first();

    // Query Books with PublisherA
    const bookQuery: Parse.Query = new Parse.Query('Book');
    bookQuery.equalTo('publisher', PublisherA);

    try {
      let results: Parse.Object[] = await bookQuery.find();
      setQueryResults(results);
      return true;
    } catch (error) {
      // Error can be caused by lack of Internet connection
      alert(`Error! ${error.message}`);
      return false;
    }
  };

  const doQueryB = async function (): Promise<boolean> {
    // Create inner Book query
    const bookQuery: Parse.Query = new Parse.Query('Book');
    bookQuery.greaterThan('publishingDate', new Date('01/01/2010'));

    // Query BookStore using inner Book query
    const bookStoreQuery: Parse.Query = new Parse.Query('BookStore');
    bookStoreQuery.matchesQuery('books', bookQuery);

    try {
      let results: Parse.Object[] = await bookStoreQuery.find();
      setQueryResults(results);
      return true;
    } catch (error) {
      // Error can be caused by lack of Internet connection
      alert(`Error! ${error.message}`);
      return false;
    }
  };

  const doQueryC = async function (): Promise<boolean> {
    // Get AuthorA object
    const AuthorAQuery: Parse.Query = new Parse.Query('Author');
    AuthorAQuery.equalTo('name', 'Aaron Writer');
    const AuthorA: Parse.Object | undefined = await AuthorAQuery.first();

    // Create inner Book query
    const bookQuery: Parse.Query = new Parse.Query('Book');
    bookQuery.equalTo('authors', AuthorA);

    // Query BookStore using inner Book query
    const bookStoreQuery: Parse.Query = new Parse.Query('BookStore');
    bookStoreQuery.matchesQuery('books', bookQuery);

    try {
      let results: Parse.Object[] = await bookStoreQuery.find();
      setQueryResults(results);
      return true;
    } catch (error) {
      // Error can be caused by lack of Internet connection
      alert(`Error! ${error.message}`);
      return false;
    }
  };

  const clearQueryResults = async function (): Promise<boolean> {
    setQueryResults(undefined);
    return true;
  };

  return (
    <div>
      <div className="header">
        <img
          className="header_logo"
          alt="Back4App Logo"
          src={
            'https://blog.back4app.com/wp-content/uploads/2019/05/back4app-white-logo-500px.png'
          }
        />
        <p className="header_text_bold">{'React on Back4App'}</p>
        <p className="header_text">{'Relational Queries'}</p>
      </div>
      <div className="container">
        <div className="flex_between">
          <h2 className="heading">{'Query List'}</h2>
          <div className="flex">
            <Button
              onClick={() => doQueryA()}
              type="primary"
              className="heading_button"
              color={'#208AEC'}
              icon={<SearchOutlined />}
            >
              QUERY A
            </Button>
            <Button
              onClick={() => doQueryB()}
              type="primary"
              className="heading_button"
              color={'#208AEC'}
              icon={<SearchOutlined />}
            >
              QUERY B
            </Button>
            <Button
              onClick={() => doQueryC()}
              type="primary"
              className="heading_button"
              color={'#208AEC'}
              icon={<SearchOutlined />}
            >
              QUERY C
            </Button>
            <Button
              onClick={() => clearQueryResults()}
              type="primary"
              className="heading_button"
              color={'#208AEC'}
              icon={<CloseOutlined />}
            >
              CLEAR RESULTS
            </Button>
          </div>
        </div>
        <Divider />
        <div className="flex_between">
          <div className="flex_child">
            {/* Query list */}
            {queryResults !== undefined &&
              queryResults.map((result: Parse.Object, index: number) => (
                <div className="list_item" key={`${index}`}>
                  <p className="list_item_title">{`${result.get('name') !== undefined ? result.get('name') : result.get('title')}`}</p>
                </div>
              ))}
            {queryResults !== undefined &&
            queryResults.length <= 0 ? (
              <p>{'No results here!'}</p>
            ) : null}
          </div>
        </div>
      </div>
    </div>
  );
};

Also add these classes to your App.css file to fully render the component layout:

App.css

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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
@import '~antd/dist/antd.css';

.App {
  text-align: center;
}

html {
  box-sizing: border-box;
  outline: none;
  overflow: auto;
}

*,
*:before,
*:after {
  margin: 0;
  padding: 0;
  box-sizing: inherit;
}

h1,
h2,
h3,
h4,
h5,
h6 {
  margin: 0;
  font-weight: bold;
}

p {
  margin: 0;
}

body {
  margin: 0;
  background-color: #fff;
}

.container {
  width: 100%;
  max-width: 900px;
  margin: auto;
  padding: 20px 0;
  text-align: left;
}

.header {
  align-items: center;
  padding: 25px 0;
  background-color: #208AEC;
}

.header_logo {
  height: 55px;
  margin-bottom: 20px;
  object-fit: contain;
}

.header_text_bold {
  margin-bottom: 3px;
  color: rgba(255, 255, 255, 0.9);
  font-size: 16px;
  font-weight: bold;
}

.header_text {
  color: rgba(255, 255, 255, 0.9);
  font-size: 15px;
}

.heading {
  font-size: 22px;
}

.flex {
  display: flex;
}

.flex_between {
  display: flex;
  justify-content: space-between;
}

.flex_child {
  flex: 0 0 45%;
}

.heading_button {
  margin-left: 12px;
}

.list_item {
  padding-bottom: 15px;
  margin-bottom: 15px;
  border-bottom: 1px solid rgba(0,0,0,0.06);
  text-align: left;
}

.list_item_title {
  color: rgba(0,0,0,0.87);
  font-size: 17px;
}

This is how the component should look like after rendering and querying by one of the query functions:

React Back4App

Conclusion

At the end of this guide, you learned how relational queries work on Parse and how to perform them on Back4App from a React App. In the next guide, you will learn how to work with Users in Parse.