Skip to content

Commit ff14469

Browse files
committed
Add some javadocs, rearrange EnumDefinitionToDataModelMapper
1 parent 216d45b commit ff14469

File tree

3 files changed

+80
-39
lines changed

3 files changed

+80
-39
lines changed

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

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,13 @@
22

33
import com.kobylynskyi.graphql.codegen.model.MappingConfig;
44
import graphql.language.EnumTypeDefinition;
5+
import graphql.language.EnumValueDefinition;
56

7+
import java.util.Collections;
68
import java.util.HashMap;
9+
import java.util.List;
710
import java.util.Map;
11+
import java.util.stream.Collectors;
812

913
import static com.kobylynskyi.graphql.codegen.model.DataModelFields.*;
1014

@@ -28,8 +32,24 @@ public static Map<String, Object> map(MappingConfig mappingConfig, EnumTypeDefin
2832
dataModel.put(PACKAGE, packageName);
2933
dataModel.put(IMPORTS, MapperUtils.getImports(mappingConfig, packageName));
3034
dataModel.put(CLASS_NAME, MapperUtils.getClassNameWithPrefixAndSuffix(mappingConfig, enumDef));
31-
dataModel.put(FIELDS, EnumValueDefinitionToStringMapper.map(enumDef.getEnumValueDefinitions()));
35+
dataModel.put(FIELDS, map(enumDef.getEnumValueDefinitions()));
3236
return dataModel;
3337
}
3438

39+
/**
40+
* Mapper from GraphQL's EnumValueDefinition to a Freemarker-understandable format
41+
*
42+
* @param enumValueDefinitions list of GraphQL EnumValueDefinition types
43+
* @return list of strings
44+
*/
45+
private static List<String> map(List<EnumValueDefinition> enumValueDefinitions) {
46+
if (enumValueDefinitions == null) {
47+
return Collections.emptyList();
48+
}
49+
return enumValueDefinitions.stream()
50+
.map(EnumValueDefinition::getName)
51+
.map(MapperUtils::capitalizeIfRestricted)
52+
.collect(Collectors.toList());
53+
}
54+
3555
}

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

Lines changed: 0 additions & 29 deletions
This file was deleted.

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

Lines changed: 59 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,14 @@
1515
*/
1616
class GraphqlTypeToJavaTypeMapper {
1717

18+
/**
19+
* Map GraphQL's FieldDefinition to a Freemarker-understandable format of operation
20+
*
21+
* @param mappingConfig Global mapping configuration
22+
* @param fieldDef GraphQL field definition
23+
* @param parentTypeName Name of the parent type
24+
* @return Freemarker-understandable format of parameter (field)
25+
*/
1826
public static ParameterDefinition map(MappingConfig mappingConfig, FieldDefinition fieldDef, String parentTypeName) {
1927
ParameterDefinition parameter = new ParameterDefinition();
2028
parameter.setName(MapperUtils.capitalizeIfRestricted(fieldDef.getName()));
@@ -23,6 +31,14 @@ public static ParameterDefinition map(MappingConfig mappingConfig, FieldDefiniti
2331
return parameter;
2432
}
2533

34+
/**
35+
* Map GraphQL's InputValueDefinition to a Freemarker-understandable format of operation
36+
*
37+
* @param mappingConfig Global mapping configuration
38+
* @param inputValueDefinition GraphQL input value definition
39+
* @param parentTypeName Name of the parent type
40+
* @return Freemarker-understandable format of parameter (field)
41+
*/
2642
public static ParameterDefinition map(MappingConfig mappingConfig, InputValueDefinition inputValueDefinition, String parentTypeName) {
2743
ParameterDefinition parameter = new ParameterDefinition();
2844
parameter.setName(MapperUtils.capitalizeIfRestricted(inputValueDefinition.getName()));
@@ -31,22 +47,47 @@ public static ParameterDefinition map(MappingConfig mappingConfig, InputValueDef
3147
return parameter;
3248
}
3349

50+
/**
51+
* Convert GraphQL type to a corresponding Java type
52+
*
53+
* @param mappingConfig Global mapping configuration
54+
* @param type GraphQL type
55+
* @return Corresponding Java type
56+
*/
3457
static String getJavaType(MappingConfig mappingConfig, Type type) {
3558
return getJavaType(mappingConfig, type, null, null);
3659
}
3760

38-
static String getJavaType(MappingConfig mappingConfig, Type type, String name, String parentTypeName) {
39-
if (type instanceof TypeName) {
40-
return getJavaType(mappingConfig, ((TypeName) type).getName(), name, parentTypeName);
41-
} else if (type instanceof ListType) {
42-
String mappedCollectionType = getJavaType(mappingConfig, ((ListType) type).getType(), name, parentTypeName);
61+
/**
62+
* Convert GraphQL type to a corresponding Java type
63+
*
64+
* @param mappingConfig Global mapping configuration
65+
* @param graphlType GraphQL type
66+
* @param name GraphQL type name
67+
* @param parentTypeName Name of the parent type
68+
* @return Corresponding Java type
69+
*/
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);
4375
return wrapIntoJavaCollection(mappedCollectionType);
44-
} else if (type instanceof NonNullType) {
45-
return getJavaType(mappingConfig, ((NonNullType) type).getType(), name, parentTypeName);
76+
} else if (graphlType instanceof NonNullType) {
77+
return getJavaType(mappingConfig, ((NonNullType) graphlType).getType(), name, parentTypeName);
4678
}
4779
return null;
4880
}
4981

82+
/**
83+
* Convert GraphQL type to a corresponding Java type
84+
*
85+
* @param mappingConfig Global mapping configuration
86+
* @param graphlType GraphQL type
87+
* @param name GraphQL type name
88+
* @param parentTypeName Name of the parent type
89+
* @return Corresponding Java type
90+
*/
5091
private static String getJavaType(MappingConfig mappingConfig, String graphlType, String name, String parentTypeName) {
5192
Map<String, String> customTypesMapping = mappingConfig.getCustomTypesMapping();
5293
if (name != null && parentTypeName != null && customTypesMapping.containsKey(parentTypeName + "." + name)) {
@@ -69,8 +110,17 @@ private static String getJavaType(MappingConfig mappingConfig, String graphlType
69110
}
70111
}
71112

72-
static List<String> getAnnotations(MappingConfig mappingConfig, Type type, String name, String parentTypeName) {
73-
return getAnnotations(mappingConfig, type, name, parentTypeName, false);
113+
/**
114+
* Get annotations for a given GraphQL type
115+
*
116+
* @param mappingConfig Global mapping configuration
117+
* @param graphlType GraphQL type
118+
* @param name GraphQL type name
119+
* @param parentTypeName Name of the parent type
120+
* @return list of Java annotations for a given GraphQL type
121+
*/
122+
static List<String> getAnnotations(MappingConfig mappingConfig, Type graphlType, String name, String parentTypeName) {
123+
return getAnnotations(mappingConfig, graphlType, name, parentTypeName, false);
74124
}
75125

76126
private static List<String> getAnnotations(MappingConfig mappingConfig, Type type, String name, String parentTypeName,

0 commit comments

Comments
 (0)