Spring Boot + GraphQL Tutorial - Understanding GraphQL Query
Implementation
Previously we were calling the helloWorld method which used to return the hello world string. We will be modifying the Spring Boot + GraphQL example we had implemented in previous tutorial. The Maven Project will be as follows-
Passing request parameter using GraphQL
Define a method named getEmployeeName which takes the empId parameter and returns the name for the Employee.
package com.javainuse.query;
import org.springframework.stereotype.Component;
import com.javainuse.model.InputRequest;
import graphql.kickstart.tools.GraphQLQueryResolver;
@Component
public class Query implements GraphQLQueryResolver {
public String helloWorld() {
return "Hello World";
}
public String getEmployeeName(String empId) {
return "TestEmployee for employee id " + empId;
}
}
Next we will be defining the getEmployeeName method in the GraphQL schema -
type Query{
helloWorld : String
getEmployeeName(empId : String): String
}
Start the Spring Boot Application. If we now go to the GraphQL UI using http://localhost:8080/graphiqlWe will be creating the input request as follows-
query{
getEmployeeName(empId : "emp1")
}

Using GraphQL pass JSON object as request parameter and get JSON object as response
We will be creating a method which takes a object InputRequest and returns object of type Employee.Create the InputRequest class as follows-
package com.javainuse.model;
public class InputRequest {
private String empId;
private boolean present;
public String getEmpId() {
return empId;
}
public void setEmpId(String empId) {
this.empId = empId;
}
public boolean isPresent() {
return present;
}
public void setPresent(boolean present) {
this.present = present;
}
}
Create the Employee class as follows-
package com.javainuse.model;
public class Employee {
private String empId;
private String empName;
public String getEmpId() {
return empId;
}
public void setEmpId(String empId) {
this.empId = empId;
}
public String getEmpName() {
return empName;
}
public void setEmpName(String empName) {
this.empName = empName;
}
}
Modify the Query class to create the method which takes the InputRequest as a method parameter and returns the Employee class-
package com.javainuse.query;
import org.springframework.stereotype.Component;
import com.javainuse.model.Employee;
import com.javainuse.model.InputRequest;
import graphql.kickstart.tools.GraphQLQueryResolver;
@Component
public class Query implements GraphQLQueryResolver {
public String helloWorld() {
return "Hello World";
}
public String getEmployeeName(String empId) {
return "TestEmployee for employee id " + empId;
}
public Employee getEmployeeNameIfPresent(InputRequest inputRequest) {
Employee emp = new Employee();
emp.setEmpId(inputRequest.getEmpId());
emp.setEmpName("TestEmployee");
return emp;
}
}
Finally modify the GraphQL schema as follows-
type Query{
helloWorld : String
getEmployeeName(empId : String): String
getEmployeeNameIfPresent(inputRequest : InputRequest): Employee
}
input InputRequest {
empId : String
present : Boolean
}
type Employee {
empId : String
empName : String
}
Start the Spring Boot Application. If we now go to the GraphQL UI using http://localhost:8080/graphiqlWe will be creating the input request as follows-
query{
getEmployeeNameIfPresent(inputRequest:{
empId:"emp11"
present:true
}) {
empId
empName
}
}
