Skip to content

Commit

Permalink
New config to generate only no-args constructor #1017
Browse files Browse the repository at this point in the history
  • Loading branch information
kobylynskyi committed Feb 26, 2023
1 parent bea0364 commit 2507dd5
Show file tree
Hide file tree
Showing 31 changed files with 459 additions and 41 deletions.
9 changes: 5 additions & 4 deletions docs/codegen-options.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,12 @@
| `fieldsWithoutResolvers` | Set(String) | Empty | Fields that DO NOT require Resolvers should be defined here in format: `TypeName.fieldName` or `TypeName` or `@directive`. Can be used in conjunction with `generateExtensionFieldsResolvers` option. E.g.: `Person`, `Person.friends`, `@noResolver`. |
| `resolverArgumentAnnotations` | Set(String) | Empty | Annotations that will be added to all resolver arguments. Can be used for [spring-graphql](https://github.com/spring-projects/spring-graphql) inegration by supplying: `org.springframework.graphql.data.method.annotation.Argument` |
| `parametrizedResolverAnnotations` | Set(String) | Empty | Annotations that will be added to all parametrized resolver methods. Can be used for [spring-graphql](https://github.com/spring-projects/spring-graphql) inegration by supplying: `org.springframework.graphql.data.method.annotation.SchemaMapping(typeName="{{TYPE_NAME}}")` |
| `generateParameterizedFieldsR`<br>`esolvers` | Boolean | True | If true, then generate separate `Resolver` interface for parametrized fields. If false, then add field to the type definition and ignore field parameters. |
| `generateExtensionFieldsResol`<br>`vers` | Boolean | False | Specifies whether all fields in extensions (`extend type` and `extend interface`) should be present in Resolver interface instead of the type class itself. |
| `generateParameterizedFieldsResolvers` | Boolean | True | If true, then generate separate `Resolver` interface for parametrized fields. If false, then add field to the type definition and ignore field parameters. |
| `generateExtensionFieldsResolvers` | Boolean | False | Specifies whether all fields in extensions (`extend type` and `extend interface`) should be present in Resolver interface instead of the type class itself. |
| `generateModelsForRootTypes` | Boolean | False | Specifies whether model classes should be generated for `type Query`, `type Subscription`, `type Mutation`. |
| `useOptionalForNullableReturn`<br>`Types` | Boolean | False | Specifies whether nullable return types of api methods should be wrapped into [`java.util.Optional<>`](https://docs.oracle.com/javase/8/docs/api/index.html?java/util/Optional.html). Lists will not be wrapped. |
| `generateApisWithThrowsExcept`<br>`ion` | Boolean | True | Specifies whether api interface methods should have `throws Exception` in signature. |
| `useOptionalForNullableReturnTypes` | Boolean | False | Specifies whether nullable return types of api methods should be wrapped into [`java.util.Optional<>`](https://docs.oracle.com/javase/8/docs/api/index.html?java/util/Optional.html). Lists will not be wrapped. |
| `generateApisWithThrowsException` | Boolean | True | Specifies whether api interface methods should have `throws Exception` in signature. |
| `generateNoArgsConstructorOnly` | Boolean | False | Specifies whether model classes should only have a no-args constructor. All-args constructor will not be generated in case value is <b>true</b> |
| `apiReturnType` | String | Empty | Return type for api methods (query/mutation). For example: `reactor.core.publisher.Mono`, etc. |
| `apiReturnListType` | String | Empty | Return type for api methods (query/mutation) having list type. For example: `reactor.core.publisher.Flux`, etc. By default is empty, so `apiReturnType` will be used. |
| `subscriptionReturnType` | String | Empty | Return type for subscription methods. For example: `org.reactivestreams.Publisher`, `io.reactivex.Observable`, etc. |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ public class GraphQLCodegenGradleTask extends DefaultTask implements GraphQLCode
private Boolean generateApisWithThrowsException = MappingConfigConstants.DEFAULT_GENERATE_APIS_WITH_THROWS_EXCEPTION;
private Boolean generateJacksonTypeIdResolver = MappingConfigConstants.DEFAULT_GENERATE_JACKSON_TYPE_ID_RESOLVER;
private Boolean addGeneratedAnnotation = MappingConfigConstants.DEFAULT_ADD_GENERATED_ANNOTATION;
private Boolean generateNoArgsConstructorOnly = MappingConfigConstants.DEFAULT_GENERATE_NOARGS_CONSTRUCTOR_ONLY;
private String generatedAnnotation;
private Set<String> fieldsWithResolvers = new HashSet<>();
private Set<String> fieldsWithoutResolvers = new HashSet<>();
Expand Down Expand Up @@ -155,6 +156,7 @@ public void generate() throws Exception {
mappingConfig.setUseOptionalForNullableReturnTypes(useOptionalForNullableReturnTypes);
mappingConfig.setGenerateApisWithThrowsException(generateApisWithThrowsException);
mappingConfig.setGenerateJacksonTypeIdResolver(generateJacksonTypeIdResolver);
mappingConfig.setGenerateNoArgsConstructorOnly(generateNoArgsConstructorOnly);
mappingConfig.setAddGeneratedAnnotation(addGeneratedAnnotation);
mappingConfig.setGeneratedAnnotation(generatedAnnotation);
mappingConfig.setApiReturnType(apiReturnType);
Expand Down Expand Up @@ -682,6 +684,17 @@ public void setGeneratedAnnotation(String generatedAnnotation) {
this.generatedAnnotation = generatedAnnotation;
}

@Input
@Optional
@Override
public Boolean isGenerateNoArgsConstructorOnly() {
return generateNoArgsConstructorOnly;
}

public void setGenerateNoArgsConstructorOnly(Boolean generateNoArgsConstructorOnly) {
this.generateNoArgsConstructorOnly = generateNoArgsConstructorOnly;
}

@Input
@Optional
@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,9 @@ public class GraphQLCodegenMojo extends AbstractMojo implements GraphQLCodegenCo
@Parameter(defaultValue = MappingConfigConstants.DEFAULT_ADD_GENERATED_ANNOTATION_STRING)
private boolean addGeneratedAnnotation;

@Parameter(defaultValue = MappingConfigConstants.DEFAULT_GENERATE_NOARGS_CONSTRUCTOR_ONLY_STRING)
private boolean generateNoArgsConstructorOnly;

@Parameter
private String generatedAnnotation;

Expand Down Expand Up @@ -271,6 +274,7 @@ public void execute() throws MojoExecutionException {
mappingConfig.setGenerateJacksonTypeIdResolver(generateJacksonTypeIdResolver);
mappingConfig.setAddGeneratedAnnotation(addGeneratedAnnotation);
mappingConfig.setGeneratedAnnotation(generatedAnnotation);
mappingConfig.setGenerateNoArgsConstructorOnly(generateNoArgsConstructorOnly);
mappingConfig.setFieldsWithResolvers(mapToHashSet(fieldsWithResolvers));
mappingConfig.setFieldsWithoutResolvers(mapToHashSet(fieldsWithoutResolvers));
mappingConfig.setRelayConfig(relayConfig);
Expand Down Expand Up @@ -665,30 +669,27 @@ public Boolean isGenerateSealedInterfaces() {
return generateSealedInterfaces;
}

public ParentInterfacesConfig getParentInterfaces() {
return parentInterfaces;
}

public String[] getConfigurationFiles() {
return configurationFiles;
}

@Override
public Boolean isSupportUnknownFields() {
return supportUnknownFields;
}

public void setSupportUnknownFields(boolean supportUnknownFields) {
this.supportUnknownFields = supportUnknownFields;
}

@Override
public String getUnknownFieldsPropertyName() {
return unknownFieldsPropertyName;
}

public void setUnknownFieldsPropertyName(String unknownFieldsPropertyName) {
this.unknownFieldsPropertyName = unknownFieldsPropertyName;
@Override
public Boolean isGenerateNoArgsConstructorOnly() {
return generateNoArgsConstructorOnly;
}

public ParentInterfacesConfig getParentInterfaces() {
return parentInterfaces;
}

public String[] getConfigurationFiles() {
return configurationFiles;
}

private static Map<String, List<String>> convertToListsMap(Map<String, Properties> sourceMap) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,8 @@ trait GraphQLCodegenKeys {

val generateJacksonTypeIdResolver = settingKey[Boolean]("Specifies whether generated union interfaces should be annotated with a custom Jackson type id resolver generated in model package.")

val generateNoArgsConstructorOnly = settingKey[Boolean]("Specifies whether model classes should only have a no-args constructor. All-args constructor will not be generated in case value is .true.")

//for version
val javaxValidationApiVersion = settingKey[Option[String]]("javax-validation-api version")
val graphqlJavaCodegenVersion = settingKey[Option[String]]("graphql-java-codegen version")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ class GraphQLCodegenPlugin(configuration: Configuration, private[codegen] val co

supportUnknownFields := MappingConfigConstants.DEFAULT_SUPPORT_UNKNOWN_FIELDS,
unknownFieldsPropertyName := MappingConfigConstants.DEFAULT_UNKNOWN_FIELDS_PROPERTY_NAME,
generateNoArgsConstructorOnly := MappingConfigConstants.DEFAULT_GENERATE_NOARGS_CONSTRUCTOR_ONLY,

skip := false
)
Expand Down Expand Up @@ -179,6 +180,7 @@ class GraphQLCodegenPlugin(configuration: Configuration, private[codegen] val co
mappingConfig.setGeneratedLanguage((generatedLanguage in GraphQLCodegenConfig).value)
mappingConfig.setGenerateModelOpenClasses((generateModelOpenClasses in GraphQLCodegenConfig).value)
mappingConfig.setGenerateJacksonTypeIdResolver((generateJacksonTypeIdResolver in GraphQLCodegenConfig).value);
mappingConfig.setGenerateNoArgsConstructorOnly((generateNoArgsConstructorOnly in GraphQLCodegenConfig).value);

mappingConfig.setSupportUnknownFields((supportUnknownFields in GraphQLCodegenConfig).value)
mappingConfig.setUnknownFieldsPropertyName((unknownFieldsPropertyName in GraphQLCodegenConfig).value)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import static com.kobylynskyi.graphql.codegen.model.DataModelFields.GENERATED_ANNOTATION;
import static com.kobylynskyi.graphql.codegen.model.DataModelFields.GENERATED_INFO;
import static com.kobylynskyi.graphql.codegen.model.DataModelFields.GENERATE_MODEL_OPEN_CLASSES;
import static com.kobylynskyi.graphql.codegen.model.DataModelFields.GENERATE_NOARGS_CONSTRUCTOR_ONLY;
import static com.kobylynskyi.graphql.codegen.model.DataModelFields.IMMUTABLE_MODELS;
import static com.kobylynskyi.graphql.codegen.model.DataModelFields.INITIALIZE_NULLABLE_TYPES;
import static com.kobylynskyi.graphql.codegen.model.DataModelFields.JAVA_DOC;
Expand Down Expand Up @@ -78,6 +79,7 @@ public Map<String, Object> map(MappingContext mappingContext, ExtendedInputObjec
dataModel.put(INITIALIZE_NULLABLE_TYPES, mappingContext.isInitializeNullableTypes());
dataModel.put(SUPPORT_UNKNOWN_FIELDS, mappingContext.isSupportUnknownFields());
dataModel.put(UNKNOWN_FIELDS_PROPERTY_NAME, mappingContext.getUnknownFieldsPropertyName());
dataModel.put(GENERATE_NOARGS_CONSTRUCTOR_ONLY, mappingContext.isGenerateNoArgsConstructorOnly());
return dataModel;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import static com.kobylynskyi.graphql.codegen.model.DataModelFields.GENERATED_ANNOTATION;
import static com.kobylynskyi.graphql.codegen.model.DataModelFields.GENERATED_INFO;
import static com.kobylynskyi.graphql.codegen.model.DataModelFields.GENERATE_ALL_METHOD_IN_PROJECTION;
import static com.kobylynskyi.graphql.codegen.model.DataModelFields.GENERATE_NOARGS_CONSTRUCTOR_ONLY;
import static com.kobylynskyi.graphql.codegen.model.DataModelFields.JAVA_DOC;
import static com.kobylynskyi.graphql.codegen.model.DataModelFields.METHOD_NAME;
import static com.kobylynskyi.graphql.codegen.model.DataModelFields.OPERATION_NAME;
Expand Down Expand Up @@ -154,6 +155,7 @@ public Map<String, Object> mapParametrizedInput(MappingContext mappingContext,
dataModel.put(GENERATED_ANNOTATION, mappingContext.getAddGeneratedAnnotation());
dataModel.put(GENERATED_INFO, mappingContext.getGeneratedInformation());
dataModel.put(ENUM_IMPORT_IT_SELF_IN_SCALA, mappingContext.getEnumImportItSelfInScala());
dataModel.put(GENERATE_NOARGS_CONSTRUCTOR_ONLY, mappingContext.isGenerateNoArgsConstructorOnly());
// dataModel.put(TO_STRING, mappingConfig.getGenerateToString()); always generated for serialization purposes
return dataModel;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import static com.kobylynskyi.graphql.codegen.model.DataModelFields.GENERATED_ANNOTATION;
import static com.kobylynskyi.graphql.codegen.model.DataModelFields.GENERATED_INFO;
import static com.kobylynskyi.graphql.codegen.model.DataModelFields.GENERATE_MODEL_OPEN_CLASSES;
import static com.kobylynskyi.graphql.codegen.model.DataModelFields.GENERATE_NOARGS_CONSTRUCTOR_ONLY;
import static com.kobylynskyi.graphql.codegen.model.DataModelFields.GENERATE_SEALED_INTERFACES;
import static com.kobylynskyi.graphql.codegen.model.DataModelFields.IMMUTABLE_MODELS;
import static com.kobylynskyi.graphql.codegen.model.DataModelFields.IMPLEMENTS;
Expand Down Expand Up @@ -110,6 +111,7 @@ public Map<String, Object> map(MappingContext mappingContext,
dataModel.put(GENERATE_SEALED_INTERFACES, mappingContext.isGenerateSealedInterfaces());
dataModel.put(SUPPORT_UNKNOWN_FIELDS, mappingContext.isSupportUnknownFields());
dataModel.put(UNKNOWN_FIELDS_PROPERTY_NAME, mappingContext.getUnknownFieldsPropertyName());
dataModel.put(GENERATE_NOARGS_CONSTRUCTOR_ONLY, mappingContext.isGenerateNoArgsConstructorOnly());
return dataModel;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ public final class DataModelFields {
public static final String GENERATE_SEALED_INTERFACES = "generateSealedInterfaces";
public static final String SUPPORT_UNKNOWN_FIELDS = "supportUnknownFields";
public static final String UNKNOWN_FIELDS_PROPERTY_NAME = "unknownFieldsPropertyName";
public static final String GENERATE_NOARGS_CONSTRUCTOR_ONLY = "generateNoArgsConstructorOnly";

private DataModelFields() {
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -513,5 +513,14 @@ public interface GraphQLCodegenConfiguration {
*/
String getGeneratedAnnotation();

/**
* Specifies whether model classes should only have a no-args constructor.
* All-args constructor will not be generated in case returned value is <b>true</b>
*
* @return <b>true</b> if only no-args constructor should be generated.
* <b>false</b> if both no-args and all-args constructors should be generated
*/
Boolean isGenerateNoArgsConstructorOnly();


}
Loading

0 comments on commit 2507dd5

Please sign in to comment.