-
Notifications
You must be signed in to change notification settings - Fork 50
usage_customtemplates
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. You can then provide your own templates. There are two possibilities for that:
- By putting them in a jar, that will be added as a dependency for the graphql-maven-plugin.
- (since v1.13) By putting them in your current project or module, and give a reference to it
You can combine both styles in one pom or gradle.build file.
To do this, you provide the path of the file, relative to the project or module root. You can check this sample:
<project ...>
...
<build>
<plugins>
...
<plugin>
<groupId>com.graphql-java-generator</groupId>
<artifactId>graphql-maven-plugin</artifactId>
<version>${lastReleasedVersion}</version>
<executions>
<execution>
<goals>
<goal>graphql</goal>
</goals>
</execution>
</executions>
<configuration>
<mode>client</mode>
<templates>
<!-- The SUBSCRIPTION_EXECUTOR is found in this project, int the src/graphql/customtemplates folder -->
<SUBSCRIPTION_EXECUTOR>src/graphql/customtemplates/custom_client_subscription_executor.vm.java</SUBSCRIPTION_EXECUTOR>
</templates>
</configuration>
</plugin>
</plugins>
</build>
...
</project>
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>${lastReleasedVersion}</version>
<executions>
<execution>
<goals>
<goal>graphql</goal>
</goals>
</execution>
</executions>
<configuration>
<mode>client</mode>
<templates>
<!-- The QUERY_MUTATION is found in the dependency, added just below WITHIN the plugin definition -->
<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>
The 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.
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](http://github.com/graphql-java-generator/graphql-maven-plugin-project/tree/master/graphql-maven-plugin-logic/src/main/resources/templates/server_GraphQLDataFetchersDelegate.vm.java | | GRAPHQLUTIL | SERVER | templates/server_GraphQLUtil.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 | | WIRING | SERVER | templates/server_GraphQLWiring.vm.java |
Available on the project there is an example of this behavior:
- graphql-maven-plugin-samples-customtemplates-resttemplate: contains an example of a template customization for QUERY_MUTATION_SUBSCRIPTION template. Code generated uses a Spring implementation of com.graphql_java_generator.client.QueryExecutor that uses RestTemplate as the client
- graphql-maven-plugin-samples-customtemplates-client: a Spring-based client generated with the previous customized template.
Creating a first app (non spring)
Connect to more than one GraphQL servers
Easily execute GraphQL requests with GraphQL Repositories
Access to an OAuth2 GraphQL server
How to personalize the client app
Howto personalize the generated code
Client migration from 1.x to 2.x
Implement an OAuth2 GraphQL server
Howto personalize the generated code
Server migration from 1.x to 2.x