Skip to content
Etienne edited this page Mar 16, 2021 · 64 revisions

GraphQL Maven Plugin (server mode usage)

This project is a maven plugin, which makes it easy to work in Java with graphQL in a schema first approach.

In server mode, the graphql-maven-plugin reads a graphqls schema, and generated the maximum of boilerplate code. That is, it generates:

  • When in a jar maven project, the main class to start a Spring Boot application
  • When in a war maven project, the servlet configuration to be embedded in a war package. It can then be deployed in any standard application server
  • Almost all the Spring components to wire the whole stuff
  • The interfaces (DataFetchersDelegate) for the classes that are specific to the application context (see below)
  • The POJOs to manipulate the GraphQL objects defined in the GraphQL schema.
    • These POJOs are annotated with JPA annotations. This allows to link them to almost any database
    • You can customize these annotations, with the Schema Personalization file (see below for details)
    • (in a near future) It will be possible to define your own code template, to generate exactly the code you want

Please note that the generated code uses dataloader to greatly improve the server's performances. See https://github.com/graphql-java/java-dataloader.

Once all this is generated, you'll have to implement the DataFetchersDelegate interfaces. The DataFetchersDelegate implementation is the only work that remains on your side. They are the link between the GraphQL schema and your data storage. See below for more details.

A war or a Spring Boot application

Depending on your use case, you can set the maven packaging to jar or war, in your pom. This changes the generated code. But your specific code is exactly the same. That is: you can change the packaging at any time, and it will still produce a ready-to-go product without any other modification from you.

Below you'll find:

  • A sample pom to start with
  • The explanation about the DataFetchersDelegate interfaces you'll have to implement.

The exposed URL

The parameters for the server are stored in the application.properties file.

You can have a look at this file, in the given server samples. It's a standard spring boot configuration file, so you'll find all the needed information on the net.

If you want to expose your Spring Boot app in https, take a look at the doc on the net. All parameters will go in the application.properties. For instance, Thomas Vitale provides a doc for that.

The important parameter is: server.port (for instance server.port = 8180), which determines the app port, when running as a spring boot app, that is, when it's packaged as a jar.

The path depends on the way the GraphQL is run:

  • If packaged as a jar: the path is /graphql

  • If packaged as a war: the paths is /{WebAppContext}/graphql

Clone this wiki locally