|
| 1 | +package com.kobylynskyi.graphql.codegen.mapper; |
| 2 | + |
| 3 | +import com.kobylynskyi.graphql.codegen.model.MappingConfig; |
| 4 | +import com.kobylynskyi.graphql.codegen.model.OperationDefinition; |
| 5 | +import com.kobylynskyi.graphql.codegen.model.ParameterDefinition; |
| 6 | +import com.kobylynskyi.graphql.codegen.utils.Utils; |
| 7 | +import graphql.language.FieldDefinition; |
| 8 | +import graphql.language.ObjectTypeDefinition; |
| 9 | +import graphql.language.TypeName; |
| 10 | + |
| 11 | +import java.util.ArrayList; |
| 12 | +import java.util.Collections; |
| 13 | +import java.util.HashMap; |
| 14 | +import java.util.List; |
| 15 | +import java.util.Map; |
| 16 | +import java.util.Set; |
| 17 | +import java.util.stream.Collectors; |
| 18 | + |
| 19 | +import static com.kobylynskyi.graphql.codegen.model.DataModelFields.*; |
| 20 | +import static java.util.Collections.emptyList; |
| 21 | + |
| 22 | +/** |
| 23 | + * Map field definitions to a Freemarker data model representing a resolver for these fields. |
| 24 | + */ |
| 25 | +public class FieldDefinitionsToResolverDataModelMapper { |
| 26 | + |
| 27 | + /** |
| 28 | + * Map field definition to a Freemarker data model |
| 29 | + * |
| 30 | + * @param mappingConfig Global mapping configuration |
| 31 | + * @param fieldDefs GraphQL field definitions that require resolvers |
| 32 | + * @param parentTypeName Name of the type for which Resolver will be generated |
| 33 | + * @return Freemarker data model of the GraphQL parametrized field |
| 34 | + */ |
| 35 | + public static Map<String, Object> mapToTypeResolver(MappingConfig mappingConfig, List<FieldDefinition> fieldDefs, |
| 36 | + String parentTypeName) { |
| 37 | + // Example: PersonResolver |
| 38 | + String className = parentTypeName + "Resolver"; |
| 39 | + return mapToResolverModel(mappingConfig, parentTypeName, className, fieldDefs); |
| 40 | + } |
| 41 | + |
| 42 | + /** |
| 43 | + * Map field definition to a Freemarker data model |
| 44 | + * |
| 45 | + * @param mappingConfig Global mapping configuration |
| 46 | + * @param fieldDefinition GraphQL field definition |
| 47 | + * @param rootTypeName Object type (e.g.: "Query", "Mutation" or "Subscription") |
| 48 | + * @return Freemarker data model of the GraphQL field |
| 49 | + */ |
| 50 | + public static Map<String, Object> mapRootTypeField(MappingConfig mappingConfig, FieldDefinition fieldDefinition, |
| 51 | + String rootTypeName) { |
| 52 | + // Examples: VersionQuery, CreateEventMutation (rootTypeName is "Query" or the likes) |
| 53 | + String className = Utils.capitalize(fieldDefinition.getName()) + rootTypeName; |
| 54 | + List<FieldDefinition> fieldDefs = Collections.singletonList(fieldDefinition); |
| 55 | + return mapToResolverModel(mappingConfig, rootTypeName, className, fieldDefs); |
| 56 | + } |
| 57 | + |
| 58 | + /** |
| 59 | + * Map a root object type definition to a Freemarker data model for a resolver with all its fields. |
| 60 | + * |
| 61 | + * @param mappingConfig Global mapping configuration |
| 62 | + * @param rootTypeDefinition GraphQL object definition of a root type like Query |
| 63 | + * @return Freemarker data model of the GraphQL object |
| 64 | + */ |
| 65 | + public static Map<String, Object> mapRootTypeFields(MappingConfig mappingConfig, ObjectTypeDefinition rootTypeDefinition) { |
| 66 | + String parentTypeName = rootTypeDefinition.getName(); |
| 67 | + String className = Utils.capitalize(parentTypeName); |
| 68 | + // For root types like "Query", we create resolvers for all fields |
| 69 | + List<FieldDefinition> fieldDefinitions = rootTypeDefinition.getFieldDefinitions(); |
| 70 | + return mapToResolverModel(mappingConfig, parentTypeName, className, fieldDefinitions); |
| 71 | + } |
| 72 | + |
| 73 | + private static Map<String, Object> mapToResolverModel(MappingConfig mappingConfig, String parentTypeName, String className, |
| 74 | + List<FieldDefinition> fieldDefinitions) { |
| 75 | + String packageName = MapperUtils.getApiPackageName(mappingConfig); |
| 76 | + Set<String> imports = MapperUtils.getImportsForFieldResolvers(mappingConfig, packageName, parentTypeName); |
| 77 | + List<OperationDefinition> operations = mapToOperations(mappingConfig, fieldDefinitions, parentTypeName); |
| 78 | + |
| 79 | + Map<String, Object> dataModel = new HashMap<>(); |
| 80 | + dataModel.put(PACKAGE, packageName); |
| 81 | + dataModel.put(IMPORTS, imports); |
| 82 | + dataModel.put(CLASS_NAME, className); |
| 83 | + dataModel.put(OPERATIONS, operations); |
| 84 | + return dataModel; |
| 85 | + } |
| 86 | + |
| 87 | + /** |
| 88 | + * Builds a list of Freemarker-understandable structures representing operations to resolve the given fields |
| 89 | + * for a given parent type. |
| 90 | + * |
| 91 | + * @param mappingConfig Global mapping configuration |
| 92 | + * @param fieldDefinitions The GraphQL definition of the fields that the methods should resolve |
| 93 | + * @param parentTypeName Name of the parent type which the field belongs to |
| 94 | + * @return Freemarker-understandable format of operations |
| 95 | + */ |
| 96 | + private static List<OperationDefinition> mapToOperations(MappingConfig mappingConfig, List<FieldDefinition> fieldDefinitions, String parentTypeName) { |
| 97 | + return fieldDefinitions.stream() |
| 98 | + .map(fieldDef -> map(mappingConfig, fieldDef, parentTypeName)) |
| 99 | + .collect(Collectors.toList()); |
| 100 | + } |
| 101 | + |
| 102 | + /** |
| 103 | + * Builds a Freemarker-understandable structure representing an operation to resolve a field for a given parent type. |
| 104 | + * |
| 105 | + * @param mappingConfig Global mapping configuration |
| 106 | + * @param resolvedField The GraphQL definition of the field that the method should resolve |
| 107 | + * @param parentTypeName Name of the parent type which the field belongs to |
| 108 | + * @return Freemarker-understandable format of operation |
| 109 | + */ |
| 110 | + private static OperationDefinition map(MappingConfig mappingConfig, FieldDefinition resolvedField, String parentTypeName) { |
| 111 | + String javaType = GraphqlTypeToJavaTypeMapper.getJavaType(mappingConfig, resolvedField.getType(), resolvedField.getName(), parentTypeName); |
| 112 | + OperationDefinition operation = new OperationDefinition(); |
| 113 | + operation.setName(resolvedField.getName()); |
| 114 | + operation.setType(GraphqlTypeToJavaTypeMapper.wrapIntoAsyncIfRequired(mappingConfig, javaType, parentTypeName)); |
| 115 | + operation.setAnnotations(GraphqlTypeToJavaTypeMapper.getAnnotations(mappingConfig, resolvedField.getType(), resolvedField.getName(), parentTypeName)); |
| 116 | + operation.setParameters(getOperationParameters(mappingConfig, resolvedField, parentTypeName)); |
| 117 | + return operation; |
| 118 | + } |
| 119 | + |
| 120 | + private static List<ParameterDefinition> getOperationParameters(MappingConfig mappingConfig, FieldDefinition resolvedField, String parentTypeName) { |
| 121 | + List<ParameterDefinition> parameters = new ArrayList<>(); |
| 122 | + |
| 123 | + // 1. First parameter is the parent object for which we are resolving fields (unless it's the root Query) |
| 124 | + if (!Utils.isGraphqlOperation(parentTypeName)) { |
| 125 | + String parentObjectParamType = GraphqlTypeToJavaTypeMapper.getJavaType(mappingConfig, new TypeName(parentTypeName)); |
| 126 | + String parentObjectParamName = MapperUtils.capitalizeIfRestricted(Utils.uncapitalize(parentObjectParamType)); |
| 127 | + parameters.add(new ParameterDefinition(parentObjectParamType, parentObjectParamName, null, emptyList())); |
| 128 | + } |
| 129 | + |
| 130 | + // 2. Next parameters are input values |
| 131 | + parameters.addAll(InputValueDefinitionToParameterMapper.map(mappingConfig, resolvedField.getInputValueDefinitions(), resolvedField.getName())); |
| 132 | + |
| 133 | + // 3. Last parameter (optional) is the DataFetchingEnvironment |
| 134 | + if (mappingConfig.getGenerateDataFetchingEnvironmentArgumentInApis()) { |
| 135 | + parameters.add(ParameterDefinition.DATA_FETCHING_ENVIRONMENT); |
| 136 | + } |
| 137 | + return parameters; |
| 138 | + } |
| 139 | +} |
0 commit comments