Skip to content

usage_customtemplates

etienne-sf edited this page Aug 28, 2024 · 49 revisions

Customizing code templates

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.
    • This can be nice, if you plan to reuse these templates in several projects.
  • (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.

templates param usage

Provide the custom template as a file within the current project

To do this, you provide the path of the file, relative to the project or module root. You can check this sample:

As these files are resources, you can to put your custom templates in the resources folder. As they are useless at runtime, my opinion is that it's better to put them in a specific folder, which can be: src/graphql.

So, this leads to:

  • Your template is in the src/graphql/customtemplates/object_type.vm.java (where src is a subfolder of the folder that contains your pom.xml file)
  • Your pom configuration can look like this:
...
        <templates>
            <OBJECT>src/graphql/customtemplates/object_type.vm.java</OBJECT>
        </templates>
...

<project ...> ...

... com.graphql-java-generator graphql-maven-plugin 2.8
		<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>
... ```

Provide the custom template within a provided jar

notice: this doesn't work when using the Gradle plugin (it's ok with the Maven plugin). Apparently, the Gradle plugin can't see a dependency that is not defined when building the plugin, even if this dependency is added in the buildscript.dependency block. if anyone find a solution, please post a message in one of the project github forum

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>2.8</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.

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_MUTATION_EXECUTOR | CLIENT | templates/client_query_mutation_executor.vm.java | | QUERY_MUTATION_REACTIVE_EXECUTOR | CLIENT | templates/client_query_mutation_reactive_executor.vm.java | | QUERY_TARGET_TYPE | CLIENT | templates/client_query_target_type.vm.java | | JACKSON_DESERIALIZER | CLIENT | templates/client_jackson_deserialize.vm.java | | JACKSON_SERIALIZER | CLIENT | templates/client_jackson_serialize.vm.java | | SPRING_AUTOCONFIGURATION_DEFINITION_FILE | CLIENT | templates/client_spring_autoconfiguration_definition.vm.properties | | (1.x versions) SPRING_CONFIGURATION_CLASS
(versions >= 2.1) CLIENT_SPRING_CONFIGURATION_CLASS | CLIENT | templates/client_spring_configuration.vm.java | | SUBSCRIPTION | CLIENT | templates/client_subscription_type.vm.java | | SUBSCRIPTION_EXECUTOR | CLIENT | templates/client_subscription_executor.vm.java | | SUBSCRIPTION_REACTIVE_EXECUTOR | CLIENT | templates/client_subscription_reactive_executor.vm.java | | TYPE_MAPPING | CLIENT | templates/client_type_mapping.vm.java | | TYPE_MAPPING_CSV | CLIENT | templates/client_type_mapping.vm.csv | | DATA_FETCHER | SERVER | templates/server_GraphQLDataFetchers.vm.java | | (versions >= 2.5) DATA_FETCHERS_DELEGATES_REGISTRY | SERVER | templates/server_GraphQLDataFetchersDelegateRegistry.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 | | (versions >= 2.1) SERVER_SPRING_CONFIGURATION_CLASS | SERVER | templates/server_spring_configuration.vm.java || WEB_SOCKET_CONFIG | SERVER | templates/server_WebSocketConfig.vm.java | | WEB_SOCKET_HANDLER
Removed since release 1.18 | SERVER | templates/server_WebSocketHandler.vm.java | | WIRING | COMMON | templates/GraphQLWiring.vm.java |

Note:

  • Since 2.7, the server_GraphQLWiring.vm.java has been renamed to GraphQLWiring.vm.java, as it is used on both the client and the server side, starting from this version.
    • Until 2.6, it was only on server side

Examples

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

Clone this wiki locally