Subscription methods with custom return type #14 by kobylynskyi · Pull Request #18 · kobylynskyi/graphql-java-codegen · GitHub
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ public static Map<String, Object> map(MappingConfig mappingConfig, FieldDefiniti
static OperationDefinition mapFieldDefinition(MappingConfig mappingConfig, FieldDefinition fieldDef, String parentTypeName) {
OperationDefinition operation = new OperationDefinition();
operation.setName(fieldDef.getName());
operation.setType(GraphqlTypeToJavaTypeMapper.getJavaType(mappingConfig, fieldDef.getType(), fieldDef.getName(), parentTypeName));
String javaType = GraphqlTypeToJavaTypeMapper.getJavaType(mappingConfig, fieldDef.getType(), fieldDef.getName(), parentTypeName);
operation.setType(GraphqlTypeToJavaTypeMapper.wrapIntoSubscriptionIfRequired(mappingConfig, javaType, parentTypeName));
operation.setAnnotations(GraphqlTypeToJavaTypeMapper.getAnnotations(mappingConfig, fieldDef.getType(), fieldDef.getName(), parentTypeName));
operation.setParameters(InputValueDefinitionToParameterMapper.map(mappingConfig, fieldDef.getInputValueDefinitions(), fieldDef.getName()));
return operation;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.kobylynskyi.graphql.codegen.mapper;

import static graphql.language.OperationDefinition.*;

import com.kobylynskyi.graphql.codegen.model.MappingConfig;
import com.kobylynskyi.graphql.codegen.model.ParameterDefinition;
import graphql.language.*;
Expand All @@ -16,7 +18,7 @@
class GraphqlTypeToJavaTypeMapper {

/**
* Map GraphQL's FieldDefinition to a Freemarker-understandable format of operation
* Map GraphQL's FieldDefinition to a Freemarker-understandable format of parameter
*
* @param mappingConfig Global mapping configuration
* @param fieldDef GraphQL field definition
Expand Down Expand Up @@ -62,19 +64,19 @@ static String getJavaType(MappingConfig mappingConfig, Type type) {
* Convert GraphQL type to a corresponding Java type
*
* @param mappingConfig Global mapping configuration
* @param graphlType GraphQL type
* @param graphqlType GraphQL type
* @param name GraphQL type name
* @param parentTypeName Name of the parent type
* @return Corresponding Java type
*/
static String getJavaType(MappingConfig mappingConfig, Type graphlType, String name, String parentTypeName) {
if (graphlType instanceof TypeName) {
return getJavaType(mappingConfig, ((TypeName) graphlType).getName(), name, parentTypeName);
} else if (graphlType instanceof ListType) {
String mappedCollectionType = getJavaType(mappingConfig, ((ListType) graphlType).getType(), name, parentTypeName);
static String getJavaType(MappingConfig mappingConfig, Type graphqlType, String name, String parentTypeName) {
if (graphqlType instanceof TypeName) {
return getJavaType(mappingConfig, ((TypeName) graphqlType).getName(), name, parentTypeName);
} else if (graphqlType instanceof ListType) {
String mappedCollectionType = getJavaType(mappingConfig, ((ListType) graphqlType).getType(), name, parentTypeName);
return wrapIntoJavaCollection(mappedCollectionType);
} else if (graphlType instanceof NonNullType) {
return getJavaType(mappingConfig, ((NonNullType) graphlType).getType(), name, parentTypeName);
} else if (graphqlType instanceof NonNullType) {
return getJavaType(mappingConfig, ((NonNullType) graphqlType).getType(), name, parentTypeName);
}
return null;
}
Expand Down Expand Up @@ -156,4 +158,13 @@ private static String wrapIntoJavaCollection(String type) {
return String.format("Collection<%s>", type);
}

static String wrapIntoSubscriptionIfRequired(MappingConfig mappingConfig, String javaTypeName, String parentTypeName) {
if (parentTypeName.equalsIgnoreCase(Operation.SUBSCRIPTION.name())
&& mappingConfig.getSubscriptionReturnType() != null
&& !mappingConfig.getSubscriptionReturnType().trim().isEmpty()) {
return String.format("%s<%s>", mappingConfig.getSubscriptionReturnType(), javaTypeName);
}
return javaTypeName;
}

}
Loading