What is the point of naming queries and mutations in GraphQL?

The query name doesn't have any meaning on the server whatsoever. It's only used for clients to identify the responses (since you can send multiple queries/mutations in a single request).

In fact, you can send just an anonymous query object if that's the only thing in the GraphQL request (and doesn't have any parameters):

{
  user(id: 4) {
    id
    name
    profilePic(size: 200)
  }
}

This only works for a query, not mutation.

Once you use the query (or mutation) keyword though, you need to provide a name. The server might theoretically be able to process the request even without a name but that's not what the current GraphQL specification allows.

EDIT

As @orta notes below, the name could also be used by the server to identify a persistent query. However, this is not part of the GraphQL spec, it's just a custom implementation on top.


We use named queries so that they can be monitored consistently, and so that we can do persistent storage of a query. The duplication is there for query variables to fill the gaps.

As an example:

query getArtwork($id: String!) {
  artwork(id: $id) {
    title
  }
}

You can run it against the Artsy GraphQL API here

The advantage is that the same query each time, not a different string because the query variables are the bit that differs. This means you can build tools on top of those queries because you can treat them as immutable.

Tags:

Graphql