Connecting GraphQL with Java Spring Boot and Postgres or MySQL

Connecting GraphQL with Java Spring Boot and Postgres or MySQL hackernoon.com3 months ago in #Dev Love46

@tamizhvendan Pragmatic, Passionate & Polyglot Programmer | Full Stack Solution Provider GraphQL 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. In this blog post, I am going to introduce GraphQLize, a JVM library for developing GraphQL API server instantly from any Postgres and MySQL databases. 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 Spring Initializr and create a Java project with Web & JPA as dependencies. This documentation uses this Spring Initializr template. Adding Dependencies The first step is to add the graphqlize-java & the JDBC driver dependencies. Initialising GraphQLizeResolver The next step is initialising GraphQLizeResolver . To do it, let’s a create new file and add the following code to expose the GraphQLizeResolver as a spring-boot bean. package; import; import org.springframework.context.annotation.Bean; import org.springframework.stereotype.Component; import javax.sql.DataSource; @Component public class GraphQLizeResolverProvider { private final DataSource dataSource; private final GraphQLizeResolver graphQLizeResolver; public GraphQLizeResolverProvider(DataSource dataSource) { this.dataSource = dataSource; graphQLizeResolver = new GraphQLizeResolver(dataSource); } @Bean public GraphQLizeResolver graphQLizeResolver() { return this.graphQLizeResolver; } } During initialisation, the  GraphQLizeResolver  reads the metadata of the database using the JDBC metadata APIs and keeps an in-memory representation of them.  » Read More

Like to keep reading?

This article first appeared on If you'd like to keep reading, follow the white rabbit.

View Full Article

Leave a Reply