Working with GraphQL Query | Part1

Question :- What is GraphQL ?

Question :- What is the difference between the GraphQL and REST API ?

Question :- What are the dependencies required for GraphQL, while working with Java language ?

Question :- Demonstrate the simple example for Plain Query using GQL ?

Step #1.) We create a Query as shown below :-

Step #2.) We now create a schema file as shown below. Note that :-

  • The return type of this GQL-Query also needs to be specified, which in this case is String.

Step #3.) We can now start this spring boot application and as a result, we get to see the UI as shown below :-

Question :- Demonstrate another simple example for another Query using GQL ?

Step #1.) We create a second Query as shown below :-

Step #2.) We now create a schema file as shown below. Note that :-

  • The return type of this GQL-Query also needs to be specified, which in this case is String.

Step #3.) We can now restart this spring boot application and as a result, we get to see the UI as shown below :-

Step #4.) We can see the query option, which exists within our GQL :-

Step #5.) Upon clicking on the query, we can go inside the query, to see all possible query which we can use :-

Question :- Demonstrate the use of Altair Plugin ?

Answer → There also exists an Altair plugin into the chrome browser. We can access our GraphQL through this as well :-

Question :- Demonstrate another example for some new Query using GQL ?

Step #1.) We create a third Query as shown below with name as “fullName” :-

Step #2.) We now create a schema file as shown below. Note that :-

  • The three parameters that this query takes-in, has also been shown as the parameters to this query in the schema file below.
  • The return type of this GQL-Query also needs to be specified, which in this case is String.

Step #3.) We can now restart this spring boot application and as a result, we get to see the UI as shown below. Note that, all the possible query names are auto-suggested :-

Step #4.) We can now fire a request to this GQL query named “fullName” and get to see the result as shown below :-

Question :- Can we define a new object, which shall be passed as request in the GQL ?

Step #1.) We modified the above third Query as shown below with name as “fullName”. This query receives the object of type SampleRequest in the request :-

Step #2.) We can define the Object definition as below :-

Step #3.) We now create a schema file as shown below. Note that :-

  • The one parameter (SampleRequest)that this query takes-in, has also been shown as the parameter to this query in the schema file below.
  • The return type of this GQL-Query also needs to be specified, which in this case is String.
  • Also, at line no. 7, we have defined the schema of this Object in the schema file.

Step #3.) We can now restart this spring boot application and as a result, we get to see the UI as shown below. Note that, all the possible query names are auto-suggested :-

Question :- What would happen, if we don’t supply one of the parameter in the GQL request ?

Question :- How can we make a particular parameter as a mandatory parameter ?

Answer → By putting the exclamatory symbol (!) behind the particular parameter.

Now, if we try to invoke this GQL, without this mandatory parameter (firstName), it shall throw us an error :-

Question :- How can we change the endpoint URL of our GQL application ?

So, now we can open the GraphQL UI at this address :-

And, we can also access the GraphQL application through Altair plugin :-

Question :- Demonstrate the full working of student-service through the GQL ?

Step #1.) We have following tables in our mysql database :-

Step #2.) Let’s configure the mysql database into our application :-

Step #3.) Let’s add our dependencies, to use mysql along with SpringBoot application :-

Step #4.) Let’s define our repositories and service layer now :-

Step #5.) We would now declare the repository package into our components scan :-

Step #6.) We now create our fourth Query as shown below with name as “student”. This query receives the parameter of type long :-

Step #7.) This is how, we define the object of “StudentResponse” :-

Step #8.) We now declare the schema of this GQL :-

  • At line no. 13, we defined the response object of this GQL-Query.
  • Note that, the fields are not mandatory.

Step #9.) We can now restart our application and access our newly created GQL query. Note following aspects :-

  • We are interested in fetching only the 2 fields only i.e. id and firstName.

Question :- Say, we now want to fetch field : lastName along with fields : id and firstName as well from this GQL :-

Question :- Say, we now only want to fetch a particular field “firstName” from the GQL Query :-

Question :- Let’s now also get the subjects associated with particular student in the response ?

Step #1.) We first define the information into the schema definition of our GQL Query :-

Step #2.) We can now query and fetch our subjects information as well now :-

Question :- Say, we also want to fetch other attributes of each of subject :-

Answer → Note that, while querying through GQL, we always uses POST type of requests.

Question :- Show a full-fledged GQL-Query for our Student-Service application ?

Question :- Can we access/hit our GQL-Query through the Postman client ?

That’s all in this series. We would meet you in next part of this series.

--

--

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