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.
Add a User
GraphQL mutations have the useful property of returning a result. That result can help your UI, for example, to re-render a page without further queries. Dgraph Cloud lets the result returned from a mutation be as expressive as any graph traversal. Let’s start by adding a single test user. The user type has the following fields:username
, displayName
, avatarImg
, posts
and comments
, as shown
below:
!
) is the username. So,
to generate a new user node in the graph, we only need to supply a username.
The sample app’s GraphQL API includes an addUser
mutation, which can be used
to add multiple users and their data in a single operation. You can add one user
and their username
with the following mutation:
mutation
keyword tells a GraphQL server that it’s running a mutation. The
mutation (in this case, addUser
) takes the provided arguments and adds that
data to the graph.
The mutation shown above adds a single user, User1
, and returns the newly
created user’s username
and displayName
. The displayName
will be null
because you didn’t provide that data. This user also has no posts
or
avatarImg
, but we aren’t asking for those in the result. Here’s how it looks
when run in the Dgraph Cloud API Explorer.

Add a category
The graph now has a single node. Next, you’ll add a category using theaddCategory
mutation. Categories are a little different than users because the
id is auto-generated by Dgraph Cloud. The following mutation creates the
category and returns its name
and the id
Dgraph Cloud gave it.
id
is auto generated and will be different on any
execution of the mutation.

Add Some Posts
Dgraph Cloud can do more than add single graph nodes at a time. The mutations can add whole subgraphs and link into the existing graph. To show this, let’s do a few things at once. Remember our first sketch of some graph data?
User1
and Category1
nodes. It is not much of
a graph, so let’s flesh out the rest of the graph in a single mutation. We’ll
use the addPost
mutation to add the three posts, link all the posts to
User1
, link posts 2 and 3 to the existing category, and then create
Category2
. And, you’ll do all of this in a single operation using the
following mutation:
ID
, when you run such a
mutation, you’ll need to make sure that you use the right id value for
Category1
--- in my run that was 0xfffd8d6ab6e7890a
, but yours might differ.
In the Dgraph Cloud API explorer, that mutation looked like this:

Add sample data
You can run some more mutations, add more users or posts, or add comments to the posts. To get you started, here’s a mutation that adds some more data that we can use to explore GraphQL queries in the following section.GraphQL Variables
A mutation that takes data in its arguments is great to try out in a UI tool, but an app needs to connect the data in its internal data structures to the API without building complex query strings. GraphQL Query Variables let a query or mutation depend on input values that are resolved at run-time. For example, the followingaddOnePost
mutation requires an input $post
(of
type post
) that it then passes as the input
argument to the addPost
mutation:

Mutations used in the App
The app always uses GraphQL variables so that there’s a small set of mutations and the data can be supplied by serializing client-side data structures. The app will need a mutation to add users:updatePost
mutation combines a search and mutation into one. The mutation
first finds the posts to update (the filter
) and then sets new values for the
post’s fields with the set
argument. To learn how the filter
works, let’s
look at how Dgraph Cloud handles queries.