{{announcement.body}}
{{announcement.title}}

Implement GraphQL With Spring Boot by Connecting to Oracle

DZone 's Guide to

Implement GraphQL With Spring Boot by Connecting to Oracle

In this tutorial, see how to implement GraphQL with Spring Boot by connecting to Oracle DB.

· Database Zone ·
Free Resource

Introduction

GraphQL is a query language for APIs. According to Wikipedia, "GraphQL was developed internally by Facebook in 2012 before being publicly released in 2015." 

There are many benefits with GraphQL:

  • Ask for what you need and get it exactly
  • Get many resources in a single request
  • Improved performance

Key Concepts

  • Schema (Query and Mutation)
  • Resolvers

Schema

Determines what data you can fetch from querying. Here, we have types and relationships also.  

Query

This is similar to a GET request in REST APIs. We will use Query for fetching the data.

Mutations

They are used to create, delete, and update data. Similar to POST, DELETE, and PUT in REST.

You might also like: GraphQL: Core Features, Architecture, Pros, and Cons

Consider that we have a table called Person in the Oracle DB with fields being id, name, firstName,  lastName, and email. Now the schema looks like below:

person.graphqls

Java

 

We can see from the example that the getperson method is written in Query. It fetches data based on the id, whereas the setperson method is written in the Mutation. It creates a new person. 

Resolvers

Now we need to write the QueryResolver and mutationResolver to implement the methods in the schema's query and mutation.

Adding Dependency

In this article, we are using Spring Boot. To support GraphQL in Spring Boot, we need to add some dependencies in pox.xml:

XML


Also, add the oracle dependencies:

XML


To Connect to Remote Oracle DB

In application.properties, add the following:

Properties files

 

We are now connected to remote Oracle DB and have added GrapQL and Oracle dependencies. Now we will add the person schema to the person.graphqls file. 

Create an Entity that should match both the table in the DB and the schema in perso.graphqls.

Import javax.persistence.*;

Java


Create a repository for the above entity:

Java

 

Now we need to write a resolver. 

QueryResolver

Java

 

MutationResolver:

Java

 

Application starter:

Java

 

We are done with the coding part.

Steps

  1. Create a person.graphqls file with schema
  2. Connected to remote oracle db from application.properties
  3. Create a entity for table person matching the schema
  4. Implemented repository for  the Entity
  5. Written the query and mutation resolver.
  6. Testing 

Navigate to http://localhost:8080/graphiql.

Write the query:

Java

 

Execute.

You're done. Thanks for reading!

Further Reading

Why and When to Use GraphQL

An Overview of GraphQL

Topics:
graphql ,java ,oracl ,hibernate ,jpa ,database ,tutorial ,spring boot ,apis ,schema

Published at DZone with permission of sai kumar javvaji . See the original article here.

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}