Working with Variables in Mutations

Tutorial 4 of 5

Working with Variables in Mutations

Introduction

In this tutorial, we're going to dive into the world of GraphQL mutations, specifically focusing on the use of variables. The goal is to learn how to create dynamic and reusable mutations by leveraging the power of variables.

By the end of this tutorial, you will be able to:

  • Understand the concept of mutations in GraphQL
  • Create dynamic mutations using variables
  • Use best practices when working with mutations

Prerequisites:

  • Basic understanding of GraphQL
  • Familiarity with JavaScript

Step-by-Step Guide

Mutations in GraphQL are used to change data on the server. They are similar to 'POST' or 'PUT' requests in REST. Mutations often need to be dynamic, and that's where variables come in. GraphQL allows you to pass variables to mutations to make them more reusable.

Here's a general syntax of a mutation with variables:

mutation ($variable1: Type1!, $variable2: Type2) {
  mutationName(input: {field1: $variable1, field2: $variable2}) {
    responseField1
    responseField2
  }
}

In the above syntax, Type1! and Type2 are the types of variables. The ! after Type1 means that variable1 is a required field.

Code Examples

Let's go through some practical examples.

Example 1

Let's create a mutation to add a new book to our library.

mutation ($title: String!, $author: String!) {
  addBook(input: {title: $title, author: $author}) {
    id
    title
    author
  }
}

In the above code:
- $title and $author are variables of type String. Both are required.
- addBook is the mutation function that takes an input object.
- The input object contains fields title and author which are set to the values of $title and $author respectively.
- The mutation returns the id, title, and author of the newly added book.

Example 2

Let's create a mutation to update a book's title.

mutation ($id: ID!, $newTitle: String!) {
  updateBook(input: {id: $id, title: $newTitle}) {
    id
    title
  }
}

In the above code:
- $id and $newTitle are variables of type ID and String respectively. Both are required.
- updateBook is the mutation function that takes an input object.
- The input object contains fields id and title which are set to the values of $id and $newTitle respectively.
- The mutation returns the id and the updated title of the book.

Summary

You've learned how to use variables in GraphQL mutations to make them more dynamic and reusable. Remember, when you need to change data on the server, mutations are the way to go. And when you want your mutations to be flexible and reusable, don't forget to use variables!

Next steps for learning:
- Learn about advanced mutation concepts like optimistic UI and error handling.
- Explore subscriptions in GraphQL.

For additional resources, you can check out the official GraphQL documentation here.

Practice Exercises

  1. Write a mutation to delete a book from the library.
  2. Write a mutation to add a new author to the system.

Solutions:

  1. Deletion mutation
mutation ($id: ID!) {
  deleteBook(id: $id) {
    id
  }
}

In this mutation, $id is the ID of the book to be deleted.

  1. Mutation to add a new author
mutation ($name: String!, $age: Int!) {
  addAuthor(input: {name: $name, age: $age}) {
    id
    name
    age
  }
}

In this mutation, $name and $age are the name and age of the new author, respectively.