Working with GraphQL Mutation | Part2

aditya goel
5 min readDec 16, 2022

In case you are landing at this page directly, it’s recommended that you read through the Part 1 first here.

Question :- What is GraphQL Mutation used for ?

Answer → We use mutation in order to save/write some entity with the backend. This is similar to the POST verbatim of RESTful APIs.

Question :- Demonstrate the implementation of the GQL based Mutation ?

Step #1.) We first create a Mutation, where we define the name of mutation and it’s parameter.

Step #2.) Below is how the definition of the ‘CreateStudentRequest’ looks like :-

Step #3.) Below is how the definition of the ‘CreateSubjectRequest’ looks like :-

Step #4.) Below is how the definition of the ‘studentService.createStudent(CreateStudentRequest)’ looks like :-

  • First, we save the Address object, corresponding to this request.
  • Second, we save the Student object into the database.
  • Third, we save the subjects corresponding to this student.

Step #5.) Next, we declare the package-path into the ComponentScan :-

Step #6.) Now, we define the schema definition for this mutation :-

  • Note that, the name of this mutation is same as the one that we created in step 1 above.
  • In Request, it takes in an object of CreateStudentRequest.
  • In Response, it returns the object of StudentResponse.

Step #7.) Let’s define the schema the above request as well : CreateStudentRequest object : We receive as input to the GQL. Note that, there is a nested object with name as CreateSubjectRequest, to which we have also defined in the same file as below :-

Step #8.) Recall that, we had already created the object of StudentResponse as part of the GraphQL schema.

Step #10.) Now restart the application and we can observe that, we can see the mutation coming in auto-suggest as well now :-

Step #11.) We can also specify all those fields, against which we wish to save the values.

Step #12.) Here is we have defined the full fledged request for this Mutation :-

  • In lines 2 to 17, we define the field-values for the student object, for which we wish to save the values.
  • In lines 19 to 31, we are specifying the query fields, which we wish to fetch from the GQL-Query after this mutation have been executed.

Step #13.) Let’s test out the newly object by querying the same to the GQL-Query :-

Question :- Can you demonstrate some Client, which can query to GQL ?

Answer → GQL-Query/Mutation can have various clients :-

  • PostMan → We demonstrated in Part1 here.
  • AltAir plugin → We demonstrated in this blog.
  • Any Restful Application → We would show next now.

Here is what we are going to do next :-

Step #1.) Here is the dependency that we would need to do the above thing :-

Step #2.) Below is how we define our RESTful GET & POST APIs :-

Step #3.) Below is how we define the mutation request :-

Step #4.) Below is how we would be using this mutation : Note that, all the requests to GQL are always the POST type :-

Question :- Can we also deploy our application at Heroku ?

Step #1.) Let’s create our free account at Heroku and install MYSQL application at it :-

Step #2.) Next, let’s try to access mysql through our MySql client :-

Step #3.) Next, let’s login on our heroku server :-

Step #4.) Let’s install JAVA now at this Heroku server :-

Step #5.) Now, we configure the DB properties in our file :-

Step #6.) Now, we deploy the JAR at Heroku server :-

We can now access the application through the application URL as mentioned above.

That’s all in this series, we shall see you in next blog.