Working with GraphQL Mutation | Part2

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 :-

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

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

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 :-

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 :-

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 application.properties 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.

--

--

Software Engineer for Big Data distributed systems

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store