Class GenerateServerCodeTask
- All Implemented Interfaces:
com.graphql_java_generator.plugin.conf.CommonConfiguration,com.graphql_java_generator.plugin.conf.GenerateCodeCommonConfiguration,com.graphql_java_generator.plugin.conf.GenerateServerCodeConfiguration,Comparable<org.gradle.api.Task>,org.gradle.api.internal.DynamicObjectAware,org.gradle.api.internal.TaskInternal,org.gradle.api.Named,org.gradle.api.plugins.ExtensionAware,org.gradle.api.Task,org.gradle.util.Configurable<org.gradle.api.Task>
- Direct Known Subclasses:
GraphQLGenerateCodeTask
The generateServerCode Maven goal (and Gradle task) generates the java code for an almost ready to start GraphQL server. The developer has only to develop request to the data.
The java code is generated from one or more GraphQL schemas. It allows to work in Java with graphQL, in a schema first approach. These items are generated:
- the main method (in a jar project) or the main servlet (in a war project)
- All the GraphQL wiring, based on graphql-java-spring, itself being build on top of graphql-java
- All the POJOs, that contain the incoming request contents. The request response is written by the user code into these POJO, and the plugin take care of mapping them into the server response.
- An option allows to annotate the POJOs with the standard JPA annotations, to make it easy to link with a database. Please note that a
- All the interfaces for the
DataFetchersDelegate(named providers in the graphql.org presentation) that the server needs to implement
The specific code that needs to be implemented is the access to the Data: your database, other APIs or web services,
or any kind of storage you may have. This is done by implementing the interfaces for the DataFetchersDelegate
into a Spring component, that is:
- Create a class for each generated
DataFetchersDelegateinterface - Make it implement the relevant
DataFetchersDelegateinterface - Mark it with the
Componentannotation
And you're done! :)
You'll find more info in the tutorials: take a look at the Maven server tutorial or the Gradle server tutorial
Note: The attribute have no default values: their default values is read from the
GenerateCodeCommonExtension, whose attributes can be either the default value, or a value set in the build
script.
-
Nested Class Summary
Nested classes/interfaces inherited from interface org.gradle.api.Named
org.gradle.api.Named.Namer -
Field Summary
FieldsModifier and TypeFieldDescriptionIf this parameter is set to true, the spring GraphQL controller methods will be annotated with the@BatchMapping(instead of the@SchemaMapping).This parameter marks a list of GraphQL mappings as ignored, so that they are not generated by the plugin.Fields inherited from class com.graphql_java_generator.gradleplugin.GenerateCodeCommonTask
targetResourceFolder, targetSourceFolder, useJakartaEE9Fields inherited from class com.graphql_java_generator.gradleplugin.CommonTask
extension, skipGenerationIfSchemaHasNotChangedFields 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, DEFAULT_TARGET_SCHEMA_SUBFOLDERFields 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_EE9Fields 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_PACKAGESFields inherited from interface org.gradle.api.Task
TASK_ACTION, TASK_CONSTRUCTOR_ARGS, TASK_DEPENDS_ON, TASK_DESCRIPTION, TASK_GROUP, TASK_NAME, TASK_OVERWRITE, TASK_TYPE -
Constructor Summary
ConstructorsConstructorDescriptionGenerateServerCodeTask(GenerateServerCodeExtension extension, org.gradle.api.file.ProjectLayout projectLayout) GenerateServerCodeTask(org.gradle.api.file.ProjectLayout projectLayout) -
Method Summary
Modifier and TypeMethodDescriptionvoidexecute()com.graphql_java_generator.plugin.conf.BatchMappingDataFetcherReturnTypeprotected GenerateServerCodeExtensionfinal Stringcom.graphql_java_generator.plugin.conf.PluginModegetMode()com.graphql_java_generator.plugin.conf.Packagingfinal Stringfinal booleanbooleanfinal booleanbooleanbooleanfinal booleanvoidsetBatchMappingDataFetcherReturnType(com.graphql_java_generator.plugin.conf.BatchMappingDataFetcherReturnType batchMappingDataFetcherReturnType) final voidsetGenerateBatchLoaderEnvironment(boolean generateBatchLoaderEnvironment) voidsetGenerateBatchMappingDataFetchers(boolean generateBatchMappingDataFetchers) final voidsetGenerateDataFetcherForEveryFieldsWithArguments(boolean generateDataFetcherForEveryFieldsWithArguments) final voidsetGenerateDataLoaderForLists(boolean generateDataLoaderForLists) final voidsetGenerateJPAAnnotation(boolean generateJPAAnnotation) final voidsetIgnoredSpringMappings(String ignoredSpringMappings) final voidsetJavaTypeForIDType(String javaTypeForIDType) voidsetPackaging(com.graphql_java_generator.plugin.conf.Packaging packaging) final voidsetScanBasePackages(String scanBasePackages) Methods inherited from class com.graphql_java_generator.gradleplugin.GenerateCodeCommonTask
getCustomScalars, getPackageName, getQueryMutationExecutionProtocol, getSchemaPersonalizationFile, getSourceEncoding, getSpringAutoConfigurationPackage, getSpringBeanSuffix, getTargetClassFolder, getTargetResourceFolder, getTargetSourceFolder, isCopyRuntimeSources, isGenerateUtilityClasses, isSeparateUtilityClasses, isUseJakartaEE9, setCopyRuntimeSources, setCustomScalars, setPackageName, setQueryMutationExecutionProtocol, setSchemaPersonalizationFile, setSeparateUtilityClasses, setSourceEncoding, setSpringBeanSuffix, setTargetResourceFolder, setTargetSourceFolder, setUseJakartaEE9Methods inherited from class com.graphql_java_generator.gradleplugin.CommonTask
getDefaultTargetSchemaFileName, getEnumPrefix, getEnumSuffix, getFileValue, getInputPrefix, getInputSuffix, getInterfacePrefix, getInterfaceSuffix, getJsonGraphqlSchemaFilename, getMaxTokens, getProjectBuildDir, getProjectDir, getProjectMainSourceFolder, getSchemaFileFolder, getSchemaFilePattern, getTargetSchemaSubFolder, getTemplates, getTypePrefix, getTypeSuffix, getUnionPrefix, getUnionSuffix, getValue, isAddRelayConnections, isInitialized, isSkipGenerationIfSchemaHasNotChanged, logConfiguration, setAddRelayConnections, setEnumPrefix, setEnumSuffix, setExtension, setInitialized, setInputPrefix, setInputSuffix, setInterfacePrefix, setInterfaceSuffix, setJsonGraphqlSchemaFilename, setParserOptions, setSchemaFileFolder, setSchemaFilePattern, setSkipGenerationIfSchemaHasNotChanged, setTargetSchemaSubFolder, setTemplates, setTypePrefix, setTypeSuffix, setUnionPrefix, setUnionSuffixMethods inherited from class org.gradle.api.DefaultTask
compareTo, configure, dependsOn, doFirst, doFirst, doFirst, doLast, doLast, doLast, finalizedBy, getActions, getAnt, getDependsOn, getDescription, getDestroyables, getDidWork, getEnabled, getExtensions, getFinalizedBy, getGroup, getInputs, getLocalState, getLogger, getLogging, getMustRunAfter, getName, getOutputs, getPath, getProject, getShouldRunAfter, getState, getTaskDependencies, getTemporaryDir, getTimeout, hasProperty, mustRunAfter, onlyIf, onlyIf, onlyIf, property, setActions, setDependsOn, setDescription, setDidWork, setEnabled, setFinalizedBy, setGroup, setMustRunAfter, setOnlyIf, setOnlyIf, setOnlyIf, setProperty, setShouldRunAfter, shouldRunAfter, usesServiceMethods inherited from class org.gradle.api.internal.AbstractTask
acceptServiceReferences, appendParallelSafeAction, doNotTrackState, getAsDynamicObject, getIdentityPath, getImpliesSubProjects, getLifecycleDependencies, getOnlyIf, getReasonNotToTrackState, getReasonTaskIsIncompatibleWithConfigurationCache, getRequiredServices, getServices, getSharedResources, getStandardOutputCapture, getTaskActions, getTaskIdentity, getTemporaryDirFactory, hasTaskActions, injectIntoNewInstance, isCompatibleWithConfigurationCache, isEnabled, isHasCustomActions, notCompatibleWithConfigurationCache, prependParallelSafeAction, setImpliesSubProjectsMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface com.graphql_java_generator.plugin.conf.CommonConfiguration
getDefaultTargetSchemaFileName, getEnumPrefix, getEnumSuffix, getInputPrefix, getInputSuffix, getInterfacePrefix, getInterfaceSuffix, getJsonGraphqlSchemaFilename, getMaxTokens, getProjectBuildDir, getProjectDir, getProjectMainSourceFolder, getSchemaFileFolder, getSchemaFilePattern, getTargetSchemaSubFolder, getTemplates, getTypePrefix, getTypeSuffix, getUnionPrefix, getUnionSuffix, isAddRelayConnections, isSkipGenerationIfSchemaHasNotChanged, logCommonConfigurationMethods inherited from interface com.graphql_java_generator.plugin.conf.GenerateCodeCommonConfiguration
getCustomScalars, getPackageName, getQueryMutationExecutionProtocol, getSchemaPersonalizationFile, getSourceEncoding, getSpringAutoConfigurationPackage, getSpringBeanSuffix, getTargetClassFolder, getTargetResourceFolder, getTargetSourceFolder, isCopyRuntimeSources, isGenerateUtilityClasses, isSeparateUtilityClasses, isUseJakartaEE9, logGenerateCodeCommonConfigurationMethods inherited from interface com.graphql_java_generator.plugin.conf.GenerateServerCodeConfiguration
logConfiguration, logGenerateServerCodeConfigurationMethods inherited from interface org.gradle.api.Task
doNotTrackState, notCompatibleWithConfigurationCache
-
Field Details
-
generateBatchMappingDataFetchers
If 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")
When setting this parameter to true, the main changes are:- The
@BatchMappingannotation 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
batchMappingMethodReturnTypeplugin 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
DataFetchersDelegatemethod's signature changes - The
generateBatchLoaderEnvironment,generateDataFetcherForEveryFieldsWithArgumentsandgenerateDataLoaderForListsplugin parameters are ignored
A typical method signature for a data fetcher would be as below, where the return type is controller by the
batchMappingMethodReturnTypeplugin parameter :public Flux
topics(// BatchLoaderEnvironment batchLoaderEnvironment, // GraphQLContext graphQLContext, // List boards); Please note that the
@BatchMappingannotation 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@SchemaMappingannotation - The
-
ignoredSpringMappings
This 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).
-
-
Constructor Details
-
GenerateServerCodeTask
@Inject public GenerateServerCodeTask(org.gradle.api.file.ProjectLayout projectLayout) - Parameters:
projectLayout- This Gradle service is automatically injected by gradle. It allows to retrieve the project directory, as accessing the GradleProjectis forbidden from a task.
-
GenerateServerCodeTask
public GenerateServerCodeTask(GenerateServerCodeExtension extension, org.gradle.api.file.ProjectLayout projectLayout)
-
-
Method Details
-
execute
- Throws:
IOException
-
getBatchMappingDataFetcherReturnType
@Input public com.graphql_java_generator.plugin.conf.BatchMappingDataFetcherReturnType getBatchMappingDataFetcherReturnType()- Specified by:
getBatchMappingDataFetcherReturnTypein interfacecom.graphql_java_generator.plugin.conf.GenerateServerCodeConfiguration
-
setBatchMappingDataFetcherReturnType
public void setBatchMappingDataFetcherReturnType(com.graphql_java_generator.plugin.conf.BatchMappingDataFetcherReturnType batchMappingDataFetcherReturnType) -
getIgnoredSpringMappings
- Specified by:
getIgnoredSpringMappingsin interfacecom.graphql_java_generator.plugin.conf.GenerateServerCodeConfiguration
-
setIgnoredSpringMappings
-
getJavaTypeForIDType
- Specified by:
getJavaTypeForIDTypein interfacecom.graphql_java_generator.plugin.conf.GenerateServerCodeConfiguration
-
setJavaTypeForIDType
-
getMode
@Input public com.graphql_java_generator.plugin.conf.PluginMode getMode()- Specified by:
getModein interfacecom.graphql_java_generator.plugin.conf.GenerateCodeCommonConfiguration- Overrides:
getModein classGenerateCodeCommonTask
-
getPackaging
@Input @Optional public com.graphql_java_generator.plugin.conf.Packaging getPackaging()- Specified by:
getPackagingin interfacecom.graphql_java_generator.plugin.conf.GenerateServerCodeConfiguration
-
setPackaging
public void setPackaging(com.graphql_java_generator.plugin.conf.Packaging packaging) -
getQuotedScanBasePackages
- Specified by:
getQuotedScanBasePackagesin interfacecom.graphql_java_generator.plugin.conf.GenerateServerCodeConfiguration
-
getScanBasePackages
- Specified by:
getScanBasePackagesin interfacecom.graphql_java_generator.plugin.conf.GenerateServerCodeConfiguration
-
setScanBasePackages
-
isGenerateBatchLoaderEnvironment
@Input public final boolean isGenerateBatchLoaderEnvironment()- Specified by:
isGenerateBatchLoaderEnvironmentin interfacecom.graphql_java_generator.plugin.conf.GenerateServerCodeConfiguration
-
setGenerateBatchLoaderEnvironment
public final void setGenerateBatchLoaderEnvironment(boolean generateBatchLoaderEnvironment) -
isGenerateBatchMappingDataFetchers
@Input public boolean isGenerateBatchMappingDataFetchers()- Specified by:
isGenerateBatchMappingDataFetchersin interfacecom.graphql_java_generator.plugin.conf.GenerateServerCodeConfiguration
-
setGenerateBatchMappingDataFetchers
public void setGenerateBatchMappingDataFetchers(boolean generateBatchMappingDataFetchers) -
isGenerateDataFetcherForEveryFieldsWithArguments
@Input public final boolean isGenerateDataFetcherForEveryFieldsWithArguments()- Specified by:
isGenerateDataFetcherForEveryFieldsWithArgumentsin interfacecom.graphql_java_generator.plugin.conf.GenerateServerCodeConfiguration
-
setGenerateDataFetcherForEveryFieldsWithArguments
public final void setGenerateDataFetcherForEveryFieldsWithArguments(boolean generateDataFetcherForEveryFieldsWithArguments) -
isGenerateJacksonAnnotations
@Internal public boolean isGenerateJacksonAnnotations()- Specified by:
isGenerateJacksonAnnotationsin interfacecom.graphql_java_generator.plugin.conf.CommonConfiguration- Specified by:
isGenerateJacksonAnnotationsin interfacecom.graphql_java_generator.plugin.conf.GenerateServerCodeConfiguration- Overrides:
isGenerateJacksonAnnotationsin classGenerateCodeCommonTask
-
isGenerateJPAAnnotation
@Input public final boolean isGenerateJPAAnnotation()- Specified by:
isGenerateJPAAnnotationin interfacecom.graphql_java_generator.plugin.conf.GenerateServerCodeConfiguration
-
setGenerateJPAAnnotation
public final void setGenerateJPAAnnotation(boolean generateJPAAnnotation) -
isGenerateDataLoaderForLists
@Input public boolean isGenerateDataLoaderForLists()- Specified by:
isGenerateDataLoaderForListsin interfacecom.graphql_java_generator.plugin.conf.GenerateServerCodeConfiguration
-
setGenerateDataLoaderForLists
public final void setGenerateDataLoaderForLists(boolean generateDataLoaderForLists) -
getExtension
- Overrides:
getExtensionin classGenerateCodeCommonTask
-