Skip to content

Commit b534b70

Browse files
committed
Introduce custom return type for subscription methods #14
1 parent 06f2db2 commit b534b70

File tree

9 files changed

+164
-113
lines changed

9 files changed

+164
-113
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ Please refer to:
2727
| modelNamePrefix | String | Empty | Sets the prefix for GraphQL model classes (type, input, interface, enum, union). |
2828
| modelNameSuffix | String | Empty | Sets the suffix for GraphQL model classes (type, input, interface, enum, union). |
2929
| modelAnnotations | String | Empty | Annotations that will be added to model classes (type, input). |
30+
| subscriptionReturnType | String | Empty | Return type for subscription methods. For example: `org.reactivestreams.Publisher`, `io.reactivex.Observable`, etc. |
3031
| generateEqualsAndHashCode | Boolean | False | Specifies whether generated model classes should have equals and hashCode methods defined. |
3132
| generateToString | Boolean | False | Specifies whether generated model classes should have toString method defined. |
3233

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,8 @@ public static Map<String, Object> map(MappingConfig mappingConfig, FieldDefiniti
4949
static OperationDefinition mapFieldDefinition(MappingConfig mappingConfig, FieldDefinition fieldDef, String parentTypeName) {
5050
OperationDefinition operation = new OperationDefinition();
5151
operation.setName(fieldDef.getName());
52-
operation.setType(GraphqlTypeToJavaTypeMapper.getJavaType(mappingConfig, fieldDef.getType(), fieldDef.getName(), parentTypeName));
52+
String javaType = GraphqlTypeToJavaTypeMapper.getJavaType(mappingConfig, fieldDef.getType(), fieldDef.getName(), parentTypeName);
53+
operation.setType(GraphqlTypeToJavaTypeMapper.wrapIntoSubscriptionIfRequired(mappingConfig, javaType, parentTypeName));
5354
operation.setAnnotations(GraphqlTypeToJavaTypeMapper.getAnnotations(mappingConfig, fieldDef.getType(), fieldDef.getName(), parentTypeName));
5455
operation.setParameters(InputValueDefinitionToParameterMapper.map(mappingConfig, fieldDef.getInputValueDefinitions(), fieldDef.getName()));
5556
return operation;

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

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.kobylynskyi.graphql.codegen.mapper;
22

3+
import static graphql.language.OperationDefinition.*;
4+
35
import com.kobylynskyi.graphql.codegen.model.MappingConfig;
46
import com.kobylynskyi.graphql.codegen.model.ParameterDefinition;
57
import graphql.language.*;
@@ -16,7 +18,7 @@
1618
class GraphqlTypeToJavaTypeMapper {
1719

1820
/**
19-
* Map GraphQL's FieldDefinition to a Freemarker-understandable format of operation
21+
* Map GraphQL's FieldDefinition to a Freemarker-understandable format of parameter
2022
*
2123
* @param mappingConfig Global mapping configuration
2224
* @param fieldDef GraphQL field definition
@@ -62,19 +64,19 @@ static String getJavaType(MappingConfig mappingConfig, Type type) {
6264
* Convert GraphQL type to a corresponding Java type
6365
*
6466
* @param mappingConfig Global mapping configuration
65-
* @param graphlType GraphQL type
67+
* @param graphqlType GraphQL type
6668
* @param name GraphQL type name
6769
* @param parentTypeName Name of the parent type
6870
* @return Corresponding Java type
6971
*/
70-
static String getJavaType(MappingConfig mappingConfig, Type graphlType, String name, String parentTypeName) {
71-
if (graphlType instanceof TypeName) {
72-
return getJavaType(mappingConfig, ((TypeName) graphlType).getName(), name, parentTypeName);
73-
} else if (graphlType instanceof ListType) {
74-
String mappedCollectionType = getJavaType(mappingConfig, ((ListType) graphlType).getType(), name, parentTypeName);
72+
static String getJavaType(MappingConfig mappingConfig, Type graphqlType, String name, String parentTypeName) {
73+
if (graphqlType instanceof TypeName) {
74+
return getJavaType(mappingConfig, ((TypeName) graphqlType).getName(), name, parentTypeName);
75+
} else if (graphqlType instanceof ListType) {
76+
String mappedCollectionType = getJavaType(mappingConfig, ((ListType) graphqlType).getType(), name, parentTypeName);
7577
return wrapIntoJavaCollection(mappedCollectionType);
76-
} else if (graphlType instanceof NonNullType) {
77-
return getJavaType(mappingConfig, ((NonNullType) graphlType).getType(), name, parentTypeName);
78+
} else if (graphqlType instanceof NonNullType) {
79+
return getJavaType(mappingConfig, ((NonNullType) graphqlType).getType(), name, parentTypeName);
7880
}
7981
return null;
8082
}
@@ -156,4 +158,13 @@ private static String wrapIntoJavaCollection(String type) {
156158
return String.format("Collection<%s>", type);
157159
}
158160

161+
static String wrapIntoSubscriptionIfRequired(MappingConfig mappingConfig, String javaTypeName, String parentTypeName) {
162+
if (parentTypeName.equalsIgnoreCase(Operation.SUBSCRIPTION.name())
163+
&& mappingConfig.getSubscriptionReturnType() != null
164+
&& !mappingConfig.getSubscriptionReturnType().trim().isEmpty()) {
165+
return String.format("%s<%s>", mappingConfig.getSubscriptionReturnType(), javaTypeName);
166+
}
167+
return javaTypeName;
168+
}
169+
159170
}

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
import java.util.Map;
66
import java.util.Set;
77

8-
import freemarker.template.utility.CollectionUtils;
98
import lombok.Data;
109

1110
/**
@@ -42,6 +41,7 @@ public class MappingConfig implements Combinable<MappingConfig> {
4241
private String modelNamePrefix;
4342
private String modelNameSuffix;
4443
private String modelValidationAnnotation;
44+
private String subscriptionReturnType;
4545
private Boolean generateEqualsAndHashCode;
4646
private Boolean generateToString;
4747

@@ -88,6 +88,8 @@ public void combine(MappingConfig source) {
8888
this.modelNamePrefix = source.modelNamePrefix != null ? source.modelNamePrefix : this.modelNamePrefix;
8989
this.modelNameSuffix = source.modelNameSuffix != null ? source.modelNameSuffix : this.modelNameSuffix;
9090
this.modelValidationAnnotation = source.modelValidationAnnotation != null ? source.modelValidationAnnotation : this.modelValidationAnnotation;
91+
this.subscriptionReturnType = source.subscriptionReturnType
92+
!= null ? source.subscriptionReturnType : this.subscriptionReturnType;
9193
this.generateEqualsAndHashCode = source.generateEqualsAndHashCode != null ? source.generateEqualsAndHashCode : this.generateEqualsAndHashCode;
9294
this.generateToString = source.generateToString != null ? source.generateToString : this.generateToString;
9395
}

0 commit comments

Comments
 (0)