How to Define Operation Names?
Defining an operation name in GraphQL is simple but requires careful consideration. After specifying the operation type (query, mutation, or subscription), we add our chosen name. This makes each operation in our code clear and easy to distinguish.
Here’s how to define an operation name in GraphQL:
- Start by selecting the desired operation type: query, mutation, or subscription.
- After the operation type, add a descriptive and meaningful name.
- Enclose the operation name in curly brackets to distinguish it from the rest of the operation.
Here’s an example of how to define an operation name in GraphQL:
query GetUserDetails {
user(id: "123") {
name
age
}
}
By following these steps, we’ve defined an operation name, “GetUserDetails,” for the GraphQL query, making it easily identifiable within a document.
GraphQL has three types of operations: queries, mutations and subscriptions. Let’s understand one-by-one.
1. Query
- To retrieve data from the server based on the conditiona specified by the developer, GraphQL Queries are required.
- When speaking of comparisons for queries one can think of themselves in a library displaying a need for a particular book that a librarian can find.
- Just like in this case, GraphQL queries enable developers to obtain just the needed data from the server.
- In the query, developers describe data they need and a server responds to them, delivering just what they are looking for.
Let’s take an example :
query getQuoteByUser {
quote(by: "4232") {
name
by
}
}
Response:
{
"data": {
"quote": {
"name": "If it works don't touch it",
"by": "User 4232"
}
}
}
In this query, “GetUserDetails” serves as the Operation Name, providing context and clarity to the purpose of the query.
2. Mutation
- Mutations in GraphQL serve the purpose of modifying data on the server.
- While queries are similar to requests for information, mutations require that the server be instructed to modify or update any changes.
- Going further with our library analogy, mutations can be compared to asking the librarian to reserve a book for us.
- Mutations are used in GraphQL when you need to add, update or remove data from your server.
Let’s take an example :
mutation createUser($userNew: UserInput!) {
signupUserDummy(userNew: $userNew) {
id
firstName
lastName
}
}
Response:
{
"data": {
"signupUserDummy": {
"id": "c938bd98d6",
"email": "sdsad@sd.com",
"firstName": "navin",
"lastName": "ssd"
}
}
}
In this mutation, “AddNewUser” acts as the Operation Name, aiding in identifying and distinguishing this specific mutation operation.
3. Subscriptions
- The GraphQL subscriptions bridge the gap between the server and the client and establish real-time data updates.
- They are capable of serving you with an uninterrupted stream of data as if you were talking with the server in real time.
- Subscription allows the server to send data in real time to the client rather than waiting for query and mutation to be invoked implicitly.
- When it is necessary to quickly deliver brief news updates on a regular basis then subscriptions are an excellent choice.
Let’s take an example:
subscription watchOperations {
operationFinished {
endDate
name
}
}
mutation scheduleOperation($name: String!) {
scheduleOperation(name: $name)
}
Response:
{
"data": {
"operationFinished": {
"endDate": "Wed, Jan 04 2023",
"name": "Test name!"
}
}
}
In this subscription, the subscription watchOperations listens for notifications when operations are finished, while the mutation scheduleOperation allows you to schedule new operations by providing a name.
Operation Name in GraphQL
In GraphQL, the operation name is a label that we attach to requests sent to servers which acts as an indication of code complexity. This allows us to define queries, mutations, and subscriptions to improve code readability and organize in particular when it comes to large projects making it easier to access and manage it.
In this article, We will learn about Operation Name in GraphQL, its need, How to Define Operation Names with examples,, and so on.