Dgraph GraphQL Tour
Introduction
Mutation Introduction
In the last exercise, you added some data into Dgraph. Adding or removing data in Dgraph is called a mutation.
With Dgraph’s GraphQL API, every item that is created is called a node. In the previous example we added a total of 11 nodes: eight people and three animals. Nodes can have properties, such as the name and age of the person, and the name of their animal. Nodes can be connected to other nodes by edges. We connected people to their friends and animals to their owners with edges.
Dgraph’s GraphQL API uses two methods for identifying nodes. Nodes can be
identified using the ID scalar, or any other field with the @id
directive.
Fields with the @id
directive must be unique for all nodes of that type. In
our examples we will be using our field xid
which we defined in the schema
with the @id
directive.
When adding a node, you must provide values for the fields identified with a
exclamation mark (!
), as these are required input. When updating or deleting a
node, you will need to reference the node using the methods described above.
When creating edges to nodes, if you use the method above to identify a node, it
will create an edge to the existing node. But, if you leave off all identifiers
and supply all the required fields for creating a new node, a new node will be
created and linked on that edge.
In the example mutation provided we will add a new person, “Manish”. We will
also add his new pet, “Diggy”, and make the friendship relationship to the
existing person “Alice” referencing her by her node’s xid
.
In our GraphQL Schema we placed the @hasInverse
directive on the edges so the
API will track inverse edges. We do not have to tell the API to also create the
edges between “Diggy” to his owner “Manish” or the inverse friend relationship
from “Alice” back to “Manish”.