DEV Community

oppy_sb
oppy_sb

Posted on

Introduction to GraphQL and implementation in Java

Introduction

GraphQl cartouche
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

GraphQl API calls schema

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

So i add the gradle dependencies :
Gradle dependencies

Schema :

You will need first to create your schema configuration file :

Ressource path

schema file

Configuration path

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

The class representing the data to retrieve :
Class to fetch

Calling APIs with Insomnia :

Insomnia call

**GUI :

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

Gui

Adding Actors

We change the schema configuration to add our Actors :
Actor configuration

In the DataFetcher

actor data

Actor Datafetcher

Insomnia call for actors

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

Security

A well put article about GraphQL 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/

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.

Latest comments (0)