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

Popular Posts
1Z0-830 Java SE 21 Developer Certification
Azure AI Foundry Hello World
Azure AI Agent Hello World
Foundry vs Hub Projects
Build Agents with SDK
Bing Web Search Agent
Function Calling Agent
Spring Boot + Azure Key Vault Hello World Example
Spring Boot + Elasticsearch + Azure Key Vault Example
Spring Boot Azure AD (Entra ID) OAuth 2.0 Authentication
Deploy Spring Boot App to Azure App Service
Secure Azure App Service using Azure API Management
Deploy Spring Boot JAR to Azure App Service
Deploy Spring Boot + MySQL to Azure App Service
Spring Boot + Azure Managed Identity Example
Secure Spring Boot Azure Web App with Managed Identity + App Registration
Elasticsearch 8 Security - Integrate Azure AD OIDC