This article is the first step towards using GraphQL inside a Spring application.
But first, let's know more about GraphQL. According to Red Hat, GraphQL is a query language and server-side runtime for application programming interfaces (APIs) that focuses on providing clients with only the information they need.
GraphQL was created with the goal of making APIs that are quick, versatile, and developer-friendly. It may even be used within the GraphiQL integrated development environment (IDE). GraphQL, a REST alternative, allows developers to create queries that pull data from various sources in a single API call.
Additionally, GraphQL allows API administrators to add or remove fields without affecting existing queries. Developers may use any techniques they choose to create APIs, and the GraphQL definition will ensure that they work consistently for clients.
The project we'll be building is a simple school/student find all endpoint — only this time using GraphQL which allows us to change and mutate our response data structure.
The student will have an id, name, email, and school, each school has an id and a name.
Now we created our controllers for the student and for the school with a simple findAll
endpoint, starting with the student's:
The school controller will be similar to the student's:
And of course, we need our GraphQL schema to be set, inside the resources folder we created another name graphql which contains a file under the name schema.graphqls (resources>graphql>schema.graphqls).
The schema should look like this:
Finally to the results! You can use the GraphiQL interface on http://localhost:8080/graphiql.
Let's try and retrieve the list of students only showing their names:
Now, let's have them by id, name, and email:
Let's add their schools to the results:
Also, we can retrieve the schools:
And of course, we can retrieve students
and schools
with the same query:
GraphQL is a significant benefit for any project due to its result manipulation; this is simply the first step toward GraphQL usage under the Spring umbrella!
Kindly, find the source code in my GitHub Repository here.
Blossoming Intelligence: How to Run Spring AI Locally with Ollama
In this short article, we'll look at how easy it is to create a chat bot backend powered by Spring and Olama using the llama 3 model.
Sat, 11th May 2024
Read MoreGetting started with native java apps with GraalVM
Native Image is a technology to ahead-of-time compile Java code to a standalone executable, called a native image. This executable includes the application classes, classes from its dependencies, runtime library classes, and statically linked native code from JDK.
Wed, 10th April 2024
Read MoreLeveraging Spring Reactive, Functional Endpoints, Docker, and MongoDB
Blocking is a feature of classic servlet-based web frameworks like Spring MVC. Introduced in Spring 5, Spring WebFlux is a reactive framework that operates on servers like Netty and is completely non-blocking. Two programming paradigms are supported by Spring WebFlux. Annotations (Aspect Oriented Programming) and WebFlux.fn (Functional Programming).
Thu, 29th February 2024
Read MoreNextJs meets Redux: A simple user PoC
Redux is a powerful state management library primarily used in JavaScript applications, particularly those built with frameworks like React. At its core, Redux provides a predictable state container for managing the state of an application in a more organised and centralised manner. It operates on a unidirectional data flow model, which helps in maintaining the consistency of application state and facilitates easier debugging and testing.
Thu, 15th February 2024
Read MoreMonitor Spring reactive microservices with Prometheus and Grafana: a how-to guide
Micro-services monitoring is a crucial aspect of managing modern, complex software architectures. Unlike traditional monolithic applications, micro-services break down functionality into smaller, independent services that can be developed, deployed, and scaled independently.
Fri, 27th October 2023
Read More