What is a Mutation in GraphQL?

What is a Mutation in GraphQL?

GraphQL is a query language for your API. In this post, we are going to learn what a GraphQL Mutation is all about, and even write a few ourselves. To change data with GraphQL, we can send a mutation. In the REST world you might be familiar with GET and POST requests. Think of a GraphQL mutation as a function that can perform custom CREATE, UPDATE, and/or DELETE operations with a little extra finesse and flexibility.

GraphQL is a query language for your API. It was open-sourced by Facebook in 2015 and ever since then gained immense popularity as an alternative to REST. In this post, we are going to learn what a GraphQL Mutation is all about, and even write a few ourselves.

Introduction

In the REST world you might be familiar with GET and POST requests. Similarly, in GraphQL when a client asks for data from the server it is called a _query _and when the client writes or updates data to the server it is termed as a mutation.

Mutations in GraphQL are used to make changes to the data.

To learn mutations let’s use the GraphQL explorer and run some mutations against the GitHub’s public GraphQL API. You can follow along this post by clicking the link below. Make sure you are signed into your GitHub account to follow along.

https://developer.github.com/v4/explorer/

On the GitHub GraphQL explorer, we can start typing our queries and mutations on the left side and hit play to see the JSON response on the right side. We can also browse through the documentation of the API on the right side. The explorer is a great way to write, validate and test queries and mutations.

GraphQL Mutations

Alright, we are all set to write our first mutation. You can browse the mutations that are available to us, from the Documentation Explorer on the right. Something interesting to note is that, the GraphQL Schema is strongly-typed and hence generates its own documentation without any manual work from developers. Isn’t that nice?! It provides great API documentation without any manual effort. Alright back to mutations.

There are plenty of mutations that are available to us in this API and I am going to pick the _createRepository _mutation. As the name suggests, the client can use this mutation to create a new repository for the user on GitHub. Let’s dive into writing this mutation.

Define an Operation Name

mutation myFirstMutation {
  // ..
}

Whenever you write a mutation you define it with a unique operation name. Here we use the keyword _mutation _and give our mutation an operation name _myFirstMutation. _

Explore The Documentation

I am now going to browse through the documentation for the createRepository mutation. It looks like the **_createRepository _**mutation takes in an argument of type **_CreateRepositoryInput. _**Clicking on it will provide me more information on what this type contains.

In GraphQL, the exclamation mark (!) on a type indicates that the field cannot be null. In this case the createRepository mutation is expecting an argument CreateRepositoryInput! and it cannot be null.

Pass Variable Input Arguments

mutation myFirstMutation ($input: CreateRepositoryInput!){
  createRepository(input: $input) {
    clientMutationId
  }
}

I have now passed an input argument of type _CreateRepositoryInput! _The input argument here can be any variable that the client can pass. The input variable is represented as $input. Now we are writing our createRepository mutation and passing the input variable down to it.

Define Input Variable

Since we decided to pass a variable input to the mutation, it is time to define it. In the GraphQL explorer, you can define input variables in the Query Variables section on the left. In our case the input needs to be of type CreateRepositoryInput and it requires a _name _and visibility fields to perform the mutation.

{
  "input": {
    "clientMutationId": "102020",
    "name": "My New Repository",
    "visibility": "PUBLIC"
  }
}

We are giving a name for the repository as “My New Repository” and setting the visibility to be public. I am also passing a clientMutationId. This is not required field for our mutation. It can be any random number that acts as the unique Id for this mutation.

Run the Mutation

Now we are all set. We can run our mutation and it should return back a successful JSON. This is the JSON response I receive back. It doesn’t tell us much, but it indicates that the mutation executed at the _clientMutationId _that we defined in the input variable.

{
  "data": {
    "createRepository": {
      "clientMutationId": "102020"
    }
  }
}

Validate Mutation

Since we are using live production data from GitHub to run our mutations, we can validate this mutation easily. If you were following along with me this far, if you go to your GitHub profile and look at your repositories, there should be a new repository created.

Isn’t that neat! Well, we can’t always validate mutations by checking the user interface. To validate this mutation without the UI, we can write a simple query to make sure the new repository was created.

query validate{
  user (login:"adhithiravi") {
    repositories(last: 1) {
      nodes {
      	name
      }
    }
  }
}

This query above, queries my user on GitHub and queries for the most recently created repository. This query can validate if the mutation occurred as expected or not. The query result should return the name of the new repository I created.

Resources

That’s a wrap folks! I hope this article shed some light on GraphQL mutations and using them. I encourage you to play around with some GraphQL APIs to learn this better. Here is a list of other public GraphQL APIs that are available to everyone.

https://github.com/APIs-guru/graphql-apis

Angular 9 Tutorial: Learn to Build a CRUD Angular App Quickly

What's new in Bootstrap 5 and when Bootstrap 5 release date?

What’s new in HTML6

How to Build Progressive Web Apps (PWA) using Angular 9

What is new features in Javascript ES2020 ECMAScript 2020

Hire Dedicated eCommerce Web Developers | Top eCommerce Web Designers

Hire Dedicated eCommerce Web Developers | Top eCommerce Web Designers

Build your eCommerce project by hiring our expert eCommerce Website developers. Our Dedicated Web Designers develop powerful & robust website in a short span of time.

Build your eCommerce project by hiring our expert eCommerce Website developers. Our Dedicated Web Designers develop powerful & robust website in a short span of time.

Hire Now: https://bit.ly/394wdOx

Mobile App Development Company India | Ecommerce Web Development Company India

Mobile App Development Company India | Ecommerce Web Development Company India

Best Mobile App Development Company India, WebClues Global is one of the leading web and mobile app development company. Our team offers complete IT solutions including Cross-Platform App Development, CMS & E-Commerce, and UI/UX Design.

We are custom eCommerce Development Company working with all types of industry verticals and providing them end-to-end solutions for their eCommerce store development.

Know more about Top E-Commerce Web Development Company

Hire PHP Developer and Web Developer for your Online Business

Hire PHP Developer and Web Developer for your Online Business

PHP is widely used open-source scripting language it helps in making dynamically easy your websites and web application. Mobiweb Technology is your best technical partner and offering you solution for any kind of website and application...

PHP is widely used open-source scripting language it helps in making dynamically easy your websites and web application. Mobiweb Technology is your best technical partner and offering you solution for any kind of website and application development. To hire PHP developer and web developer at affordable prices contact Mobiweb Technology via [email protected]