is revolutionising the way developers build APIs. It lets you query precisely what you want. Nothing more, nothing less! It also gives you the flexibility to query related objects in a single round trip, unlike the REST APIs. GraphQL In this blog post, I am going to introduce , a JVM library for developing GraphQL API server instantly from any Postgres and MySQL databases. GraphQLize Why GraphQLize? In a nutshell, it aims to simplify the effort required to expose GraphQL APIs over relational databases. In the JVM ecosystem, developing GraphQL APIs to expose the data from the relational databases requires a lot of manual work. Right from defining the GraphQL schemas (either code-first or schema-first) to wiring them with resolvers and the database access logic, we spend a significant amount of our development time. In addition to this, we also need to take care of optimising the underlying SQL queries to avoid problems like N+1 queries. We have to account the maintenance of the resulting codebase as well! GraphQLize will help you to overcome all these shortcomings. It provides you with an efficient GraphQL implementation in just a few lines of code. What is GraphQLize? GraphQLize is a JVM library written in Clojure with Java interoperability. The crux of GraphQLize is generating the GraphQL schema and resolving the queries by making use of JDBC metadata provided by the JDBC drivers. It currently supports Postgres (9.4 & above) and MySQL (8.0 & above). Getting Started One of the core design goals of GraphQLize is not to tie to any web development framework and remain as a drop-in JVM library in any JVM languages like Java, Scala, Clojure or Kotlin. Getting started with GraphQLize is simple and involves only a few steps. Add the GraphQLize dependency in your project. Initialise GraphQLize Resolver by providing the Java . SQL data source Add a GraphQL API endpoint and use the initialised GraphQlize Resolver in the previous step. The actual implementation of these steps will vary based on which language (Java, Kotlin, Clojure, Scala) and framework (Spring Boot, Ktor, Pedestal, Scalatra, etc.). In this blog post, we are going to look at how to use GraphQLize in a Java spring-boot project to build a GraphQL API. Getting Started With Spring Boot As we typically do, let's go to and create a Java project with Web & JPA as dependencies. This documentation uses this Spring . Spring Initializr Initializr template Adding Dependencies The first step is to add the & the JDBC driver dependencies. graphqlize-java Initialising GraphQLizeResolver The next step is initialising . To do it, let's a create new file and add the following code to expose the as a spring-boot bean. GraphQLizeResolver GraphQLizeResolverProvider.java GraphQLizeResolver org.graphqlize.java.springboot; org.graphqlize.java.GraphQLizeResolver; org.springframework.context.annotation.Bean; org.springframework.stereotype.Component; javax.sql.DataSource; { DataSource dataSource; GraphQLizeResolver graphQLizeResolver; { .dataSource = dataSource; graphQLizeResolver = GraphQLizeResolver(dataSource); } { .graphQLizeResolver; } } package import import import import @Component public class GraphQLizeResolverProvider private final private final public GraphQLizeResolverProvider (DataSource dataSource) this new @Bean GraphQLizeResolver public graphQLizeResolver () return this During initialisation, the reads the metadata of the database using the JDBC metadata APIs and keeps an in-memory representation of them. GraphQLizeResolver Configuring DataSource To configure the DataSource, let's add the following properties in the file. application.properties For Postgres, = = = spring.datasource.url jdbc:postgresql://localhost:5432/sakila spring.datasource.username postgres spring.datasource.password postgres For MySQL, = = = spring.datasource.url jdbc:mysql://localhost:3306/sakila spring.datasource.username root spring.datasource.password mysql Make sure you are changing the above values to refer your database connection. The above example assumes that you are using the database created from this . sakila JOOQ's example repository Adding GraphQL Endpoint The final step is exposing an API endpoint for handling the GraphQL request. To do it, let's create a new file and do the following. GraphQLController.java Create a POJO GraphQLRequest for deserialising GraphQL request from the client. Create a Controller class with a GraphQLResolver dependency. Create a method inside this class to handle the GraphQL request org.graphqlize.java.springboot; org.graphqlize.java.GraphQLResolver; org.springframework.http.*; org.springframework.web.bind.annotation.*; java.util.Map; { String query; Map<String, Object> variables; } { GraphQLResolver graphQLResolver; { .graphQLResolver = graphQLResolver; } ( ) { String result = graphQLResolver.resolve( graphQLRequest.getQuery(), graphQLRequest.getVariables()); ResponseEntity.ok() .header(HttpHeaders.CONTENT_TYPE, ) .body(result); } } package import import import import class GraphQLRequest private private // ... Getters & Setters are ignored for brevity @RestController public class GraphQLController private final public GraphQLController (GraphQLResolver graphQLResolver) this @PostMapping "/graphql" ResponseEntity public handle (@RequestBody GraphQLRequest graphQLRequest) return "application/json" Handling the GraphQL request is as simple as highlighted above. Get the query & the variables from the request and invoke the method on the initialised instance of . resolve GraphQLizeResolver It returns the as stringified JSON, and we are sending it as response body with the content type as . result application/json Test Drive To do a test drive of this implementation, start the server and hit the endpoint via curl. > curl -X POST \ --data \ -H \ http://localhost:8080/graphql '{"query": "query { actorByActorId(actorId: 1){firstName}}"}' "Content-Type: application/json" You'll get a response like below. { : { : { : } } } "data" "actorByActorId" "firstName" "PENELOPE" GraphQL Playground and Voyager With the GraphQL endpoint up and running, the next step is introspecting the GraphQL schema and try out some more queries. To introspect, we are going to make use of , a tool to visualise GraphQL API as an interactive graph. Adding it to our project is easy thanks to of Spring Boot. Voyager static content serve capability All you need to do is download this file and put it under the directory. voyager.html src/main/resources/static When you restart the server, the Voyager will be available at . A sample output would look like . http://localhost:8080/voyager.html this Then to interact with the GraphQL API, let's add the . Like Voyager, download this file and put in the directory. GraphQL Playground playground.html static This GraphQL playground will be available at after server restart. http://localhost:8080/playground.html Next Steps Congrats! You are on course to build impressive applications using GraphQLize in less time. To save yourself some more time, do refer to know more about how GraphQLize generates the GraphQL schema and the queries. this documentation The sample code is available in . this GitHub Repository Summary In this blog post, we experience a glimpse of how GraphQLize can simplify your efforts in creating GraphQL API to expose the data from Postgres or MySQL. GraphQLize is at its early stages now and lacks some critical features to call it production-ready. In the next few months it should be ready for real-world use cases. You can keep track of the progress by Following the . GraphQLize Twitter account Joining . GraphQLize's Discord Subscribing to . GraphQLize's newsletter ⭐️ If you like GraphQLize, give it a star on GitHub ! ⭐️ Previously published at https://dev.to/tamizhvendan/graphql-with-java-spring-boot-and-postgres-or-mysql-made-easy-3p9n