Schema Design
Starting with listing the entities that are involved in a basic to-do app, this step in the GraphQL tutorial walks you through schema design.
We’re overhauling Dgraph’s docs to make them clearer and more approachable. If you notice any issues during this transition or have suggestions, please let us know.
Let’s start with listing down the entities that are involved in a basic to do app.
- Task
- User
Equivalent GraphQL schema for this graph is be as follow:
What are the fields that these two simple entities contain?
There is a title and a status to check if it was completed or not in the Task
type. Then the User
type has a username (unique identifier), name and the
tasks.
So each user can have many tasks.
Now let’s add @id
directive to username
which makes it the unique key & also
add @hasInverse
directive to enable the relationship between tasks and user.
We represent that in the GraphQL schema shown below:
Save the content in a file schema.graphql
.
Running
Before we begin, make sure that you have Docker installed on your machine.
Let’s begin by starting Dgraph standalone by running the command below:
Let’s load up the GraphQL schema file to Dgraph:
You can access that GraphQL endpoint with any of the great GraphQL developer tools. Good choices include GraphQL Playground, Insomnia, GraphiQL and Altair.
Set up any of them and point it at http://localhost:8080/graphql
. If you know
lots about GraphQL, you might want to explore the schema, queries and mutations
that were generated from the schema.
Mutating data
Let’s add a user and some to dos in our To Do app.
Querying data
Let’s fetch the to dos to list in our To Do app:
Running this query should return JSON response as shown below:
Querying data with filters
Before we get into querying data with filters, we’re required to define search indexes to the specific fields.
Let’s say we want to run a query on the completed
field, for which we add
@search
directive to the field, as shown in the schema below:
The @search
directive is added to support the native search indexes of
Dgraph.
Resubmit the updated schema -
Now, let’s fetch all to dos which are completed:
Next, let’s say we want to run a query on the title
field, for which we add
another @search
directive to the field, as shown in the schema below:
The fulltext
search index provides the advanced search capability to perform
equality comparison as well as matching with language-specific stemming and
stopwords.
Resubmit the updated schema -
Now, let’s try to fetch to dos whose title has the word “avoid”:
Was this page helpful?