Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does the "operation name" reference?

Tags:

I'm learning about GraphQL and I'm very interested in the operation name the part of the query that comes after the query or mutation (depending on the root query type). I found a couple of code examples using the operation name, and I'm confused as to where they come from? There seems to be no references in the code about them, and they seem completely arbitrary.

query Welcome {   echo (email: "[email protected]") } 

and

query HeroNameQuery {   hero {     name   } } 

I don't understand why a given schema can't just contain the queries and types that follow (eg. user, article, order, etc.), and I don't understand the namespacing system and the operation name provides any sort of advantage.

https://github.com/mugli/learning-graphql/blame/master/7.%20Deep%20Dive%20into%20GraphQL%20Type%20System.md#L436

http://graphql.org/docs/queries/

like image 828
ThomasReggi Avatar asked Jun 24 '16 20:06

ThomasReggi


People also ask

What is Operation name in GraphQL?

The operation name is pretty much up to you on what you want to call it. However, you do need it when you pass in query / mutation parameters like so: // GraphQL Query query Welcome ($data: String!) { echo (email: $data) { name } } // GraphQL Variables { "data": "[email protected]" }

What is 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.


2 Answers

In GraphiQL you can choose from a list of queries specified by the operation name. Here's some screenshots to help make this make sense.

enter image description here

When you have two mutations / queries side-by-side and they don't have an operation name you can't run them.

When they have operation names they can be listed when you click the play / run button.

enter image description here

This is the biggest case for having an operation name that I've seen so far.

But it's not necessary because when you just have run query / mutation that's what's gonna run.

enter image description here

like image 151
ThomasReggi Avatar answered Oct 11 '22 12:10

ThomasReggi


The Query/Mutation name is optional. You can use it on the backend for stored queries if your backend supports it. However, it is generally used for logging. You can use a unique name for each query/mutation. Then, when you are having problems, you can grep through your logs for the query name to see what was happening with that specific query.

like image 45
James Avatar answered Oct 11 '22 11:10

James