Run Dgraph with Docker
dgraph/standalone
Docker image has everything needed to run Dgraph locally.Ensure you have Docker installed, then run the following command to start a local Dgraph instance:docker run
- initiates a new Docker container--rm
- automatically removes the container when it exits, helping with cleanup-it
- uses interactive mode to show output of the container-p 8080:8080
- maps port 8080 from the host machine to port 8080 in the Docker container to allow Dgraph HTTP connections-p 9080:9080
- maps port 9080 from the host machine to port 9080 in the Docker container to allow Dgraph gRPC connectionsdgraph/standalone:latest
- specifies the Docker image to use, this is the official Dgraph image with latest tagConnect Ratel
https://ratel.hypermode.com
and enter http://localhost:8080
for the “Dgraph Conn String”.
This will allow Ratel to connect to our local Dgraph instance and execute DQL queries.dgraph/ratel
container with the following command:Add mutation in Ratel
View mutation results
Query for all movies
Movie
nodes as the start of the traversal using
the type(Movie)
function to define the starting point of our query traversal,
then finds any genres, directors, and characters connected to each movie.View results in JSON and graph visualization
type()
function to find the starting point of our
graph traversal. We can use more complex functions to filter by string
comparison operator, and others, however to use these function we must first
update the graph schema to create an index on the predicates we want to use in
these functions.
The function documentation specifies which kind of
index is needed for each function.
We’ll use Ratel to alter the schema to add indexes on some of the data so
queries can use term matching, filtering, and sorting.
Create an index for movie title
Movie.title
predicate. Ratel displays details about the predicate
type and indexes.Change the type to string then select index and select term for the
Movie.title
predicate, then select Update to apply the index.Create an index for movie release date
Movie.release_date
predicate.Select the Movie.release_date
predicate. Change the type to dateTime.
Select index and choose year for the index tokenizer. Click Update
to apply the index on the release-date
predicate.Query using indexes
Add reverse relationship
Movie.genre
predicate as a reverse relationship.To define a reverse relationship for the Movie.genre
predicate we’ll return to
the Schema page in Ratel, select the Movie.genre
predicate and toggle the
reverse checkbox. Then select Update to apply this schema change.Query using the reverse relationship
~
operator is used to specify a reverse relationship. To
traverse from a genre node to a movie node we use the syntax ~Movie.genre
.In this query we find all movies connected to the “Sci-Fi” genre:movies: ~Movie.genre
.