This article will guide you to build your first GraphQL application in java step by step.
1. Initialize a project
Generate a springboot project here and add graphql-spring-boot-starter
andgraphql-java-tools
into dependencies.
2. Prepare GraphQL Schema
We start by adding a simple schema with a type
Customer and a query
for it. Name this file with a .graphqls
suffix and place it under resources
directory. The GraphQL spring boot start will pick it up and configure everything automatically.
type Customer {
id: ID!
name: String!
age: Int!
}
type Query {
customer(id: ID): Customer!
}
3. Add POJO to represent type
Create a POJO class to represent the Customer
type that we defined in the schema file.
public record Customer(String id, String name, int age) {
}
4. Add a query resolver
The query resolver is the actual component that handles the incoming GQL query. In the example the method getCustomer
takes in a id
as argument and sends back a Customer
as result. CustomerDao
is the class the queries data from data source.
@Component
@AllArgsConstructor
public class CustomerQuery implements GraphQLQueryResolver {
private final CustomerDao customerDao;
public Customer getCustomer(String id) {
return customerDao.getCustomer(id);
}
}
5. Run the application and test query
Now the code is ready. Let’s spin the application. Run the application on your local machine and test the GraphQL interface with this curl:
curl -X POST --location "http://localhost:8080/graphql" \
-H "Content-Type: application/json" \
-d "{
\"query\": \"{ customer(id: 1) { id name age } } }\"
}"
And you can get this result back:
{
"data": {
"customer": {
"id": "1",
"name": "name",
"age": 18,
}
}
}
Bingo! We have managed to make the first GraphQL query.
You can also open http://localhost:8080/graphiql in browser to make the query. In order to do this you need to add graphiql-spring-boot-starter
in the dependencies.
6. Add a mutation in schema
The next step is to achieve data mutation with GraphQL. To do this we need to add a root mutation into the schema file first.
type Mutation {
addCustomer(name: String!, age: Int!) : Customer!
}
7. Add a mutation resolver
Correspondingly, we also need to add some code to accomplish mutation.
Here the addCustomer
method represents the mutation that we just added in the schema file. And it will call CustomerDao
to save the data.
@Component
@AllArgsConstructor
public class CustomerMutation implements GraphQLMutationResolver {
private final CustomerDao customerDao;
public Customer addCustomer(String name, Integer age) {
return customerDao.saveCustomer(name, age);
}
}
8. Test mutation
Run this curl that tries to add a customer:
curl -X POST --location "http://localhost:8080/graphql" \
-H "Content-Type: application/json" \
-d "{
\"query\": \"mutation {addCustomer(name: \\\"John\\\", age: 18) {id name age}}\"
}"
And the return will look like this:
{
"data": {
"addCustomer": {
"id": "id",
"name": "John",
"age": 18
}
}
}
Alright, that’s it. We have built a java application that can get and create customer. Enjoy your first GraphQL application!
You can find the complete java project here.