Mutations

Mutations

The process of sending data to server is called mutation. Defining it is pretty similar on how you’ve defined the query.

Let me explain this snippet, piece by piece:

  • #1: Defines a mutation class. Right after, you define the output of the mutation, the data the server can send back to the client. The output is defined field by field for learning purposes. In the next mutation you’ll define them as just one.
  • #2: Defines the data you can send to the server, in this case, the links’ url and description.
  • #3: The mutation method: it creates a link in the database using the data sent by the user, through the url and description parameters. After, the server returns the CreateLink class with the data just created. See how this matches the parameters set on #1.
  • #4: Creates a mutation class with a field to be resolved, which points to our mutation defined before.

This will make sure our schema knows the mutation created.

Creating a Link

Time to play! In the GraphiQL interface, enter the following data and see how a link gets created:

link getting created

After, try to query the data again, you should see the new link.

see new link

Unlock the next chapter
What differentiates a query from a mutation?
The query returns just lists of objects
The mutation is used for pagination
Mutations are used for getting data and queries for sending data
Mutations are used for sending data and queries for getting data