未验证 提交 f0fc9665 编写于 作者: B Bogdan Kobylynskyi 提交者: GitHub

Consider case when both Optional & apiReturnType is used #410 (#414)

上级 ed0c7422
......@@ -70,7 +70,7 @@ public class FieldDefinitionToParameterMapper {
ParameterDefinition parameter = new ParameterDefinition();
parameter.setName(MapperUtils.capitalizeIfRestricted(mappingContext, fieldDef.getName()));
parameter.setOriginalName(fieldDef.getName());
parameter.setType(GraphqlTypeToJavaTypeMapper.getTypeConsideringPrimitive(mappingContext, namedDefinition));
parameter.setType(GraphqlTypeToJavaTypeMapper.getTypeConsideringPrimitive(mappingContext, namedDefinition, namedDefinition.getJavaName()));
parameter.setAnnotations(GraphqlTypeToJavaTypeMapper.getAnnotations(mappingContext, fieldDef.getType(), fieldDef, parentTypeName, false));
parameter.setJavaDoc(fieldDef.getJavaDoc());
parameter.setDeprecated(fieldDef.isDeprecated());
......
......@@ -22,7 +22,14 @@ import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
import static com.kobylynskyi.graphql.codegen.model.DataModelFields.*;
import static com.kobylynskyi.graphql.codegen.model.DataModelFields.CLASS_NAME;
import static com.kobylynskyi.graphql.codegen.model.DataModelFields.ENUM_IMPORT_IT_SELF_IN_SCALA;
import static com.kobylynskyi.graphql.codegen.model.DataModelFields.GENERATED_INFO;
import static com.kobylynskyi.graphql.codegen.model.DataModelFields.IMPLEMENTS;
import static com.kobylynskyi.graphql.codegen.model.DataModelFields.IMPORTS;
import static com.kobylynskyi.graphql.codegen.model.DataModelFields.JAVA_DOC;
import static com.kobylynskyi.graphql.codegen.model.DataModelFields.OPERATIONS;
import static com.kobylynskyi.graphql.codegen.model.DataModelFields.PACKAGE;
import static com.kobylynskyi.graphql.codegen.model.MappingConfigConstants.PARENT_INTERFACE_TYPE_PLACEHOLDER;
import static java.util.Collections.emptyList;
import static java.util.Collections.singletonList;
......@@ -215,7 +222,7 @@ public class FieldDefinitionsToResolverDataModelMapper {
}
}
}
return GraphqlTypeToJavaTypeMapper.wrapApiReturnTypeIfRequired(mappingContext, fieldDef, namedDefinition, parentTypeName);
return GraphqlTypeToJavaTypeMapper.wrapApiReturnTypeIfRequired(mappingContext, namedDefinition, parentTypeName);
}
}
......@@ -4,7 +4,6 @@ import com.kobylynskyi.graphql.codegen.model.GeneratedLanguage;
import com.kobylynskyi.graphql.codegen.model.MappingContext;
import com.kobylynskyi.graphql.codegen.model.NamedDefinition;
import com.kobylynskyi.graphql.codegen.model.definitions.ExtendedDefinition;
import com.kobylynskyi.graphql.codegen.model.definitions.ExtendedFieldDefinition;
import com.kobylynskyi.graphql.codegen.model.graphql.GraphQLOperation;
import com.kobylynskyi.graphql.codegen.utils.Utils;
import graphql.language.Argument;
......@@ -296,67 +295,68 @@ public class GraphqlTypeToJavaTypeMapper {
* @return Java/Scala type wrapped into the subscriptionReturnType
*/
static String wrapApiReturnTypeIfRequired(MappingContext mappingContext,
ExtendedFieldDefinition fieldDef,
NamedDefinition namedDefinition,
String parentTypeName) {
String javaTypeName = namedDefinition.getJavaName();
if (parentTypeName.equalsIgnoreCase(GraphQLOperation.SUBSCRIPTION.name())) {
if (Utils.isNotBlank(mappingContext.getSubscriptionReturnType())) {
// in case it is subscription and subscriptionReturnType is set
return getGenericsString(mappingContext, mappingContext.getSubscriptionReturnType(), javaTypeName);
String computedTypeName = namedDefinition.getJavaName();
if (parentTypeName.equalsIgnoreCase(GraphQLOperation.SUBSCRIPTION.name()) &&
Utils.isNotBlank(mappingContext.getSubscriptionReturnType())) {
// in case it is subscription and subscriptionReturnType is set
return getGenericsString(mappingContext, mappingContext.getSubscriptionReturnType(), computedTypeName);
}
if (Boolean.TRUE.equals(mappingContext.getUseOptionalForNullableReturnTypes()) && !namedDefinition.isMandatory()) {
if (GeneratedLanguage.SCALA.equals(mappingContext.getGeneratedLanguage()) &&
!computedTypeName.startsWith(SCALA_UTIL_LIST) && !computedTypeName.startsWith(JAVA_UTIL_LIST)) {
// wrap the type into java.util.Optional (except java list and scala list)
computedTypeName = getGenericsString(mappingContext, SCALA_UTIL_OPTIONAL, computedTypeName);
} else if (!computedTypeName.startsWith(JAVA_UTIL_LIST)) {
// wrap the type into java.util.Optional (except lists)
computedTypeName = getGenericsString(mappingContext, JAVA_UTIL_OPTIONAL, computedTypeName);
}
} else if (Boolean.TRUE.equals(mappingContext.getUseOptionalForNullableReturnTypes())) {
// wrap the type into java.util.Optional (except lists)
if (!namedDefinition.isMandatory() && !javaTypeName.startsWith(JAVA_UTIL_LIST)) {
return getGenericsString(mappingContext, JAVA_UTIL_OPTIONAL, javaTypeName);
}
// scala
if (GeneratedLanguage.SCALA.equals(mappingContext.getGeneratedLanguage())) {
if (computedTypeName.startsWith(SCALA_UTIL_LIST) &&
Utils.isNotBlank(mappingContext.getApiReturnListType())) {
// in case it is query/mutation, return type is list and apiReturnListType is set
return computedTypeName.replace(SCALA_UTIL_LIST, mappingContext.getApiReturnListType());
}
// wrap the type into java.util.Optional (except java list and scala list)
if (!namedDefinition.isMandatory() && !javaTypeName.startsWith(SCALA_UTIL_LIST) && !javaTypeName.startsWith(JAVA_UTIL_LIST)) {
return getGenericsString(mappingContext, SCALA_UTIL_OPTIONAL, javaTypeName);
if (Utils.isNotBlank(mappingContext.getApiReturnType())) {
// in case it is query/mutation and apiReturnType is set
return getGenericsString(mappingContext, mappingContext.getApiReturnType(), computedTypeName);
}
} else {
// scala
if (GeneratedLanguage.SCALA.equals(mappingContext.getGeneratedLanguage())) {
if (javaTypeName.startsWith(SCALA_UTIL_LIST) &&
Utils.isNotBlank(mappingContext.getApiReturnListType())) {
// in case it is query/mutation, return type is list and apiReturnListType is set
return javaTypeName.replace(SCALA_UTIL_LIST, mappingContext.getApiReturnListType());
}
if (Utils.isNotBlank(mappingContext.getApiReturnType())) {
// in case it is query/mutation and apiReturnType is set
return getGenericsString(mappingContext, mappingContext.getApiReturnType(), javaTypeName);
}
}
if (javaTypeName.startsWith(JAVA_UTIL_LIST) &&
if (computedTypeName.startsWith(JAVA_UTIL_LIST) &&
Utils.isNotBlank(mappingContext.getApiReturnListType())) {
// in case it is query/mutation, return type is list and apiReturnListType is set
return javaTypeName.replace(JAVA_UTIL_LIST, mappingContext.getApiReturnListType());
return computedTypeName.replace(JAVA_UTIL_LIST, mappingContext.getApiReturnListType());
}
if (Utils.isNotBlank(mappingContext.getApiReturnType())) {
// in case it is query/mutation and apiReturnType is set
return getGenericsString(mappingContext, mappingContext.getApiReturnType(), javaTypeName);
return getGenericsString(mappingContext, mappingContext.getApiReturnType(), computedTypeName);
}
}
return GraphqlTypeToJavaTypeMapper.getTypeConsideringPrimitive(mappingContext, namedDefinition);
return GraphqlTypeToJavaTypeMapper.getTypeConsideringPrimitive(mappingContext, namedDefinition, computedTypeName);
}
public static String getTypeConsideringPrimitive(MappingContext mappingContext,
NamedDefinition namedDefinition) {
NamedDefinition namedDefinition,
String computedTypeName) {
String graphqlTypeName = namedDefinition.getGraphqlTypeName();
if (namedDefinition.isMandatory() && namedDefinition.isPrimitiveCanBeUsed()) {
String possiblyPrimitiveType = mappingContext.getCustomTypesMapping().get(getMandatoryType(graphqlTypeName));
if (GeneratedLanguage.JAVA.equals(mappingContext.getGeneratedLanguage())) {
if (isJavaPrimitive(possiblyPrimitiveType)) {
return possiblyPrimitiveType;
}
} else if (GeneratedLanguage.SCALA.equals(mappingContext.getGeneratedLanguage())) {
if (isScalaPrimitive(possiblyPrimitiveType)) {
return possiblyPrimitiveType;
}
if (isPrimitive(mappingContext, possiblyPrimitiveType)) {
return possiblyPrimitiveType;
}
//TODO kotlin
}
return namedDefinition.getJavaName();
return computedTypeName;
}
private static boolean isPrimitive(MappingContext mappingContext, String possiblyPrimitiveType) {
GeneratedLanguage generatedLanguage = mappingContext.getGeneratedLanguage();
// TODO kotlin
return GeneratedLanguage.JAVA.equals(generatedLanguage) && isJavaPrimitive(possiblyPrimitiveType)
|| GeneratedLanguage.SCALA.equals(generatedLanguage) && isScalaPrimitive(possiblyPrimitiveType);
}
public static boolean isJavaPrimitive(String javaType) {
......
......@@ -49,7 +49,7 @@ public class InputValueDefinitionToParameterMapper {
ParameterDefinition parameter = new ParameterDefinition();
parameter.setName(MapperUtils.capitalizeIfRestricted(mappingContext, inputValueDefinition.getName()));
parameter.setOriginalName(inputValueDefinition.getName());
parameter.setType(GraphqlTypeToJavaTypeMapper.getTypeConsideringPrimitive(mappingContext, namedDefinition));
parameter.setType(GraphqlTypeToJavaTypeMapper.getTypeConsideringPrimitive(mappingContext, namedDefinition, namedDefinition.getJavaName()));
parameter.setDefaultValue(ValueMapper.map(mappingContext, inputValueDefinition.getDefaultValue(), inputValueDefinition.getType()));
parameter.setAnnotations(GraphqlTypeToJavaTypeMapper.getAnnotations(mappingContext, inputValueDefinition.getType(), inputValueDefinition, parentTypeName, false));
parameter.setDeprecated(isDeprecated(inputValueDefinition));
......
......@@ -56,4 +56,19 @@ class GraphQLCodegenOptionalTest {
getFileByName(files, "QueryResolver.java"));
}
@Test
void generate_OptionalWithCustomApiReturnType() throws Exception {
mappingConfig.setApiReturnType("reactor.core.publisher.Mono");
mappingConfig.setApiReturnListType("reactor.core.publisher.Flux");
new GraphQLCodegen(schemaFinder.findSchemas(), outputBuildDir, mappingConfig, TestUtils.getStaticGeneratedInfo())
.generate();
File[] files = Objects.requireNonNull(outputJavaClassesDir.listFiles());
// node(id: ID!): Node
assertSameTrimmedContent(new File("src/test/resources/expected-classes/optional/NodeQueryResolver_mono.java.txt"),
getFileByName(files, "NodeQueryResolver.java"));
}
}
\ No newline at end of file
@javax.annotation.Generated(
value = "com.kobylynskyi.graphql.codegen.GraphQLCodegen",
date = "2020-12-31T23:59:59-0500"
)
public interface NodeQueryResolver {
reactor.core.publisher.Mono<java.util.Optional<Node>> node(String id) throws Exception;
}
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册