Class AbstractGenerateServerCodeMojo
- All Implemented Interfaces:
CommonConfiguration
,GenerateCodeCommonConfiguration
,GenerateServerCodeConfiguration
,org.apache.maven.plugin.ContextEnabled
,org.apache.maven.plugin.Mojo
- Direct Known Subclasses:
AbstractGraphQLMojo
,GenerateServerCodeMojo
GenerateServerCodeMojo
and
the GraphQLMojo
mojos. It contains all parameters that are common to these goals. The parameters common to
all goal are inherited from the AbstractGenerateCodeCommonMojo
class.This avoids to redeclare each common parameter in each Mojo, including its comment. When a comment is updated, only one update is necessary, instead of updating it in each
- Author:
- etienne-sf
-
Field Summary
Modifier and TypeFieldDescriptionThis parameter is used only when generateBatchMappingDataFetchers is set to true.boolean
(only for server mode) Indicates if the plugin should generate add theBatchLoaderEnvironment
parameter to the batchLoader methods, in DataFetchersDelegate.boolean
If this parameter is set to true, the spring GraphQL controller methods will be annotated with the@BatchMapping
(instead of the@SchemaMapping
).boolean
(only for server mode, since 2.5) Defines if a data fetcher is needed for every GraphQL field that has input argument, and add them in the generated POJOs.This parameter marks a list of GraphQL mappings as ignored, so that they are not generated by the plugin.The javaTypeForIDType is the java class that is used in the generated code for GraphQL fields that are of the GraphQL ID type.Fields inherited from class com.graphql_java_generator.mavenplugin.AbstractCommonMojo
buildContext, ctx, enumPrefix, enumSuffix, inputPrefix, inputSuffix, interfacePrefix, interfaceSuffix, jsonGraphqlSchemaFilename, maxTokens, projectHelper, springConfigurationClass, typePrefix, typeSuffix, unionPrefix, unionSuffix
Fields inherited from interface com.graphql_java_generator.plugin.conf.CommonConfiguration
DEFAULT_ADD_RELAY_CONNECTIONS, DEFAULT_JSON_GRAPHQL_SCHEMA_FILE, DEFAULT_MAX_TOKENS, DEFAULT_PACKAGE_NAME, DEFAULT_PREFIX, DEFAULT_SCHEMA_FILE_FOLDER, DEFAULT_SCHEMA_FILE_PATTERN, DEFAULT_SCHEMA_SUB_FOLDER, DEFAULT_SKIP_GENERATION_IF_SCHEMA_HAS_NOT_CHANGED, DEFAULT_SUFFIX
Fields inherited from interface com.graphql_java_generator.plugin.conf.GenerateCodeCommonConfiguration
DEFAULT_COPY_RUNTIME_SOURCES, DEFAULT_QUERY_MUTATION_EXECUTION_PROTOCOL, DEFAULT_SCHEMA_PERSONALIZATION_FILE, DEFAULT_SEPARATE_UTIL_CLASSES, DEFAULT_SOURCE_ENCODING, DEFAULT_SPRING_BEAN_SUFFIX, DEFAULT_TARGET_RESOURCE_FOLDER, DEFAULT_TARGET_SOURCE_FOLDER, DEFAULT_USE_JAKARTA_EE9
Fields inherited from interface com.graphql_java_generator.plugin.conf.GenerateServerCodeConfiguration
DEFAULT_BATCH_MAPPING_DATA_FETCHER_RETURN_TYPE, DEFAULT_BATCH_MAPPING_DATA_FETCHER_RETURN_TYPE_STR, DEFAULT_GENERATE_BATCH_LOADER_ENVIRONMENT, DEFAULT_GENERATE_BATCH_MAPPING_DATA_FETCHERS, DEFAULT_GENERATE_DATA_FETCHER_FOR_EVERY_FIELD_WITH_ARGUMENT, DEFAULT_GENERATE_DATA_LOADER_FOR_LISTS, DEFAULT_GENERATE_JPA_ANNOTATION, DEFAULT_IGNORED_SPRING_MAPPINGS, DEFAULT_JAVA_TYPE_FOR_ID_TYPE, DEFAULT_SCAN_BASE_PACKAGES
Fields inherited from interface org.apache.maven.plugin.Mojo
ROLE
-
Constructor Summary
ModifierConstructorDescriptionprotected
AbstractGenerateServerCodeMojo
(Class<?> springConfigurationClass) -
Method Summary
Modifier and TypeMethodDescriptiongetMode()
The mode is forced toPluginMode.server
boolean
boolean
boolean
boolean
boolean
Methods inherited from class com.graphql_java_generator.mavenplugin.AbstractGenerateCodeCommonMojo
executePostExecutionTask, getCustomScalars, getPackageName, getQueryMutationExecutionProtocol, getSchemaPersonalizationFile, getSourceEncoding, getSpringBeanSuffix, getTargetClassFolder, getTargetFolder, getTargetResourceFolder, getTargetSourceFolder, isAddRelayConnections, isCopyRuntimeSources, isSeparateUtilityClasses, isUseJakartaEE9
Methods inherited from class com.graphql_java_generator.mavenplugin.AbstractCommonMojo
execute, getEnumPrefix, getEnumSuffix, getInputPrefix, getInputSuffix, getInterfacePrefix, getInterfaceSuffix, getJsonGraphqlSchemaFilename, getMaxTokens, getProjectDir, getSchemaFileFolder, getSchemaFilePattern, getTemplates, getTypePrefix, getTypeSuffix, getUnionPrefix, getUnionSuffix, isSkipGenerationIfSchemaHasNotChanged
Methods inherited from class org.apache.maven.plugin.AbstractMojo
getLog, getPluginContext, setLog, setPluginContext
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface com.graphql_java_generator.plugin.conf.CommonConfiguration
getDefaultTargetSchemaFileName, getEnumPrefix, getEnumSuffix, getInputPrefix, getInputSuffix, getInterfacePrefix, getInterfaceSuffix, getJsonGraphqlSchemaFilename, getMaxTokens, getProjectDir, getSchemaFileFolder, getSchemaFilePattern, getTargetSchemaSubFolder, getTemplates, getTypePrefix, getTypeSuffix, getUnionPrefix, getUnionSuffix, isAddRelayConnections, isSkipGenerationIfSchemaHasNotChanged, logCommonConfiguration
Methods inherited from interface com.graphql_java_generator.plugin.conf.GenerateCodeCommonConfiguration
getCustomScalars, getPackageName, getQueryMutationExecutionProtocol, getSchemaPersonalizationFile, getSourceEncoding, getSpringAutoConfigurationPackage, getSpringBeanSuffix, getTargetClassFolder, getTargetResourceFolder, getTargetSourceFolder, isCopyRuntimeSources, isGenerateUtilityClasses, isSeparateUtilityClasses, isUseJakartaEE9, logGenerateCodeCommonConfiguration
Methods inherited from interface com.graphql_java_generator.plugin.conf.GenerateServerCodeConfiguration
getQuotedScanBasePackages, isGenerateJacksonAnnotations, logConfiguration, logGenerateServerCodeConfiguration
-
Field Details
-
batchMappingDataFetcherReturnType
@Parameter(property="com.graphql_java_generator.mavenplugin.batchMappingDataFetcherReturnType", defaultValue="FLUX") public BatchMappingDataFetcherReturnType batchMappingDataFetcherReturnTypeThis parameter is used only when generateBatchMappingDataFetchers is set to true. It determines the return type of the data fetchers, as defined in the spring-graphql documentation.
The allowed values are (where K is the key type, that is: the parent object, and V is the value to be loaded in batch):
Value Return type MONO_MAP Mono<Map<K,V>>MAP Map<K,V> FLUX> Flux<V> COLLECTION Collection<V> The default value is
Flux<V>
For an easier use of this parameter, the comment of the generated data fetchers details the exact expected type.
-
generateBatchLoaderEnvironment
@Parameter(property="com.graphql_java_generator.mavenplugin.generateBatchLoaderEnvironment", defaultValue="true") public boolean generateBatchLoaderEnvironment(only for server mode) Indicates if the plugin should generate add the
BatchLoaderEnvironment
parameter to the batchLoader methods, in DataFetchersDelegate. This parameter allows to get the context of the Batch Loader, including the context associated to the id, when using the id has been added by theDataLoader.load(Object, Object)
method.For instance, if you have the method below, for a field named oneWithIdSubType in a DataFetcherDelegate:
@Override public CompletableFuture<AllFieldCasesWithIdSubtype> oneWithIdSubType( DataFetchingEnvironment dataFetchingEnvironment, DataLoader<UUID, AllFieldCasesWithIdSubtype> dataLoader, AllFieldCases source, Boolean uppercase) { return dataLoader.load(UUID.randomUUID()); }
then, in the AllFieldCasesWithIdSubtype DataFetcherDelegate, you can retrieve the uppercase this way:
@Override public List<AllFieldCasesWithIdSubtype> batchLoader(List<UUID> keys, BatchLoaderEnvironment environment) { List<AllFieldCasesWithIdSubtype> list = new ArrayList<>(keys.size()); for (UUID id : keys) { // Let's manage the uppercase parameter, that was associated with this key Boolean uppercase = (Boolean) environment.getKeyContexts().get(id); if (uppercase != null && uppercase) { item.setName(item.getName().toUpperCase()); } // Do something with the id and the uppercase value } return list; }
For more complex cases, you can store a
Map
with all the needed values, instead of just the parameter value.The default value changed since 2.0 version: it is false in 1.x version, and true since the 2.0 version
-
generateBatchMappingDataFetchers
@Parameter(property="com.graphql_java_generator.mavenplugin.generateBatchMappingDataFetchers", defaultValue="false") public boolean generateBatchMappingDataFetchersIf this parameter is set to true, the spring GraphQL controller methods will be annotated with the
@BatchMapping
(instead of the@SchemaMapping
). This allows to manage the of the N+1 select problem: so this allows much better performances, by highly diminishing the number of executed requests (avoid to execute several times the same "sub-query")- The
@BatchMapping
annotation may be applied to all data fetchers without argument(s) that return either a List, a Type, an Interface or an Union. - The return type must be defined in the controller: it may not be `Object`, as spring-graphql builds the
proper BatchLoader while loading the controllers, when the server starts. The return type for this method is
managed by the
batchMappingMethodReturnType
plugin parameter - DataLoader is managed transparently by spring (instead of having to declare it in the generated controller, and having it as a parameter in the generated data fetchers)
- The batch mapping is generalized on all data fetchers
- The
DataFetchersDelegate
method's signature changes - The
generateBatchLoaderEnvironment
,generateDataFetcherForEveryFieldsWithArguments
andgenerateDataLoaderForLists
plugin parameters are ignored
A typical method signature for a data fetcher would be as below, where the return type is controller by the
batchMappingMethodReturnType
plugin parameter :public Flux
topics(// BatchLoaderEnvironment batchLoaderEnvironment, // GraphQLContext graphQLContext, // List boards); Please note that the
@BatchMapping
annotation is a shortcut to avoid boilerplate code, for the most common cases. See this discussion for more information on this. For most complex cases, the use of a DataLoader is recommended by the spring-graphql case. And in these cases, the plugin will generate a method with the@SchemaMapping
annotation - The
-
generateDataFetcherForEveryFieldsWithArguments
@Parameter(property="com.graphql_java_generator.mavenplugin.generateDataFetcherForEveryFieldsWithArguments", defaultValue="false") public boolean generateDataFetcherForEveryFieldsWithArguments(only for server mode, since 2.5) Defines if a data fetcher is needed for every GraphQL field that has input argument, and add them in the generated POJOs. This allows a better compatibility with spring-graphql, and an easy access to the field's parameters.
With this argument to false, the data fetchers are generated only for field which type is a type (not a scalar or an enum), and for the query, mutation and subscription types.
With this argument to true, the data fetchers are generated for all GraphQL fields which type is a type (not a scalar or an enum) or that has one or arguments
This parameter is available since version 2.5. Its default value is false in 2.x versions for backward compatibility with existing implementations based on the plugin. But the recommended value is true.
-
ignoredSpringMappings
@Parameter(property="com.graphql_java_generator.mavenplugin.ignoredSpringMappings", defaultValue="") public String ignoredSpringMappingsThis parameter marks a list of GraphQL mappings as ignored, so that they are not generated by the plugin. These ignored mappings can then be defined by the specific implementation.
The other way to it is to create a spring GraphQL Controller, that overrides the controller generated by the plugin. But this may lead to this error:
Ambiguous mapping. Cannot map 'xxxController' method [...] to 'Type.field': there is already 'yyy' bean method [...] mapped.
The parameter may contain:
- The '*' character: this would mark all controllers and DataFetchersDeleagate to be ignored. That is: none would be generated, and it's up to the specific implementation to manage them. In this mode, you still benefit of the POJO generation, the type wiring, the custom scalars mapping...
- A list of:
- GraphQL type name: The full controller class for this type is ignored, and won't be generated
- GraphQL type's field name: The method in the controller of this type, for this field, is ignored, and won't
be generated. The field must be written like this:
{type name}.{field name}
The accepted separators for the values are: comma, space, carriage return, end of line, space, tabulation. At least one separator must exist between two values in the list. Here is a sample:
Type1, Type2.field1 Type3 Type4.field2 For field mapping, there must be no separator other than '.' between the type name and the field name. For instance, the following type declaration are invalid: 'type .field', 'type. field'
To implement the ignored mappings, you'll have to follow the [spring-graphql documentation](https://docs.spring.io/spring-graphql/reference/controllers.html).
-
javaTypeForIDType
@Parameter(property="com.graphql_java_generator.mavenplugin.javaTypeForIDType", defaultValue="java.util.UUID") public String javaTypeForIDTypeThe javaTypeForIDType is the java class that is used in the generated code for GraphQL fields that are of the GraphQL ID type. The default value is java.util.UUID. Valid values are: java.lang.String, java.lang.Long and java.util.UUID.
This parameter is only valid for the server mode. When generating the client code, the ID is always generated as a String type, as recommended in the GraphQL doc.
In other words: when in server mode and javaTypeForIDType is not set, all GraphQL ID fields are UUID attributes in java. When in server mode and javaTypeForIDType is set to the X type, all GraphQL ID fields are X attributes in java.
Note: you can override this, by using the schema personalization capability. For more information, please have a look at the Schema Personalization doc page.
-
-
Constructor Details
-
AbstractGenerateServerCodeMojo
-
-
Method Details
-
getBatchMappingDataFetcherReturnType
- Specified by:
getBatchMappingDataFetcherReturnType
in interfaceGenerateServerCodeConfiguration
-
getIgnoredSpringMappings
- Specified by:
getIgnoredSpringMappings
in interfaceGenerateServerCodeConfiguration
-
getJavaTypeForIDType
- Specified by:
getJavaTypeForIDType
in interfaceGenerateServerCodeConfiguration
-
getMode
The mode is forced toPluginMode.server
- Specified by:
getMode
in interfaceGenerateCodeCommonConfiguration
-
getPackaging
- Specified by:
getPackaging
in interfaceGenerateServerCodeConfiguration
-
isGenerateBatchLoaderEnvironment
public boolean isGenerateBatchLoaderEnvironment()- Specified by:
isGenerateBatchLoaderEnvironment
in interfaceGenerateServerCodeConfiguration
-
isGenerateBatchMappingDataFetchers
public boolean isGenerateBatchMappingDataFetchers()- Specified by:
isGenerateBatchMappingDataFetchers
in interfaceGenerateServerCodeConfiguration
-
isGenerateDataFetcherForEveryFieldsWithArguments
public boolean isGenerateDataFetcherForEveryFieldsWithArguments()- Specified by:
isGenerateDataFetcherForEveryFieldsWithArguments
in interfaceGenerateServerCodeConfiguration
-
isGenerateJPAAnnotation
public boolean isGenerateJPAAnnotation()- Specified by:
isGenerateJPAAnnotation
in interfaceGenerateServerCodeConfiguration
-
isGenerateDataLoaderForLists
public boolean isGenerateDataLoaderForLists()- Specified by:
isGenerateDataLoaderForLists
in interfaceGenerateServerCodeConfiguration
-
getScanBasePackages
- Specified by:
getScanBasePackages
in interfaceGenerateServerCodeConfiguration
-