back to all blogsSee all blog posts

Support for GraphQL with Open Liberty 20.0.0.6

image of author
Yasmin Aumeeruddy on Jun 5, 2020
Post available in languages:

With Open Liberty 20.0.0.6 you can now develop "code-first" GraphQL applications. In addition, you can now provision features from Maven repository and control the start up of applications in the server configuration.

In Open Liberty 20.0.0.6:

View the list of fixed bugs in 20.0.0.6.

Run your apps using 20.0.0.6

If you’re using Maven, here are the coordinates:

<dependency>
    <groupId>io.openliberty</groupId>
    <artifactId>openliberty-runtime</artifactId>
    <version>20.0.0.6</version>
    <type>zip</type>
</dependency>

Or for Gradle:

dependencies {
    libertyRuntime group: 'io.openliberty', name: 'openliberty-runtime', version: '[20.0.0.6,)'
}

Or if you’re using Docker:

FROM open-liberty

Or take a look at our Downloads page.

Ask a question on Stack Overflow

Use GraphQL with Open Liberty

Open Liberty officially supports MicroProfile GraphQL. This allows you to write "code-first" GraphQL applications putting clients in control of the data they receive. Use annotations like @Query and @Mutation to turn POJOs into HTTP-based GraphQL endpoints. Those query/mutation methods can then return existing entity objects and the client can specify which fields it is interested in - reducing network bandwidth and client-side processing.

Here’s an example:

@GraphQLApi
public class MovieService {
    AtomicInteger nextId = new AtomicInteger();
    Map<Integer, Movie> movieDB = new HashMap<>();

    @Query("movieById")
    public Movie getMovieByID(int id) throws UnknownMovieException {
        return Optional.ofNullable(movieDB.get(id)).orElseThrow(UnknownMovieException::new);
    }

    @Query("allMoviesDirectedBy")
    public List<Movie> getAllMoviesWithDirector(String directorName) {
        return movieDB.values().stream()
                               .filter(m -> m.getDirector().equals(directorName))
                               .collect(Collectors.toList());
    }

    @Mutation("newMovie")
    public int createNewMovie(@Name("movie") Movie movie) {
        int id = nextId.incrementAndGet();
        movie.setId(id);
        movieDB.put(id, movie);
        return id;
    }
}

This will create a GraphQL application with two queries ("movieById" and "allMoviesDirectedBy") and a mutation ("newMovie"). This will allow a client to execute a query like:

query {
  allMoviesDirectedBy(directorName: "Roland Emmerich") {
    id, title, actors
  }
}

and see a result like:

{
  "data": {
    "allMoviesDirectedBy": [
      {
        "id": 1,
        "title": "Independence Day",
        "actors": [
          "Will Smith",
          "Bill Pullman",
          "Jeff Goldblum",
          ...
        ]
      },
      ...
    ]
  }
}

Liberty’s GraphQL APIs were developed within the MicroProfile community and have broad industry support. The implementation is based on SmallRye GraphQL. Liberty’s GraphQL feature goes beyond the MicroProfile specification and adds support for metrics collection, authorization checks, and request/response logging of query and mutation methods.

Find out more in Andy’s blog post on how to use MicroProfile GraphQL in Open Liberty. Join the growing landscape of GraphQL adopters and write your first GraphQL application today!

Support provisioning features from Maven repository

Users are now able to install desired features onto their Open Liberty runtimes from Maven Central or an on-premises Maven repository, such as one served on Artifactory or Nexus, using a convenient command line tool.

Use the wlp/bin/featureUtility command to find, get information about, and install assets that are in a Maven repository.

Command Description

featureUtility help installFeature

Display help information for the installFeature action

featureUtility installFeature mpHealth-2.2 or featureUtility installFeature io.openliberty.features:mpHealth-2.2

Install the MicroProfile Health 2.2 feature from Maven Central

featureUtility installServerFeatures myserver

install the features listed in the server.xml of the myserver server

featureUtility installFeature mpHealth-2.2 --noCache

Install the MicroProfile Health 2.2 feature without caching the feature to the local Maven repository

featureUtility installServerFeatures myserver --noCache

install features for the myserver server without caching the features to the local Maven repository

featureUtility installServerFeatures defaultServer --verbose

Install features for the myserver server with debug enabled

featureUtility viewSettings

View a template of your featureUtility.properties file

featureUtility find mpHealth-2.2

Search for the MicroProfile Health 2.2 feature from Maven Central and all configured Maven repositories

featureUtility find

Search for all available features from Maven Central and all configured Maven repositories

Control application start order in Open Liberty

By default, applications start in parallel and can finish starting in random order. This update provides the ability to prevent any application from starting until one or more other applications have started.

Separate applications can often have implicit dependencies on each other. For example, a single Open Liberty server might contain a front end application that provides a user interface and a back end application that accesses a database. If the front end application is available before the back end application has started, users may run into errors. This feature would allow administrators to prevent the front end application from starting until the back end is ready so that users would no longer see those errors.

Application dependencies can be defined in the configuration using the startAfter attribute on the application element. The startAfter attribute should contain a comma separated list of ID values for applications that should start before that application can begin starting. For example:

<webApplication id="frontend" location="myFrontend.war" startAfter="backend1, backend2"/>
<enterpriseApplication id="backend1" location="myBackend.ear"/>
<enterpriseApplication id="backend2" location="myUtilities.ear"/>

REST visualizations and hover-over descriptions in Open Liberty Grafana Dashboards

The Grafana dashboard provides a wide range of time-series visualizations of MicroProfile Metrics data such as CPU, REST, Servlet, Connection Pool, and Garbage Collection metrics. It is powered by a Prometheus datasource which is configured to ingest data from one or more Open Liberty servers' /metrics endpoint, enabling users to view on Grafana dashboards in near real-time.

With the release of mpMetrics-2.3 and its addition of JAX-RS metrics, we’ve introduced a new set of visualizations to our Open Liberty Grafana dashboards under a new tab labelled "REST". In addition, hover-over descriptions have been added to help provide a short summary about each visualization and their function. These updates apply to our Grafana dashboard for Open Liberty deployed to OKD or Red Hat OpenShift Container Platform, as well as our dashboard for standalone Open Liberty instances.

If you do not already have Grafana and Prometheus set up, there is a Kabanero guide for Red Hat OpenShift Container Platform 4.3 as well as a blog post for standalone Open Liberty to help get started.

Grafana dashboards for Liberty on OKD or Red Hat OpenShift Container Platform can be found in our open-liberty-operator repository. In addition, the dashboard for standalone Open Liberty can be found on the Grafana Labs website.

Web application startup changes in Open Liberty

Open Liberty has been updated to consider Web applications started after calls to the ServletContainerInitializers and ServletContextListeners have completed. This has the effect of moving more of the application initialization into the server startup route and may make applications and the server appear to take longer to start. It doesn’t affect how long it takes for applications to start processing requests, it just moves it to run prior to the ports opening. In addition, you can now configure the server.xml so a failure in a ServletContextListener will cause application startup to fail. To do so, add the following:

<webContainer stopAppStartUponListenerException="true"/>

Find out more about application properties here.

Get Open Liberty 20.0.0.6 now