Customizing code templates

Presentation

Code generated by the Graphql Maven Plugin is generated applying a set of Velocity templates to the input GraphQL Schmea. Templates are located in the project at graphql-maven-plugin-logic/src/main/resources/templates

If for any reason you may need to customize the template to modify the generated code this can be done by using the plugin parameter templates

templates param usage

Here there's an example of plugin configuration to use customized templates

<project ...>
...

<build>
        <plugins>
...
                <plugin>
                        <groupId>com.graphql-java-generator</groupId>
                        <artifactId>graphql-maven-plugin</artifactId>
                        <version>1.12.3</version>

                        <executions>
                                <execution>
                                        <goals>
                                                <goal>graphql</goal>
                                        </goals>
                                </execution>
                        </executions>
                        <configuration>
                                <mode>client</mode>
                                <templates>
                                        <QUERY_MUTATION>classpath/entry/to/customtemplate.java.vm</QUERY_MUTATION>
                                </templates>
                        </configuration>
                        <dependencies>

                                <!-- Dependency containing your templates-->
                                <dependency>
                                        <groupId>...</groupId>
                                        <artifactId>...</artifactId>
                                        <version>...</version>
                                </dependency>

                        </dependencies>
                </plugin>
        </plugins>
</build>
...
</project>

templates param is a map where the key is the ID of the template to customize and the value is a classpath entry to the resources containing the customized template

Customize templates shall be provided in a dependency configured in the plugin

Both client and server templates can be customized.

Template IDs

The available template IDs that can be configured for customization are:

OBJECT COMMON templates/object_type.vm.java
INTERFACE COMMON templates/interface_type.vm.java
ENUM COMMON templates/enum_type.vm.java
UNION COMMON templates/union_type.vm.java
CUSTOM_SCALAR_REGISTRY_INITIALIZER CLIENT templates/client_CustomScalarRegistryInitializer.vm.java
QUERY_MUTATION CLIENT templates/client_query_mutation_type.vm.java
QUERY_TARGET_TYPE CLIENT templates/client_query_target_type.vm.java
JACKSON_DESERIALIZER CLIENT templates/client_jackson_deserialize.vm.java
SUBSCRIPTION CLIENT templates/client_subscription_type.vm.java
DATA_FETCHER SERVER templates/server_GraphQLDataFetchers.vm.java
BATCH_LOADER_DELEGATE_IMPL SERVER templates/server_BatchLoaderDelegateImpl.vm.java
DATA_FETCHER_DELEGATE SERVER templates/server_GraphQLDataFetchersDelegate.vm.java
GRAPHQLUTIL SERVER templates/server_GraphQLUtil.vm.java
PROVIDER SERVER templates/server_GraphQLProvider.vm.java
SERVER SERVER templates/server_GraphQLServerMain.vm.java
WEB_SOCKET_CONFIG SERVER templates/server_WebSocketConfig.vm.java
WEB_SOCKET_HANDLER SERVER templates/server_WebSocketHandler.vm.java

Examples

Available on the project there is an example of this behavior: