Introduction to GraphQL and implementation in Java
Tags: java, graphql, api
Published: 2023-03-24 10:48:58

---
title: Introduction to GraphQL and implementation in Java
published: true
description: A GraphQL introduction for beginners
tags: java, graphQL, API
# cover_image: ![graphQL cartouche](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7q6e7fu16cmnod9jyg9x.png)
# published_at: 2023-03-24 10:25 +0000
---


## Introduction

![GraphQl cartouche](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7q6e7fu16cmnod9jyg9x.png)
	GraphQL is an open-source data query language and data manipulation language for Apis, and a query runtime engine.

GraphQL started GraphQL development in 2012 and released it in 2015 as an open source project.


GraphQL is a specification for communicating with a server. Communication is done by sending "Operations" requesting data or requesting an action to be done.

There are 3 types of Operations : 

-   `query` retrieves data
-   `mutation` changes and retrieves data
-   `subscription` ask the server to send data when a certain event occurs

  

## Why you should use it ?

  
It depends on your use case, but usually, GraphQL has features that stands out such as :

-  aggregate data from multiples interface components
-  create a natural data representation (a graph)
-  it reduces the need for update using its deprecation system
-  ensure data is righly typed
-  you have access to its ecosystem (code generation, linting, analitycs ...)

  

  

## Does it replace Rest ? :

  Schema from the official documentation :

It illustrate how GraphQL can limit API calls versus what does REST. 
In the first case , with REST you need 3 calls. With GraphQL, you can do it with only one call.
It mean that the API user decide which data needs to be retrieved and in which form.
  ![Rest API calls schema](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7dc9qax3rr7cotctjns6.png)

![GraphQl API calls schema](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bg6x7fhquw4m8nsx7alr.png)

No. Not necessarilly. Both can manipulate APIs and serve a similar goal from a fonctional perspective.
GraphQL is often viewed as an alternative to Rest, but it is not a definitive replacement.

GraphQL an REST can coexist in the same stack. For example, it is possible to put REST APIs behind an abstraction  GraphQL server.



## Examples

  

### Adding dependencies :

  
Multiples GraphQL implementations exist for most backend server languages. Here, for my example, i chose the DGS implementation from Netflix (thank you Netflix). 

[https://netflix.github.io/dgs](https://netflix.github.io/dgs)

So i add the gradle dependencies :
  ![Gradle dependencies](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/aze9nodzw8txs3jz9cd2.png)

### Schema :

You will need first to create your schema configuration file :

![Ressource path](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ih2pcnqf1zjdsus0r7yp.png)


  ![schema file](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3sgsvfq57864ks12l6vm.png)


![Configuration path](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/eqcr3y98lsr7ak9mka02.png)


  

### Implementation :


We will create a "DgsComponent" as follows. This will allows the API to access the data (here a List od show, but we could access a database or any datasource), and filter the data as needed.
  ![Datafetcher](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6we6u83tw4953x02z0or.png)

The class representing the data to retrieve : 
![Class to fetch](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pf13k2azsdsnxotun9zw.png)
  

### **Calling APIs with Insomnia :**

  ![Insomnia call](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wi8ss7weikdguoaiixxl.png)

### **GUI :

A GUI is provided if you need it, it is very handy.

![Gui](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wr4y1xmzt3pl65bp5sab.png)
  

## Adding Actors

We change the schema configuration to add our Actors :
  ![Actor configuration](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hq5177ooqtdwsetfn6dn.png)

	
In the DataFetcher

  
![actor data](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/acaip8dyyk4xvhgwksp5.png)


  ![Actor Datafetcher](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/acaip8dyyk4xvhgwksp5.png)

  
![Insomnia call for actors](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/se02xiklj52phtp5butm.png)


## You should also check this :


### GraphQL other features 
I just showcase the basic case of retrieving data, but you can do so much more with GraphQL such as :
 - Mutation
 - aliases
 - fragments
 - variables
 - and so on ....

You can check the official doc here : 
https://graphql.org/learn/queries/

And an awesome article about the GraphQL Semantic here :

[https://blog.graphql.guide/the-graphql-spec-simplified-93005ce0671f](https://blog.graphql.guide/the-graphql-spec-simplified-93005ce0671f)


### Security

A well put article about GraphQL security
[https://dimitr.im/graphql-spring-security](https://dimitr.im/graphql-spring-security)


### Generate code

You can generate code from schema which is always good to take :
[https://netflix.github.io/dgs/generating-code-from-schema/](https://netflix.github.io/dgs/generating-code-from-schema/)


## Inconvenience of GraphQL

-  Developers learning curve
-  Can complexify back end developer work on the server side
-  Hosting costs can be higher, depending on the chosen strategy
-  Caching is more complexe than with REST

Performance can be worst than with REST if you compare one on one Request. But the bet is that you will do less call to the API and in the end it is still worth it. But it depends on the use cases.