- Java 11+
- Spring Boot 2.1.7+
- GraphQL Spring Boot starter
- GraphiQL tool to execute graphql queries
The idea is if any UI or Service wanted specific data, it could use GraphQL as a tool to filter out unnecessary data that we might not want the client to see/have. For example in this architecture, we have our University microservice which contains all university student details. Normally you would have another service consuming this University microservice with a getAllStudents() method and then the UI/Client would have to do all the filtering of that data. However, with our GraphQL microservice here, it will query our Univeristy microservice and filter out the data for our UI/client to consume. In our example, GraphQL will only return student data given an existing student name instead of the client getting all student data and then performing the filtering.
- To start the
Universityapplication just navigate into the university app directory containing thebuild.gradlefile and just start the app by running the command./gradlew bootRunand it should start the application on port 8081. - You can test that you are recieving a list of
Studentswith degrees and tutors by calling the following API:http://localhost:8081/api/university/students
and you should have an output as below:
[
{
"name": "artemas",
"surname": "muzanenhamo",
"degree": {
"degree": "software engineering"
},
"tutor": {
"name": "jessica",
"surname": "stevens",
"degree": {
"degree": "software engineering"
}
}
},
{
"name": "thomas",
"surname": "jenkins",
"degree": {
"degree": "bio-chemistry"
},
"tutor": {
"name": "lavern",
"surname": "masoja",
"degree": {
"degree": "bio-chemistry"
}
}
},
{
"name": "sarah",
"surname": "smith",
"degree": {
"degree": "computer science"
},
"tutor": {
"name": "randal",
"surname": "macbeth",
"degree": {
"degree": "computer science"
}
}
}
]-
Navigate to the
graphqlapplication directory containing thebuild.gradlefile and you should start the application with the following command:./gradlew bootRun. The application should be started on port 8080 -
You will need a GraphQl client which will enable you to run queries against the data returned from the
graphqlapplication. There are a few clients as below: -
Download any of the clients above and you should be able to access the graphql endpoint at:
http://localhost:8080/graphql. -
I have two top queries:
- Get Student By Name - This query returns a student given an existing valid name
type Query { getAllStudents: [Student] }
- Below is an example to execute this query:
{ getStudentByName(name: "artemas") { name surname degree { degree } tutor{ name surname degree { degree } } } }- Get All Students - This query will return all the students. This data matches what is returned by the
Universityapplication.
type Query { getAllStudents: [Student] }- Below is how you execute this query:
{ getAllStudents { name surname degree { degree } tutor { name surname degree { degree } } } }
