Skip to content

Commit 94a8357

Browse files
robbertnoordzijRobbert Noordzij
andauthored
Ability to disable generation of the all$ method in projections (#645)
* Add flag for generating the all method in projections * Add generation of all methods to plugin * Add to documentation * Move pojectionDepthOnFields to concrete implementation of projection Co-authored-by: Robbert Noordzij <robbert.noordzij@ns.nl>
1 parent cd49eae commit 94a8357

File tree

51 files changed

+379
-163
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

51 files changed

+379
-163
lines changed

docs/codegen-options.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ See [DirectiveAnnotationsMapping](#option-directiveannotationsmapping)* |
5151
| `responseProjectionSuffix` | String | ResponseProjection | Sets the suffix for `ResponseProjection` classes. |
5252
| `parametrizedInputSuffix` | String | ParametrizedInput | Sets the suffix for `ParametrizedInput` classes. |
5353
| `parentInterfaces` | *See<br>[parentInterfaces](#option-parentinterfaces)* | Empty | Block to define parent interfaces for generated interfaces (query / mutation / subscription / type resolver). *See [parentInterfaces](#option-parentinterfaces)* |
54+
| `generateAllMethodInProjection` | Boolean | true | Enables whether the `all$()` method should be generated in the projection classes. Disabling enforces the client to select the fields manually. |
5455
| `responseProjectionMaxDepth` | Integer | 3 | Sets max depth when use `all$()` which for facilitating the construction of projection automatically, the fields on all projections are provided when it be invoked. This is a global configuration, of course, you can use `all$(max)` to set for each method. For self recursive types, too big depth may result in a large number of returned data!|
5556
| `generatedLanguage` | Enum | GeneratedLanguage.JAVA | Choose which language you want to generate, Java,Scala,Kotlin were supported. Note that due to language features, there are slight differences in default values between languages.|
5657
| `generateModelOpenClasses` | Boolean | false | The class type of the generated model. If true, generate normal classes, else generate data classes. It only support in kotlin(```data class```) and scala(```case class```). Maybe we will consider to support Java ```record``` in the future.|

plugins/gradle/graphql-java-codegen-gradle-plugin/src/main/java/io/github/kobylynskyi/graphql/codegen/gradle/GraphQLCodegenGradleTask.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ public class GraphQLCodegenGradleTask extends DefaultTask implements GraphQLCode
9494
private String responseSuffix;
9595
private String responseProjectionSuffix;
9696
private String parametrizedInputSuffix;
97+
private Boolean generateAllMethodInProjection = MappingConfigConstants.DEFAULT_GENERATE_ALL_METHOD;
9798
private int responseProjectionMaxDepth = MappingConfigConstants.DEFAULT_RESPONSE_PROJECTION_MAX_DEPTH;
9899
private Set<String> useObjectMapperForRequestSerialization = new HashSet<>();
99100

@@ -165,6 +166,7 @@ public void generate() throws Exception {
165166
mappingConfig.setParametrizedInputSuffix(parametrizedInputSuffix);
166167
mappingConfig.setUseObjectMapperForRequestSerialization(useObjectMapperForRequestSerialization != null ?
167168
useObjectMapperForRequestSerialization : new HashSet<>());
169+
mappingConfig.setGenerateAllMethodInProjection(generateAllMethodInProjection);
168170
mappingConfig.setResponseProjectionMaxDepth(responseProjectionMaxDepth);
169171

170172
mappingConfig.setResolverParentInterface(getResolverParentInterface());
@@ -742,6 +744,17 @@ public void setUseObjectMapperForRequestSerialization(Set<String> useObjectMappe
742744
this.useObjectMapperForRequestSerialization = useObjectMapperForRequestSerialization;
743745
}
744746

747+
@Input
748+
@Optional
749+
@Override
750+
public Boolean getGenerateAllMethodInProjection() {
751+
return generateAllMethodInProjection;
752+
}
753+
754+
public void setGenerateAllMethodInProjection(boolean generateAllMethodInProjection) {
755+
this.generateAllMethodInProjection = generateAllMethodInProjection;
756+
}
757+
745758
@Input
746759
@Optional
747760
@Override

plugins/maven/graphql-java-codegen-maven-plugin/src/main/java/io/github/kobylynskyi/graphql/codegen/GraphQLCodegenMojo.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,9 @@ public class GraphQLCodegenMojo extends AbstractMojo implements GraphQLCodegenCo
185185
@Parameter(defaultValue = MappingConfigConstants.DEFAULT_RESPONSE_PROJECTION_MAX_DEPTH_STRING)
186186
private int responseProjectionMaxDepth;
187187

188+
@Parameter(defaultValue = MappingConfigConstants.DEFAULT_GENERATE_ALL_METHOD_STRING)
189+
private boolean generateAllMethodInProjection;
190+
188191
@Parameter
189192
private ParentInterfacesConfig parentInterfaces = new ParentInterfacesConfig();
190193

@@ -248,6 +251,7 @@ public void execute() throws MojoExecutionException {
248251
mappingConfig.setResponseSuffix(responseSuffix);
249252
mappingConfig.setResponseProjectionSuffix(responseProjectionSuffix);
250253
mappingConfig.setParametrizedInputSuffix(parametrizedInputSuffix);
254+
mappingConfig.setGenerateAllMethodInProjection(generateAllMethodInProjection);
251255
mappingConfig.setResponseProjectionMaxDepth(responseProjectionMaxDepth);
252256
mappingConfig.setUseObjectMapperForRequestSerialization(mapToHashSet(useObjectMapperForRequestSerialization));
253257
mappingConfig.setTypesAsInterfaces(mapToHashSet(typesAsInterfaces));
@@ -515,6 +519,11 @@ public Set<String> getFieldsWithoutResolvers() {
515519
return mapToHashSet(fieldsWithoutResolvers);
516520
}
517521

522+
@Override
523+
public Boolean getGenerateAllMethodInProjection() {
524+
return generateAllMethodInProjection;
525+
}
526+
518527
@Override
519528
public Integer getResponseProjectionMaxDepth() {
520529
return responseProjectionMaxDepth;

plugins/sbt/graphql-java-codegen-sbt-plugin/src/main/scala/io/github/dreamylost/graphql/codegen/GraphQLCodegenKeys.scala

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,8 @@ trait GraphQLCodegenKeys {
114114

115115
val graphqlQueryIntrospectionResultPath = settingKey[Option[String]]("graphqlQueryIntrospectionResultPath")
116116

117+
val generateAllMethodInProjection = settingKey[Boolean]("generateAllMethodInProjection")
118+
117119
val responseProjectionMaxDepth = settingKey[Int]("limit depth when the projection is constructed automatically")
118120

119121
val relayConfig = settingKey[RelayConfig]("Can be used to supply a custom configuration for Relay support.")

plugins/sbt/graphql-java-codegen-sbt-plugin/src/main/scala/io/github/dreamylost/graphql/codegen/GraphQLCodegenPlugin.scala

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@ class GraphQLCodegenPlugin(configuration: Configuration, private[codegen] val co
114114
generateToString := MappingConfigConstants.DEFAULT_TO_STRING,
115115
// parent interfaces configs:
116116
parentInterfaces := parentInterfacesConfig,
117+
generateAllMethodInProjection := MappingConfigConstants.DEFAULT_GENERATE_ALL_METHOD,
117118
responseProjectionMaxDepth := MappingConfigConstants.DEFAULT_RESPONSE_PROJECTION_MAX_DEPTH
118119
)
119120

@@ -163,6 +164,7 @@ class GraphQLCodegenPlugin(configuration: Configuration, private[codegen] val co
163164
mappingConfig.setUseOptionalForNullableReturnTypes((useOptionalForNullableReturnTypes in GraphQLCodegenConfig).value)
164165
mappingConfig.setGenerateApisWithThrowsException((generateApisWithThrowsException in GraphQLCodegenConfig).value)
165166
mappingConfig.setAddGeneratedAnnotation((addGeneratedAnnotation in GraphQLCodegenConfig).value)
167+
mappingConfig.setGenerateAllMethodInProjection((generateAllMethodInProjection in GraphQLCodegenConfig).value)
166168
mappingConfig.setResponseProjectionMaxDepth((responseProjectionMaxDepth in GraphQLCodegenConfig).value)
167169
mappingConfig.setRelayConfig((relayConfig in GraphQLCodegenConfig).value)
168170
mappingConfig.setGeneratedLanguage((generatedLanguage in GraphQLCodegenConfig).value)

src/main/java/com/kobylynskyi/graphql/codegen/FreeMarkerTemplatesRegistry.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
import com.kobylynskyi.graphql.codegen.model.GeneratedLanguage;
44
import com.kobylynskyi.graphql.codegen.model.exception.UnableToLoadFreeMarkerTemplateException;
5+
import freemarker.core.OutputFormat;
6+
import freemarker.core.PlainTextOutputFormat;
57
import freemarker.ext.beans.BeansWrapper;
68
import freemarker.template.Configuration;
79
import freemarker.template.Template;
@@ -33,6 +35,7 @@ class FreeMarkerTemplatesRegistry {
3335
Configuration configuration = new Configuration(FREEMARKER_TEMPLATE_VERSION);
3436
configuration.setClassLoaderForTemplateLoading(GraphQLCodegen.class.getClassLoader(), "");
3537
configuration.setDefaultEncoding("UTF-8");
38+
configuration.setOutputFormat(PlainTextOutputFormat.INSTANCE);
3639
configuration.setTemplateExceptionHandler(TemplateExceptionHandler.RETHROW_HANDLER);
3740
configuration.setLogTemplateExceptions(false);
3841
configuration.setWrapUncheckedExceptions(true);

src/main/java/com/kobylynskyi/graphql/codegen/GraphQLCodegen.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,9 @@ protected void initDefaultValues(MappingConfig mappingConfig) {
208208
if (mappingConfig.getGeneratedLanguage() == null) {
209209
mappingConfig.setGeneratedLanguage(MappingConfigConstants.DEFAULT_GENERATED_LANGUAGE);
210210
}
211+
if (mappingConfig.getGenerateAllMethodInProjection() == null) {
212+
mappingConfig.setGenerateAllMethodInProjection(MappingConfigConstants.DEFAULT_GENERATE_ALL_METHOD);
213+
}
211214
}
212215

213216
private void validateConfigs(MappingConfig mappingConfig) {

src/main/java/com/kobylynskyi/graphql/codegen/mapper/RequestResponseDefinitionToDataModelMapper.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import static com.kobylynskyi.graphql.codegen.model.DataModelFields.FIELDS;
2828
import static com.kobylynskyi.graphql.codegen.model.DataModelFields.GENERATED_ANNOTATION;
2929
import static com.kobylynskyi.graphql.codegen.model.DataModelFields.GENERATED_INFO;
30+
import static com.kobylynskyi.graphql.codegen.model.DataModelFields.GENERATE_ALL_METHOD_IN_PROJECTION;
3031
import static com.kobylynskyi.graphql.codegen.model.DataModelFields.JAVA_DOC;
3132
import static com.kobylynskyi.graphql.codegen.model.DataModelFields.METHOD_NAME;
3233
import static com.kobylynskyi.graphql.codegen.model.DataModelFields.OPERATION_NAME;
@@ -118,6 +119,7 @@ public Map<String, Object> mapResponseProjection(MappingContext mappingContext,
118119
dataModel.put(EQUALS_AND_HASH_CODE, mappingContext.getGenerateEqualsAndHashCode());
119120
dataModel.put(GENERATED_ANNOTATION, mappingContext.getAddGeneratedAnnotation());
120121
dataModel.put(GENERATED_INFO, mappingContext.getGeneratedInformation());
122+
dataModel.put(GENERATE_ALL_METHOD_IN_PROJECTION, mappingContext.getGenerateAllMethodInProjection());
121123
dataModel.put(RESPONSE_PROJECTION_MAX_DEPTH, mappingContext.getResponseProjectionMaxDepth());
122124
// dataModel.put(TO_STRING, mappingConfig.getGenerateToString()); always generated for serialization purposes
123125
return dataModel;

src/main/java/com/kobylynskyi/graphql/codegen/model/DataModelFields.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ public final class DataModelFields {
2828
public static final String RETURN_TYPE_NAME = "returnTypeName";
2929
public static final String GENERATED_ANNOTATION = "generatedAnnotation";
3030
public static final String GENERATED_INFO = "generatedInfo";
31+
public static final String GENERATE_ALL_METHOD_IN_PROJECTION = "generateAllMethodInProjection";
3132
public static final String RESPONSE_PROJECTION_MAX_DEPTH = "responseProjectionMaxDepth";
3233
public static final String ENUM_IMPORT_IT_SELF_IN_SCALA = "enumImportItSelfInScala";
3334
public static final String PARENT_INTERFACE_PROPERTIES = "parentInterfaceProperties";

src/main/java/com/kobylynskyi/graphql/codegen/model/GraphQLCodegenConfiguration.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -375,6 +375,13 @@ public interface GraphQLCodegenConfiguration {
375375
*/
376376
String getResolverParentInterface();
377377

378+
/**
379+
* Enables the generation of the all$ method in the projection classes of the client.
380+
*
381+
* @return whether the generation is enabled.
382+
*/
383+
Boolean getGenerateAllMethodInProjection();
384+
378385
/**
379386
* Limit depth when `all$` invoke which has subProjections
380387
*

0 commit comments

Comments
 (0)