Before you can query, you need to add data. Use GraphQL Mutations to add a user, category, posts, and sample data.
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.
addCategory
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.
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:
addOnePost
mutation requires an input $post
(of
type post
) that it then passes as the input
argument to the addPost
mutation:
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.