Max Rozen

What is a GraphQL Mutation, and how is it different to a GraphQL Query?

January 03, 2019 • ☕️ 1 min read

Before I start, I want to preface that a fair bit of what I’ve learned working with GraphQL comes directly from both tinkering with code, and reading the actual GraphQL specification. It may seem dense, but if you just use it to look up the feature you’re curious about, it can be helpful!

What is a GraphQL Query?

The GraphQL specification defines a query as a “read-only fetch” - essentially a request for data from your server, and getting it back. This is analagous to a GET request against a RESTful service.

A typical query looks like this:

{
    query queryName($tag: String!){
        posts($tag) {
            id
            title
            tag
        }
    }
}

As a side note - if your query contains no variables or directives, you can use query shorthand:

{
  posts {
    id
    title
  }
}

What is a GraphQL Mutation?

A mutation, on the other hand, is a “write, followed by a fetch” - in this case, the GraphQL client sends data to the GraphQL server, expecting some sort of result back.

As an example, this snippet of GraphQL might “like” a post, and return the updated number of likes as a result.

mutation {
  likePost(postID: 1002) {
    post {
      likeCount
    }
  }
}

Summary

In summary, the difference is that a Query only fetches data, while a Mutation first sends data, then fetches data.

Discuss on TwitterEdit on GitHub

Enjoyed this post? Receive the next one in your inbox!

I won't send you spam.

Unsubscribe at any time.

MaxRozen.com

Max Rozen

Thoughts on business, software, and marketing by Max Rozen [About]