Techdozo
  • Microservices
    • All
    • GraphQL
    • gRPC
    • Spring Boot
    gRPC Bidirectional Streaming with Code Example

    gRPC Bidirectional Streaming with Code Example

    gRPC Client Streaming

    gRPC Client Streaming

    Distributed transaction in microservices using Saga

    Distributed Transactions in Microservices: implementing Saga with Temporal

    Temporal Workflow Orchestration

    Workflow Orchestration with Temporal and Spring Boot

    GraphQL Directive

    GraphQL Directive

    Spring for GraphQL mutation

    Spring for GraphQL: Mutation

  • Spring Boot
    Spring Boot GraphQL service

    Getting started with Spring Boot GraphQL service

    Deploying a RESTful Spring Boot Microservice on Kubernetes

    Deploying a RESTful Spring Boot Microservice on Kubernetes

    RESTful Microservices with Spring Boot and Kubernetes

    RESTful Microservices with Spring Boot and Kubernetes

    RESTful API Gateway with gRPC

    RESTful API Gateway with gRPC

  • gRPC
    gRPC Bidirectional Streaming with Code Example

    gRPC Bidirectional Streaming with Code Example

    gRPC Client Streaming

    gRPC Client Streaming

    gRPC Interceptor: unary interceptor with code example

    gRPC Interceptor: unary interceptor with code example

    gRPC: synchronous and asynchronous Server streaming RPC

    gRPC: synchronous and asynchronous Server streaming RPC

    Photo by Ramón Salinero on Unsplash

    gRPC: synchronous and asynchronous unary RPC in Java

    Microservices inter-process communication using gRPC

    gRPC for microservices communication

  • GraphQL
    GraphQL Directive

    GraphQL Directive

    Spring for GraphQL mutation

    Spring for GraphQL: Mutation

    Spring for GraphQL: How to solve the N+1 Problem?

    Spring for GraphQL: How to solve the N+1 Problem?

    Spring GraphQL with @Controller, @SchemaMapping and @QueryMapping

    Spring for GraphQL : @SchemaMapping and @QueryMapping

    Spring Boot GraphQL service

    Getting started with Spring Boot GraphQL service

  • Kubernetes
    Deploying a RESTful Spring Boot Microservice on Kubernetes

    Deploying a RESTful Spring Boot Microservice on Kubernetes

    Components of Kubernetes Architecture

    Components of Kubernetes Architecture

    Helm Chart: quick start your app deployment on Kubernetes

    Helm Chart: quick start your app deployment on Kubernetes

    gRPC load balancing on Kubernetes (using Headless Service)

    gRPC load balancing on Kubernetes (using Headless Service)

    Getting started with Kind: quick start a multi-node local Kubernetes cluster

    Getting started with Kind: quick start a multi-node local Kubernetes cluster

    Getting started with Minikube: deploying application on local Kubernetes cluster

    Getting started with Minikube: deploying application on local Kubernetes cluster

  • Java
    Java Streams: Stream Operation with Examples

    Java Streams: Stream Operation with Examples

    Java Streams: stream creation with examples

    Java Streams: stream creation with examples

    Garbage Collection

    Super Fast Garbage Collectors in Java

    Calculus

    Functional Programming in Java

No Result
View All Result
  • Login
  • Microservices
    • All
    • GraphQL
    • gRPC
    • Spring Boot
    gRPC Bidirectional Streaming with Code Example

    gRPC Bidirectional Streaming with Code Example

    gRPC Client Streaming

    gRPC Client Streaming

    Distributed transaction in microservices using Saga

    Distributed Transactions in Microservices: implementing Saga with Temporal

    Temporal Workflow Orchestration

    Workflow Orchestration with Temporal and Spring Boot

    GraphQL Directive

    GraphQL Directive

    Spring for GraphQL mutation

    Spring for GraphQL: Mutation

  • Spring Boot
    Spring Boot GraphQL service

    Getting started with Spring Boot GraphQL service

    Deploying a RESTful Spring Boot Microservice on Kubernetes

    Deploying a RESTful Spring Boot Microservice on Kubernetes

    RESTful Microservices with Spring Boot and Kubernetes

    RESTful Microservices with Spring Boot and Kubernetes

    RESTful API Gateway with gRPC

    RESTful API Gateway with gRPC

  • gRPC
    gRPC Bidirectional Streaming with Code Example

    gRPC Bidirectional Streaming with Code Example

    gRPC Client Streaming

    gRPC Client Streaming

    gRPC Interceptor: unary interceptor with code example

    gRPC Interceptor: unary interceptor with code example

    gRPC: synchronous and asynchronous Server streaming RPC

    gRPC: synchronous and asynchronous Server streaming RPC

    Photo by Ramón Salinero on Unsplash

    gRPC: synchronous and asynchronous unary RPC in Java

    Microservices inter-process communication using gRPC

    gRPC for microservices communication

  • GraphQL
    GraphQL Directive

    GraphQL Directive

    Spring for GraphQL mutation

    Spring for GraphQL: Mutation

    Spring for GraphQL: How to solve the N+1 Problem?

    Spring for GraphQL: How to solve the N+1 Problem?

    Spring GraphQL with @Controller, @SchemaMapping and @QueryMapping

    Spring for GraphQL : @SchemaMapping and @QueryMapping

    Spring Boot GraphQL service

    Getting started with Spring Boot GraphQL service

  • Kubernetes
    Deploying a RESTful Spring Boot Microservice on Kubernetes

    Deploying a RESTful Spring Boot Microservice on Kubernetes

    Components of Kubernetes Architecture

    Components of Kubernetes Architecture

    Helm Chart: quick start your app deployment on Kubernetes

    Helm Chart: quick start your app deployment on Kubernetes

    gRPC load balancing on Kubernetes (using Headless Service)

    gRPC load balancing on Kubernetes (using Headless Service)

    Getting started with Kind: quick start a multi-node local Kubernetes cluster

    Getting started with Kind: quick start a multi-node local Kubernetes cluster

    Getting started with Minikube: deploying application on local Kubernetes cluster

    Getting started with Minikube: deploying application on local Kubernetes cluster

  • Java
    Java Streams: Stream Operation with Examples

    Java Streams: Stream Operation with Examples

    Java Streams: stream creation with examples

    Java Streams: stream creation with examples

    Garbage Collection

    Super Fast Garbage Collectors in Java

    Calculus

    Functional Programming in Java

No Result
View All Result
Techdozo
No Result
View All Result
Home Microservices GraphQL

Spring for GraphQL: Mutation

Pankaj by Pankaj
August 14, 2022
in GraphQL
Reading Time: 13 mins read
1
A A
0
Spring for GraphQL mutation

Spring for GraphQL mutation

0
SHARES
2.4k
VIEWS
Share on FacebookShare on TwitterShare on Linkedin

In earlier articles, we talked about GraphQL queries. But, GraphQL is not just about queries.

Like any other API platform, a GraphQL service also needs to provide a way to manipulate data. This is where GraphQL mutation comes into the picture.

In theory, we can use GraphQL queries to modify the state but this will confuse our API consumers.

In REST, we use GET request to retrieve data, and by convention, it should not cause any side-effect. Similarly, we should not use GraphQL queries to modify the application’s state.

In this article

  • Read more about GraphQL…
  • What is GraphQL Mutation?
    • GraphQL Mutation Input Type
  • Implementing Mutations in Spring
  • Implementing Mutation with Input Type
  • Code Example
  • Conclusion

Therefore, if we need to provide APIs that change state, we should use mutation instead.

Read more about GraphQL…

Getting started with Spring Boot GraphQL service

Spring for GraphQL : @SchemaMapping and @QueryMapping

Spring for GraphQL: How to solve the N+1 Problem?

What is GraphQL Mutation?

In GraphQL, a mutation is used to insert, update or delete data. The mutation API is defined with the type Mutation rather than the Query.

GraphQL Mutation

An example of a mutation that adds a book to the book catalog.


type Mutation {
    addBook(name: String!, author: String!, publisher: String!,price: Float!): String!
}

In the above example, addBook API is a mutation; it allows you to add/save a book and returns the ID of the book after a successful save.

! means a required field.

Similarly, we can define API to update the book as:


type Mutation {
    updateBook(id: ID!, name: String, author: String, publisher: String, price: Float): String!
}

And, to delete the book:


type Mutation {
    deleteBook(id: ID!): String!
}

We can even design APIs to return info about added, updated, or deleted books as:


type Mutation {
    addBook(name: String!, author: String!, publisher: String!,price: Float!): BookInfo!
    updateBook(id: ID!, name: String, author: String, publisher: String,price: Float): BookInfo!
    deleteBook(id: ID!): BookInfo!
}

Where type BookInfo is defined as:


type BookInfo {
    id : ID
    name : String
    author: String
    publisher: String
    price: Float
}

In GraphiQL, you can add a book using mutation as:

Add Book Mutation

GraphQL Mutation Input Type

Instead of defining the API with scalar arguments, for example – addBook(name: String!, author: String!, publisher: String!,price: Float!), you can define a complex object called input type. This is useful if you want to reuse the input type for both updates and inserts.

An input type is defined with a keyword input instead of type as:


input BookInput {
    name : String
    author: String
    publisher: String
    price: Float
}

As a result, we can change the API definition as:


type Mutation {
    addBook(book: BookInput!): BookInfo!
    updateBook(id: ID!, book: BookInput!): BookInfo!
    deleteBook(id: ID!): BookInfo!
}

And, in GraphiQL (or other clients), we can call API as:

Implementing Mutations in Spring

In Spring for GraphQL, we can implement mutation using @SchemaMapping or @MutationMapping.

If we have defined addBook API as:


type Mutation {
    addBook(name: String!, author: String!, publisher: String!,price: Float!): String!
}

Then, we can implement mutation by defining @SchemaMapping with typeName as Mutation as:


@SchemaMapping(typeName = "Mutation", field = "addBook")
public String addBook(
    @Argument String author,
    @Argument String name,
    @Argument String publisher,
    @Argument Double price) {
  log.info("Saving book, name {}", name);
  var book = new BookInput(name, author, publisher, price);
  return bookCatalogService.saveBook(book);
}

Generally, we can leave the parameter field if the method name is the same as the field, as:


@SchemaMapping(typeName = "Mutation")
public String addBook(
    @Argument String author,
    @Argument String name,
    @Argument String publisher,
    @Argument Double price) {
  log.info("Saving book, name {}", name);
  var book = new BookInput(name, author, publisher, price);
  return bookCatalogService.saveBook(book);
}

Similar to @QueryMapping, Spring also provides a shorthand annotation as @MutationMapping. As a result, we can write addBook mutation as:


@MutationMapping
public String addBook(
    @Argument String author,
    @Argument String name,
    @Argument String publisher,
    @Argument Double price) {
  log.info("Saving book, name {}", name);
  var book = new BookInput(name, author, publisher, price);
  return bookCatalogService.saveBook(book);
}

As always, we can test the API in GraphiQL at http://localhost:8080/graphiql?path=/graphql

Implementing Mutation with Input Type

If a mutation is defined as


type Mutation {
    addBook(book: BookInput!): BookInfo!
}

Then, we can implement mutation by defining @MutationMapping and @Argument as:


@MutationMapping
public BookInfo addBook(@Argument BookInput book) {
  log.info("Saving book, name {}", book.name());
  return bookCatalogService.saveBook(book);
}

Code Example

The working code example of this article is listed on GitHub . To run the example, clone the repository, and import graphql-spring-mutation as a project in your favorite IDE as a Gradle project.

The code use Spring JPA to store data in the in-memory H2 database. You can find more information on READMe.md.

Conclusion

GraphQL mutation is used to change the application’s state (insert, update and delete). In Spring for GraphQL, a mutation can be implemented by defining a handler method and using annotation @MutationMapping or @SchemaMapping with the parameter typeName as Mutation.

Tags: graphqlmicroservicesspring-boot
Previous Post

Spring for GraphQL: How to solve the N+1 Problem?

Next Post

GraphQL Directive

Pankaj

Pankaj

Software Architect @ Schlumberger ``` Cloud | Microservices | Programming | Kubernetes | Architecture | Machine Learning | Java | Python ```

Related Posts

GraphQL Directive
GraphQL

GraphQL Directive

September 29, 2022
Spring for GraphQL: How to solve the N+1 Problem?
GraphQL

Spring for GraphQL: How to solve the N+1 Problem?

August 7, 2022
Spring GraphQL with @Controller, @SchemaMapping and @QueryMapping
GraphQL

Spring for GraphQL : @SchemaMapping and @QueryMapping

July 29, 2022
Spring Boot GraphQL service
GraphQL

Getting started with Spring Boot GraphQL service

July 23, 2022

Discussion about this post

Recent Articles

gRPC Bidirectional Streaming with Code Example

gRPC Bidirectional Streaming with Code Example

February 17, 2023
gRPC Client Streaming

gRPC Client Streaming

January 20, 2023
Distributed transaction in microservices using Saga

Distributed Transactions in Microservices: implementing Saga with Temporal

November 8, 2022
Temporal Workflow Orchestration

Workflow Orchestration with Temporal and Spring Boot

October 29, 2022
  • Trending
  • Comments
  • Latest
Deploying a RESTful Spring Boot Microservice on Kubernetes

Deploying a RESTful Spring Boot Microservice on Kubernetes

August 18, 2021
Temporal Workflow Orchestration

Workflow Orchestration with Temporal and Spring Boot

October 29, 2022
gRPC Interceptor: unary interceptor with code example

gRPC Interceptor: unary interceptor with code example

April 30, 2022
Microservices inter-process communication using gRPC

gRPC for microservices communication

August 29, 2021
Calculus

Functional Programming in Java

0
Java Streams: stream creation with examples

Java Streams: stream creation with examples

0
Garbage Collection

Super Fast Garbage Collectors in Java

0
Java Streams: Stream Operation with Examples

Java Streams: Stream Operation with Examples

0
gRPC Bidirectional Streaming with Code Example

gRPC Bidirectional Streaming with Code Example

February 17, 2023
gRPC Client Streaming

gRPC Client Streaming

January 20, 2023
Distributed transaction in microservices using Saga

Distributed Transactions in Microservices: implementing Saga with Temporal

November 8, 2022
Temporal Workflow Orchestration

Workflow Orchestration with Temporal and Spring Boot

October 29, 2022
Facebook Twitter Pinterest

TECHDOZO

Simplifying modern tech stack!

Browse by Category

  • Bitesize
  • GraphQL
  • gRPC
  • Java
  • Kubernetes
  • Microservices
  • Spring Boot

Recent Articles

gRPC Bidirectional Streaming with Code Example

gRPC Bidirectional Streaming with Code Example

February 17, 2023
gRPC Client Streaming

gRPC Client Streaming

January 20, 2023

© 2023 Techdozo.

No Result
View All Result
  • Home
  • gRPC
  • Kubernetes
  • Microservices
  • GraphQL

© 2023 Techdozo.

Welcome Back!

Sign In with Google
OR

Login to your account below

Forgotten Password?

Retrieve your password

Please enter your username or email address to reset your password.

Log In