Skip to content

Commit b76c49b

Browse files
authored
Add option to render Kotlin functions with suspend modifier (#1377)
1 parent 2c2e17b commit b76c49b

File tree

16 files changed

+165
-2
lines changed

16 files changed

+165
-2
lines changed

docs/codegen-options.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
| `typeResolverPrefix` | String | Empty | Sets the prefix for GraphQL type resolver classes. |
3232
| `typeResolverSuffix` | String | `Resolver` | Sets the suffix for GraphQL type resolver classes. |
3333
| `customTypesMapping` | Map(String,String) | Empty | *See [CustomTypesMapping](#option-customtypesmapping)* |
34-
| `customTemplatesRoot` | File | Project's dir | Use to supply the path the to custom FreeMarker templates root directory. |
34+
| `customTemplatesRoot` | File | Project's dir | Use to supply the path the to custom FreeMarker templates root directory. |
3535
| `customTemplates` | Map(String,String) | Empty | Use to supply paths to custom FreeMarker templates for code generation. |
3636
| `customAnnotationsMapping` | Map(String,String[]) | Empty | *See [CustomAnnotationsMapping](#option-customannotationsmapping)* |
3737
| `directiveAnnotationsMapping` | Map(String,String[]) | Empty | *See [DirectiveAnnotationsMapping](#option-directiveannotationsmapping)* |
@@ -45,6 +45,7 @@
4545
| `generateModelsForRootTypes` | Boolean | False | Specifies whether model classes should be generated for `type Query`, `type Subscription`, `type Mutation`. |
4646
| `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. |
4747
| `generateApisWithThrowsException` | Boolean | True | Specifies whether api interface methods should have `throws Exception` in signature. |
48+
| `generateApisWithSuspendFunctions` | Boolean | False | Specifies whether api interface methods should have `suspend` modifier in signature. Only supported in Kotlin. |
4849
| `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> |
4950
| `generateModelsWithPublicFields` | Boolean | False | Specifies whether model classes should have public fields and NO getters/setters. By default, fields are private and there are getters/setters for each field. |
5051
| `apiReturnType` | String | Empty | Return type for api methods (query/mutation). For example: `reactor.core.publisher.Mono`, etc. |

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,8 @@ public class GraphQLCodegenGradleTask extends DefaultTask implements GraphQLCode
8787
private Boolean generateModelsForRootTypes = MappingConfigConstants.DEFAULT_GENERATE_MODELS_FOR_ROOT_TYPES;
8888
private Boolean useOptionalForNullableReturnTypes = MappingConfigConstants.DEFAULT_USE_OPTIONAL_FOR_NULLABLE_RETURN_TYPES;
8989
private Boolean generateApisWithThrowsException = MappingConfigConstants.DEFAULT_GENERATE_APIS_WITH_THROWS_EXCEPTION;
90+
private Boolean generateApisWithSuspendFunctions =
91+
MappingConfigConstants.DEFAULT_GENERATE_APIS_WITH_SUSPEND_FUNCTIONS;
9092
private Boolean generateJacksonTypeIdResolver = MappingConfigConstants.DEFAULT_GENERATE_JACKSON_TYPE_ID_RESOLVER;
9193
private Boolean addGeneratedAnnotation = MappingConfigConstants.DEFAULT_ADD_GENERATED_ANNOTATION;
9294
private Boolean generateNoArgsConstructorOnly = MappingConfigConstants.DEFAULT_GENERATE_NOARGS_CONSTRUCTOR_ONLY;
@@ -167,6 +169,7 @@ public void generate() throws Exception {
167169
mappingConfig.setGenerateToString(generateToString);
168170
mappingConfig.setUseOptionalForNullableReturnTypes(useOptionalForNullableReturnTypes);
169171
mappingConfig.setGenerateApisWithThrowsException(generateApisWithThrowsException);
172+
mappingConfig.setGenerateApisWithSuspendFunctions(generateApisWithSuspendFunctions);
170173
mappingConfig.setGenerateJacksonTypeIdResolver(generateJacksonTypeIdResolver);
171174
mappingConfig.setGenerateNoArgsConstructorOnly(generateNoArgsConstructorOnly);
172175
mappingConfig.setGenerateModelsWithPublicFields(generateModelsWithPublicFields);
@@ -688,6 +691,17 @@ public void setGenerateApisWithThrowsException(Boolean generateApisWithThrowsExc
688691
this.generateApisWithThrowsException = generateApisWithThrowsException;
689692
}
690693

694+
@Input
695+
@Optional
696+
@Override
697+
public Boolean getGenerateApisWithSuspendFunctions() {
698+
return generateApisWithSuspendFunctions;
699+
}
700+
701+
public void setGenerateApisWithSuspendFunctions(Boolean generateApisWithSuspendFunctions) {
702+
this.generateApisWithSuspendFunctions = generateApisWithSuspendFunctions;
703+
}
704+
691705
@Input
692706
@Optional
693707
@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
@@ -155,6 +155,9 @@ public class GraphQLCodegenMojo extends AbstractMojo implements GraphQLCodegenCo
155155
@Parameter(defaultValue = MappingConfigConstants.DEFAULT_GENERATE_APIS_WITH_THROWS_EXCEPTION_STRING)
156156
private boolean generateApisWithThrowsException;
157157

158+
@Parameter(defaultValue = MappingConfigConstants.DEFAULT_GENERATE_APIS_WITH_SUSPEND_FUNCTIONS_STRING)
159+
private boolean generateApisWithSuspendFunctions;
160+
158161
@Parameter(defaultValue = MappingConfigConstants.DEFAULT_GENERATE_JACKSON_TYPE_ID_RESOLVER_STRING)
159162
private boolean generateJacksonTypeIdResolver;
160163

@@ -285,6 +288,7 @@ public void execute() throws MojoExecutionException {
285288
mappingConfig.setGenerateModelsForRootTypes(generateModelsForRootTypes);
286289
mappingConfig.setUseOptionalForNullableReturnTypes(useOptionalForNullableReturnTypes);
287290
mappingConfig.setGenerateApisWithThrowsException(generateApisWithThrowsException);
291+
mappingConfig.setGenerateApisWithSuspendFunctions(generateApisWithSuspendFunctions);
288292
mappingConfig.setGenerateJacksonTypeIdResolver(generateJacksonTypeIdResolver);
289293
mappingConfig.setAddGeneratedAnnotation(addGeneratedAnnotation);
290294
mappingConfig.setGeneratedAnnotation(generatedAnnotation);
@@ -550,6 +554,11 @@ public Boolean getGenerateApisWithThrowsException() {
550554
return generateApisWithThrowsException;
551555
}
552556

557+
@Override
558+
public Boolean getGenerateApisWithSuspendFunctions() {
559+
return generateApisWithSuspendFunctions;
560+
}
561+
553562
@Override
554563
public Boolean getGenerateJacksonTypeIdResolver() {
555564
return generateJacksonTypeIdResolver;

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,7 @@ private OperationDefinition map(MappingContext mappingContext, ExtendedFieldDefi
212212
operation.setJavaDoc(fieldDef.getJavaDoc());
213213
operation.setDeprecated(DeprecatedDefinitionBuilder.build(mappingContext, fieldDef));
214214
operation.setThrowsException(mappingContext.getGenerateApisWithThrowsException());
215+
operation.setSuspend(mappingContext.getGenerateApisWithSuspendFunctions());
215216
return operation;
216217
}
217218

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,13 @@ public interface GraphQLCodegenConfiguration {
267267
*/
268268
Boolean getGenerateApisWithThrowsException();
269269

270+
/**
271+
* Whether signatures of API interface methods should have <code>suspend</code> modifier.
272+
*
273+
* @return <b>true</b> if API interfaces methods signature should contain <code>suspend</code> modifier.
274+
*/
275+
Boolean getGenerateApisWithSuspendFunctions();
276+
270277
/**
271278
* Specifies whether generated classes should be annotated with @Generated
272279
*

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ public class MappingConfig implements GraphQLCodegenConfiguration, Combinable<Ma
5353
private Boolean generateModelsForRootTypes;
5454
private Boolean useOptionalForNullableReturnTypes;
5555
private Boolean generateApisWithThrowsException;
56+
private Boolean generateApisWithSuspendFunctions;
5657
private Boolean addGeneratedAnnotation;
5758
private Boolean generateJacksonTypeIdResolver;
5859
private Boolean supportUnknownFields;
@@ -168,6 +169,8 @@ public void combine(MappingConfig source) {
168169
GraphQLCodegenConfiguration::getUseOptionalForNullableReturnTypes);
169170
generateApisWithThrowsException = getValueOrDefaultToThis(source,
170171
GraphQLCodegenConfiguration::getGenerateApisWithThrowsException);
172+
generateApisWithSuspendFunctions = getValueOrDefaultToThis(source,
173+
GraphQLCodegenConfiguration::getGenerateApisWithSuspendFunctions);
171174
addGeneratedAnnotation = getValueOrDefaultToThis(source,
172175
GraphQLCodegenConfiguration::getAddGeneratedAnnotation);
173176
generateJacksonTypeIdResolver = getValueOrDefaultToThis(source,
@@ -526,6 +529,15 @@ public void setGenerateApisWithThrowsException(Boolean generateApisWithThrowsExc
526529
this.generateApisWithThrowsException = generateApisWithThrowsException;
527530
}
528531

532+
@Override
533+
public Boolean getGenerateApisWithSuspendFunctions() {
534+
return generateApisWithSuspendFunctions;
535+
}
536+
537+
public void setGenerateApisWithSuspendFunctions(Boolean generateApisWithSuspendFunctions) {
538+
this.generateApisWithSuspendFunctions = generateApisWithSuspendFunctions;
539+
}
540+
529541
@Override
530542
public Boolean getAddGeneratedAnnotation() {
531543
return addGeneratedAnnotation;

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@ public class MappingConfigConstants {
4141
public static final boolean DEFAULT_GENERATE_APIS_WITH_THROWS_EXCEPTION = true;
4242
public static final String DEFAULT_GENERATE_APIS_WITH_THROWS_EXCEPTION_STRING = "true";
4343

44+
public static final boolean DEFAULT_GENERATE_APIS_WITH_SUSPEND_FUNCTIONS = false;
45+
public static final String DEFAULT_GENERATE_APIS_WITH_SUSPEND_FUNCTIONS_STRING = "false";
46+
4447
public static final boolean DEFAULT_ADD_GENERATED_ANNOTATION = true;
4548
public static final String DEFAULT_ADD_GENERATED_ANNOTATION_STRING = "true";
4649

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,10 @@ public static void initDefaultValues(MappingConfig mappingConfig) {
7272
mappingConfig.setGenerateApisWithThrowsException(
7373
MappingConfigConstants.DEFAULT_GENERATE_APIS_WITH_THROWS_EXCEPTION);
7474
}
75+
if (mappingConfig.getGenerateApisWithSuspendFunctions() == null) {
76+
mappingConfig.setGenerateApisWithSuspendFunctions(
77+
MappingConfigConstants.DEFAULT_GENERATE_APIS_WITH_SUSPEND_FUNCTIONS);
78+
}
7579
if (mappingConfig.getAddGeneratedAnnotation() == null) {
7680
mappingConfig.setAddGeneratedAnnotation(MappingConfigConstants.DEFAULT_ADD_GENERATED_ANNOTATION);
7781
}

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,11 @@ public Boolean getGenerateApisWithThrowsException() {
234234
return config.getGenerateApisWithThrowsException();
235235
}
236236

237+
@Override
238+
public Boolean getGenerateApisWithSuspendFunctions() {
239+
return config.getGenerateApisWithSuspendFunctions();
240+
}
241+
237242
@Override
238243
public Boolean getAddGeneratedAnnotation() {
239244
return config.getAddGeneratedAnnotation();

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ public class OperationDefinition {
2626
private List<String> javaDoc = new ArrayList<>();
2727
private DeprecatedDefinition deprecated;
2828
private boolean throwsException;
29+
private boolean suspend;
2930

3031
public String getName() {
3132
return name;
@@ -90,4 +91,12 @@ public boolean isThrowsException() {
9091
public void setThrowsException(boolean throwsException) {
9192
this.throwsException = throwsException;
9293
}
94+
95+
public boolean isSuspend() {
96+
return suspend;
97+
}
98+
99+
public void setSuspend(boolean suspend) {
100+
this.suspend = suspend;
101+
}
93102
}

0 commit comments

Comments
 (0)