Skip to content

Commit dee019b

Browse files
authored
Merge pull request #18 from kobylynskyi/feature/subscription-return-type
Subscription methods with custom return type #14
2 parents a3b67b5 + 63878f6 commit dee019b

File tree

9 files changed

+159
-111
lines changed

9 files changed

+159
-111
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ Please refer to:
2626
| modelValidationAnnotation | String | @javax.validation.<br>constraints.NotNull | Annotation for mandatory (NonNull) fields. Can be null/empty. |
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). |
29+
| subscriptionReturnType | String | Empty | Return type for subscription methods. For example: `org.reactivestreams.Publisher`, `io.reactivex.Observable`, etc. |
2930
| generateEqualsAndHashCode | Boolean | False | Specifies whether generated model classes should have equals and hashCode methods defined. |
3031
| generateToString | Boolean | False | Specifies whether generated model classes should have toString method defined. |
3132

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: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ public class MappingConfig implements Combinable<MappingConfig> {
3636
private String modelNamePrefix;
3737
private String modelNameSuffix;
3838
private String modelValidationAnnotation;
39+
private String subscriptionReturnType;
3940
private Boolean generateEqualsAndHashCode;
4041
private Boolean generateToString;
4142

@@ -77,6 +78,7 @@ public void combine(MappingConfig source) {
7778
this.modelNamePrefix = source.modelNamePrefix != null ? source.modelNamePrefix : this.modelNamePrefix;
7879
this.modelNameSuffix = source.modelNameSuffix != null ? source.modelNameSuffix : this.modelNameSuffix;
7980
this.modelValidationAnnotation = source.modelValidationAnnotation != null ? source.modelValidationAnnotation : this.modelValidationAnnotation;
81+
this.subscriptionReturnType = source.subscriptionReturnType != null ? source.subscriptionReturnType : this.subscriptionReturnType;
8082
this.generateEqualsAndHashCode = source.generateEqualsAndHashCode != null ? source.generateEqualsAndHashCode : this.generateEqualsAndHashCode;
8183
this.generateToString = source.generateToString != null ? source.generateToString : this.generateToString;
8284
}

0 commit comments

Comments
 (0)