GraphqlTypeToJavaTypeMapper.java 5.1 KB
Newer Older
1 2 3
package com.kobylynskyi.graphql.codegen.mapper;

import com.kobylynskyi.graphql.codegen.model.MappingConfig;
4 5
import com.kobylynskyi.graphql.codegen.model.ParameterDefinition;
import graphql.language.*;
6

7 8
import java.util.ArrayList;
import java.util.List;
9 10 11 12 13 14 15 16 17
import java.util.Map;

/**
 * Map GraphQL type to Java type
 *
 * @author kobylynskyi
 */
class GraphqlTypeToJavaTypeMapper {

18 19 20 21
    public static ParameterDefinition map(MappingConfig mappingConfig, FieldDefinition fieldDef, String parentTypeName) {
        ParameterDefinition parameter = new ParameterDefinition();
        parameter.setName(MapperUtils.capitalizeIfRestricted(fieldDef.getName()));
        parameter.setType(getJavaType(mappingConfig, fieldDef.getType(), fieldDef.getName(), parentTypeName));
22
        parameter.setAnnotations(getAnnotations(mappingConfig, fieldDef.getType(), fieldDef.getName(), parentTypeName, false));
23
        return parameter;
24 25
    }

26 27 28 29 30 31 32 33 34 35 36 37
    public static ParameterDefinition map(MappingConfig mappingConfig, InputValueDefinition inputValueDefinition) {
        ParameterDefinition parameter = new ParameterDefinition();
        parameter.setName(MapperUtils.capitalizeIfRestricted(inputValueDefinition.getName()));
        parameter.setType(getJavaType(mappingConfig, inputValueDefinition.getType()));
        return parameter;
    }

    static String getJavaType(MappingConfig mappingConfig, Type type) {
        return getJavaType(mappingConfig, type, null, null);
    }

    static String getJavaType(MappingConfig mappingConfig, Type type, String name, String parentTypeName) {
38
        if (type instanceof TypeName) {
39
            return getJavaType(mappingConfig, ((TypeName) type).getName(), name, parentTypeName);
40
        } else if (type instanceof ListType) {
41
            String mappedCollectionType = getJavaType(mappingConfig, ((ListType) type).getType(), name, parentTypeName);
42
            return wrapIntoJavaCollection(mappedCollectionType);
43
        } else if (type instanceof NonNullType) {
44
            return getJavaType(mappingConfig, ((NonNullType) type).getType(), name, parentTypeName);
45 46 47 48
        }
        return null;
    }

49
    private static String getJavaType(MappingConfig mappingConfig, String graphlType, String name, String parentTypeName) {
50 51 52 53 54
        Map<String, String> customTypesMapping = mappingConfig.getCustomTypesMapping();
        if (name != null && parentTypeName != null && customTypesMapping.containsKey(parentTypeName + "." + name)) {
            return customTypesMapping.get(parentTypeName + "." + name);
        } else if (customTypesMapping.containsKey(graphlType)) {
            return customTypesMapping.get(graphlType);
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
        }
        switch (graphlType) {
            case "ID":
                return "String";
            case "Int":
                return "Integer";
            case "String":
            case "Float":
            case "Boolean":
                return graphlType;
            default:
                // We need to refer other custom types/interfaces/unions with prefix and suffix
                return MapperUtils.getClassNameWithPrefixAndSuffix(mappingConfig, graphlType);
        }
    }

71
    static List<String> getAnnotations(MappingConfig mappingConfig, Type type, String name, String parentTypeName) {
72 73 74 75 76
        return getAnnotations(mappingConfig, type, name, parentTypeName, true);
    }

    private static List<String> getAnnotations(MappingConfig mappingConfig, Type type, String name, String parentTypeName,
                                               boolean mandatory) {
77
        if (type instanceof TypeName) {
78
            return getAnnotations(mappingConfig, ((TypeName) type).getName(), name, parentTypeName, mandatory);
79
        } else if (type instanceof ListType) {
80
            return getAnnotations(mappingConfig, ((ListType) type).getType(), name, parentTypeName, mandatory);
81
        } else if (type instanceof NonNullType) {
82
            return getAnnotations(mappingConfig, ((NonNullType) type).getType(), name, parentTypeName, true);
83 84 85 86
        }
        return null;
    }

87
    private static List<String> getAnnotations(MappingConfig mappingConfig, String graphlType, String name, String parentTypeName, boolean mandatory) {
88
        List<String> annotations = new ArrayList<>();
89 90 91 92 93 94
        if (mandatory) {
            String modelValidationAnnotation = mappingConfig.getModelValidationAnnotation();
            if (modelValidationAnnotation != null && !modelValidationAnnotation.trim().isEmpty()) {
                annotations.add(modelValidationAnnotation);
            }
        }
95 96 97 98 99 100 101 102 103
        Map<String, String> customAnnotationsMapping = mappingConfig.getCustomAnnotationsMapping();
        if (name != null && parentTypeName != null && customAnnotationsMapping.containsKey(parentTypeName + "." + name)) {
            annotations.add(customAnnotationsMapping.get(parentTypeName + "." + name));
        } else if (customAnnotationsMapping.containsKey(graphlType)) {
            annotations.add(customAnnotationsMapping.get(graphlType));
        }
        return annotations;
    }

104 105 106 107
    private static String wrapIntoJavaCollection(String type) {
        return String.format("Collection<%s>", type);
    }

108
}