GraphQL Mutations

In this lesson, we will be introducing the GraphQL mutation and actually make one ourselves.

Mutation vs. Query

GraphQL mutations complement GraphQL queries because they are used for writing data that queries can read. The mutation shares the same principles as the query: it has fields and objects, arguments and variables, fragments and operation names, as well as directives and nested objects for the returned result. When the mutation is valid, we should receive the updated data for the specified fields and objects in our query. Before we start making our first mutation, we need to be aware that we are using live GitHub data, so if you follow a person on GitHub using your experimental mutation, you will follow this person for real.

Starring a GitHub Repository

In this lesson, we will star a repository on GitHub using a mutation from GitHub’s API. This would be the same repository we requested using a query in the previous lesson. The addStar mutation can be found in the “Docs” sidebar. The repository we will be working with is a project for teaching developers about the fundamentals of React so, starring it should prove useful.

You can visit the repository just to make sure that you haven’t given it a star already. We want an unstarred repository so that we can star it using a mutation. Before you can star a repository, you need to know its identifier which can be retrieved by a query:

Get hands-on with 1200+ tech skills courses.