Build a React App
Learn how to build a front-end app for Stardog using React.
Page Contents
Stardog isn’t only for Java developers, command-line gurus, and database engineers. In this tutorial, we’ll see how easy it is to build a web app – an editable catalog of Star Wars characters – with Stardog, using some of the latest and greatest front-end tools, including React, ES2015+, yarn, Node.js, and stardog.js.
Step 1: Get a Stardog Cloud account
-
Sign up for a free account at cloud.stardog.com. If you have any difficulties with the sign up process, see our Getting Started with Stardog Cloud page.
-
Once you have logged in, click on Manage Endpoints. You will see the new endpoint you just provisioned. Under Endpoint URL, you will see an address of the form
https://sd-1ab23456.stardog.cloud:5820
. Save this address, as we will need it in step 3.
Step 2: Set up a React Project
The next thing to do is setup a basic React project that can communicate with Stardog over HTTP.
-
Go here to download and install Node.js >= version 8, if you don’t have it already. As of this writing, v18.16.0 is the current LTS release.
-
Using
npx
(which comes with Node.js), install and runcreate-react-app
. The final argument to this command should be the directory where you want to put your front-end code. This can be any directory of your choosing, but we’ll use~/stardog-react
for this example: -
Navigate to the directory for the project you just created, and install
stardog.js
, our universal JavaScript library for working with Stardog. In this tutorial, we will useyarn
as our package manager, but the correspondingnpm
commands will also work. -
After that, you can start your React application:
This command should open a browser window or tab and show you a browser-based application running on http://localhost:3000
. You’ve now got a React application that can communicate with Stardog!
Step 3: Load Some Data
For the next step, we’ll add some initial data to Stardog that we can use to create our example application. We’ll do this with a Node.js script, using stardog.js
(this allows us to easily repeat the action if we need to start over).
-
In a new terminal instance (you should leave the
yarn start
command running in the other terminal instance), create adata
subdirectory inside of thesrc
subdirectory of your React project: -
Under this directory, create a file called
data.ttl
, with the following contents (this is just some Turtle representing an RDF graph of Star Wars characters – the same Turtle used in our GraphQL documentation): -
Loading this data into Stardog will involve a couple of operations and values that we’ll also need in our application’s front-end, so let’s first create a
helpers
directory inside ofsrc
with a few shareable constants and utilities.- First, create
~/stardog-react/src/helpers
:
- Next, add a
constants.js
file to thehelpers
directory that looks like this:
The
conn
object in this code block requires the Stardog endpoint address we copied in step 1.- Now add a
wrapWithResCheck.js
file in the same directory asconstants.js
. This file will export a utility method for checking responses received viastardog.js
(just to reduce duplication):
- First, create
-
Finally, we’ll write a Node script that will load this data into a Stardog database named
StarWarsDB
. The script will use the helper method and constants we just created. Inside the samedata
directory as before (see step 2), create a file calledload-data.js
, and add these contents: -
Now execute the script you just created:
You should see a success message in the console: “Created StarWarsDB.” Once you see that, the data is loaded.
Step 4: Create a Basic UI
Our application is going to display a table of Star Wars information about characters’ episode appearances, home planets, and types. We’ll enhance (to allow adding and deleting items) in a moment, but, for now, we’re just going to set it up so that it can display this information.
-
First, so that our table looks presentable, we’ll add Material-UI to our project. At the terminal (inside the project directory), run:
And add two dependencies for this library:
Finally, open
~/stardog-react/public/index.html
and add the following HTML element (above the<title>
element inside of the<head>
element): -
Now, open
~/stardog-react/src/App.js
, and change it so that it looks like this (this creates a single React component that renders a Material-UI-styled data table):As long as you are running the app (if you aren’t, just run
yarn start
again in the project root), you should now see a decently good-looking empty table (i.e., a table consisting of only the column headings) athttp://localhost:3000
.Now let’s make the table do something.
Step 5: Add Some CRUD
At this point, we’re ready for the “meat” of the application. We’ll see how to use Stardog and stardog.js
to read our data and make changes to it (creating new items, deleting others, etc.). The focus in this blog post will be on reading, creating, and deleting – the more complicated case of updating (the ‘U’ in CRUD) is covered in the full GitHub repo for this project, which we’ll link to below.
-
Read from Stardog (Executing a
SELECT
Query withstardog.js
)When our React component mounts, we’re going to immediately request our Star Wars data from Stardog. What we’re interested in for this example is getting data for every character (so, not the data for the planets and episodes themselves), where the data includes the character’s
id
,kind
(i.e., Human or Droid),homePlanet
(if the character has a home planet - droids do not), and all of themovie
s the character has appeared in.The following
SELECT
query would get this data out of Stardog1:And here’s the GraphQL equivalent, which Stardog is happy to support:
The data structure returned by the GraphQL and SPARQL versions of this query are not identical. The code below depends on the SPARQL results form. Look for a followup blog post in which we discuss the pure GraphQL version of this React app.
To perform this query when our application mounts, we’ll add some code to
~/stardog-react/src/App.js
.At the top of this file, add the following import:
Now add the query text for the read query right before our
App
component definition:Finally, add a
componentDidMount
method to theApp
component, along with two methods that will ultimately be called as a result of executingcomponentDidMount
:Voila! At
http://localhost:3000
, you should now see all of the Star Wars data that we wanted to see, displayed in a nice little table.You’ve now created a Star Wars data table backed by Stardog. Congrats!
-
Create New Data
This table is nice for quickly browsing our Star Wars data, but the data is woefully incomplete. What if we want to add things to it?
To do this, let’s first create some UI elements. In
~/stardog-react/src/App.js
, modify the styles object to look like this:Let’s also add the following
import
to the top of the file:Next, in the
render
methods of theApp
component, we’ll add a couple of buttons and table cells/rows to our table.First, add an additional table cell to
renderRowForBinding
:Second, in
render
, let’sconcat
an extra row onto the table:This will add a new “Action” column to our table, where the column contains a “DELETE” button for existing rows, and an “ADD” button for new data. Additionally, there will be a row at the end of the table that consists entirely of input fields. We’ll use these fields to create new pieces of data. (As always, this should be viewable on your local machine at
http://localhost:3000
.)Let’s wire up the “ADD” button. First, add an
onClick
handler to the button, like so:Finally, inside of the
App
component, let’s add the actualaddItem
method:If you return to the application running in your browser, you should now be able to add data to Stardog via the table. As a test, enter (all without quotes) ‘2002’ in the ‘ID’ field, ‘Anakin Skywalker’ in the ‘Name’ field, ‘NewHope, Empire, Jedi’ in the ‘Movies’ field, ‘Tatooine’ in the ‘Home Planet’ field, and ‘Human’ in the ‘Kind’ field, then click the ‘Add’ button.
You should see the input fields clear, the table refresh, and a new entry appear in the table for Anakin Skywalker. If you’d like to confirm that the database itself (and not just the front-end) was updated, you can refresh the app and see that Anakin Skywalker persists.
-
Delete Data from Stardog
We just added Anakin Skywalker to our Star Wars data. But – spoiler alert! – Anakin Skywalker is Darth Vader, and Darth Vader is already in our table. We should probably delete one of them2. Since our table is focused on the original trilogy, Anakin is the odd one out, so we’ll delete him from the table.
Add an
onClick
handler to the “DELETE” button in the currentApp
component. The click handler should pass along theid
binding for the character being deleted, so that we only delete the correct character’s data (we’ll assume the ids are unique here, though we haven’t added validation for that).Now, create the
deleteItem
method inside of theApp
component. Here’s how that should look (it’s quick and easy):If you return to the browser and click on the “DELETE” button for Anakin, you should see his entry removed from the table. Again, if you’d like to confirm that this operation really changed the data in Stardog, feel free to refresh the application.
You’ve now built a (nearly) complete CRUD application with React and Stardog (elegant weapons for a more civilized age!).3
Summary
We’ve done quite a bit here. We’ve learned how to install Stardog, how to set up a starter React application, how to use stardog.js
in both Node and the browser, and, finally, how to bring it all together to produce an app backed by Stardog and the latest technologies in the modern front-end stack.
We hope you’ll use this knowledge to build some awesome front-end applications with Stardog (or, at least, to maintain a really great catalog of your favorite Star Wars characters).
-
If you’re not sure about the parts of this query, or how to come up with queries like it, we strongly recommend that you try out Stardog Studio, our Knowledge Graph IDE, which provides autocompletions, hints, query linting, and more. You can reach Studio by signing in at cloud.stardog.com and clicking on Stardog Studio. ↩
-
We could add aliases to our data model, but that would probably take us too far afield. ↩
-
For details on the “Update” part of CRUD, see the GitHub repository for this project. ↩