A preview of MicroProfile GraphQL

This blog post is related to my GitHub project mpql-preview.

Objectives

This project aims at providing a preview of the future Eclipse MicroProfile GraphQL specification.

If you don’t know what MicroProfile GraphQL is about, please have a look on my previous post and on  GitHub. In short, it aims at making GraphQL a first-class citizen of the MicroProfile platform to develop modern APIs.

GraphQL is a specification, initiated by FaceBook, and hosted by the Linux Foundation since Nov. 2018. It provides a lot of flexibility on the client-side and is particularly relevant for data-oriented APIs.

To know more about GraphQL in general, please follow this excellent tutorial from Bojan Tomic: graphql-java Tutorial.

 

Status of Eclipse MicroProfile GraphQL

MicroProfile GraphQL is an Eclipse project, started in December 2018, to define a standard API for GraphQL applications following a code-first approach. “Code-first” means that the GraphQL schema is not defined upfront, but is derived from the Java code at application startup.

The current API is made of a set of a set of annotations.

The project is still under development, but you can already give it a try:

  1. the project is available on GitHub,
  2. the API has been published on Maven Central,
  3. a first prototype implementation is provided by spqr. It is available on GitHub in the microprofile-proto branch.

So everything is in place to play around with it, except that complete MicroProfile GraphQL implementations are not yet available. And this is precisely what this preview project is about!

 

What is this preview project about?

This project is made of 3 parts:

  1. a SuperHero demo application,
  2. a runner based on a CDI extension which makes the glue between the SuperHero application and the spqr implementation.This glue is required since real MicroProfile GraphQL implementations are still under development. IBM, RedHat and Tomitribe are activelly working on it. A Servlet is also defined in the runner to ensure the HTTP transport,
  3. a basic GraphQL client to run the tests.

It has been developped as a Java EE Web application using Payara-5.192. It can run on any Java EE 8 platform.

 

Building and running the project

Preambles

First off, you need to have a JDK (>=8) and Maven (3.5) installed.

More precisely, this project has been developped with:

  1. Maven 3.6.1,
  2. OpenJDK 11.0.3 with OpenJ9.

Building the project

The following steps must be achieved:

  1. clone this project,
  2. clone spqr microprofile-proto branch from GitHub in the microprofile-proto branch,
  3. build and install it locally (not available on Maven central),
  4. download Payara 5.192,
  5. start Payara: asadmin start-domain domain1 (required for the tests),
  6. build the project with Maven,
  7. once the war file is generated, you are free to use your favorite IDE to change the source code or the application server.

If you change the code, beware that at least one @GraphQLApi class is expected to start the application.

Running

The war file containing the SuperHero application is fully Java/Jakarta EE 8 compliant and can be deployed and run on any application server. It has been tested on:

  1. GlassFish 5.1,
  2. OpenLiberty 19.0.5,
  3. TomEE plus 8.0.0-M2,
  4. Wildfly 16.0.0.Final.

Playing around with GraphQL

Once the application started, go to the index page which offers two options:

  1. introspect the GraphQL schema: this is a key concept of GraphQL, acting as a contract between the server and the clients. BTW, note that GraphQL provides native introspection,
  2. play with GraphiQL to interract with the SuperHero application. GraphiQL is an interactive javascript-based tool enabling to send queries and mutations from your browser.

If your are not familiar with GraphQL syntax, you can copy-paste those used by the tests defined in graphql-config.properties.

To retrieve all heroes:

query allHeroes {
  allHeroes {
    name
    primaryLocation
    superPowers
    realName
  }
}
To retrieve heroes belonging to the Avengers team:
query allAvengers {
  allHeroesInTeam(team: "Avengers") {
    name
    primaryLocation
    superPowers
  }
}
To create a new hero in the Avengers team with given super powers:
mutation createNewHero {
  createNewHero(
    hero: {
           name: "Captain America", 
           realName: "Steven Rogers", 
           superPowers: ["Super strength", "Vibranium Shield"], 
           primaryLocation: "New York, NY", 
           teamAffiliations: [{name: "Avengers"}]
           }
         ) 
  {
  name
  primaryLocation
  superPowers
  realName
  }
}

Do not hesitate to visit the GitHub mpql-preview project. Have fun and stay tuned with MicroProfile GraphQL!

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s