Generate GraphQL contract server using GraphQL Maven plugin

With RESTful API, you can use the OpenAPI Generator Maven plugin to generate source code and just add implementation for APIs. With GraphQL, you can also do this using the GraphQL Maven plugin. How is it in detail? Let’s find out together in this tutorial!

First, I will create a new Spring Boot project with Spring Web and Spring for GraphQL dependencies as an example:

The result is as follows:

For the example of this tutorial, I will define a file schema.graphqls located in the src/main/resources/graphql directory with the following content:

Now you can define the GraphQL Maven plugin as follows:

The GraphQL Maven plugin defines a number of goals, to generate a GraphQL contract for the server, please declare using the goal “generateServerCode” in the execution section of the GraphQL Maven plugin.

In the configuration section, by default, the GraphQL Maven plugin will scan all .graphqls files in the src/main/resources directory to generate server code, but because I wanted to centralize all the .graphqls files in the src/main/ directory resources/graphql so you can use the <schemaFileFolder> tag to modify this default value.

<packageName> will be the package name for the GraphQL Maven plugin to generate source code.

By default, the code will be generated in the /target/generated-sources/graphql-maven-plugin directory, if you want to change this default value, you can configure it like I used the <targetSourceFolder> tag!

You can see all configurations of the GraphQL Maven plugin with the goal “generateServerCode” here.

At this point, you will see the GraphQL Maven plugin generate code with the following results:

In order for the code to compile, you need to add some dependencies as follows:

In the com.huongdanjava.graphql package, you can see that the Graphql Maven plugin generates 2 classes Query and Student corresponding to the 2 GraphQL object types that we defined in the .graphqls file.

The QueryController class will handle the Query type for GraphQL that we have defined. This class uses the implementation of the DataFetchersDelegateQuery interface to get the returned data for the query, so you need to implement this DataFetchersDelegateQuery interface. My example is as follows:

Here, I only implement for example, you can implement to get information from the database if you want!

Another interface used in the StudentController class related to the batch loader that you also need to implement is DataFetchersDelegateStudent. Depending on your needs, you can implement it reasonably. For simplicity, I just add a class that implements this interface as follows:

Now, please declare the property spring.graphql.graphiql.enabled=true to enable using the GraphiQL tool, in the application.properties file:

At this point, we have finished using the GraphQL Maven plugin to generate GraphQL contracts.

To get all student’s information, I will use the following query:

Result:

As for getting information of “A” class, I will query:

Result:

Add Comment