Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

graphql

Pardon the naive question, but I've looked all over for the answer and all I've found is either vague or makes no sense to me. Take this example from the GraphQL spec:

query getZuckProfile($devicePicSize: Int) {   user(id: 4) {     id     name     profilePic(size: $devicePicSize)   } } 

What is the point of naming this query getZuckProfile? I've seen something about GraphQL documents containing multiple operations. Does naming queries affect the returned data somehow? I'd test this out myself, but I don't have a server and dataset I can easily play with to experiment. But it would be good if something in some document somewhere could clarify this--thus far all of the examples are super simple single queries, or are queries that are named but that don't explain why they are (other than "here's a cool thing you can do.") What benefits do I get from naming queries that I don't have when I send a single, anonymous query per request?

Also, regarding mutations, I see in the spec:

mutation setName {   setName(name: "Zuck") {     newName   } } 

In this case, you're specifying setName twice. Why? I get that one of these is the field name of the mutation and is needed to match it to the back-end schema, but why not:

mutation {   setName(name: "Zuck") { ... 

What benefit do I get specifying the same name twice? I get that the first is likely arbitrary, but why isn't it noise? I have to be missing something obvious, but nothing I've found thus far has cleared it up for me.

like image 421
Nolan Avatar asked Dec 01 '15 03:12

Nolan


People also ask

What is the use of mutation in GraphQL?

Mutations allow you to modify server-side data, and it also returns an object based on the operation performed. It can be used to insert, update, or delete data. Dgraph automatically generates GraphQL mutations for each type that you define in your schema.

Does GraphQL query name matter?

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).

What is the use of query in GraphQL?

A GraphQL query is used to read or fetch values while a mutation is used to write or post values. In either case, the operation is a simple string that a GraphQL server can parse and respond to with data in a specific format.

Why is GraphQL named?

The fields in our GraphQL query (i.e. book, authors, name) specify which edges should be followed in the application data graph to get our desired result. This is where GraphQL gets its name: GraphQL is a query language that traverses your data graph to produce a query result tree.


2 Answers

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.

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.

like image 141
Petr Bela Avatar answered Oct 19 '22 16:10

Petr Bela


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.

like image 41
orta Avatar answered Oct 19 '22 16:10

orta