|
1 | 1 | package com.kobylynskyi.graphql.codegen.mapper;
|
2 | 2 |
|
| 3 | +import static graphql.language.OperationDefinition.*; |
| 4 | + |
3 | 5 | import com.kobylynskyi.graphql.codegen.model.MappingConfig;
|
4 | 6 | import com.kobylynskyi.graphql.codegen.model.ParameterDefinition;
|
5 | 7 | import graphql.language.*;
|
|
16 | 18 | class GraphqlTypeToJavaTypeMapper {
|
17 | 19 |
|
18 | 20 | /**
|
19 |
| - * Map GraphQL's FieldDefinition to a Freemarker-understandable format of operation |
| 21 | + * Map GraphQL's FieldDefinition to a Freemarker-understandable format of parameter |
20 | 22 | *
|
21 | 23 | * @param mappingConfig Global mapping configuration
|
22 | 24 | * @param fieldDef GraphQL field definition
|
@@ -62,19 +64,19 @@ static String getJavaType(MappingConfig mappingConfig, Type type) {
|
62 | 64 | * Convert GraphQL type to a corresponding Java type
|
63 | 65 | *
|
64 | 66 | * @param mappingConfig Global mapping configuration
|
65 |
| - * @param graphlType GraphQL type |
| 67 | + * @param graphqlType GraphQL type |
66 | 68 | * @param name GraphQL type name
|
67 | 69 | * @param parentTypeName Name of the parent type
|
68 | 70 | * @return Corresponding Java type
|
69 | 71 | */
|
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); |
| 72 | + static String getJavaType(MappingConfig mappingConfig, Type graphqlType, String name, String parentTypeName) { |
| 73 | + if (graphqlType instanceof TypeName) { |
| 74 | + return getJavaType(mappingConfig, ((TypeName) graphqlType).getName(), name, parentTypeName); |
| 75 | + } else if (graphqlType instanceof ListType) { |
| 76 | + String mappedCollectionType = getJavaType(mappingConfig, ((ListType) graphqlType).getType(), name, parentTypeName); |
75 | 77 | return wrapIntoJavaCollection(mappedCollectionType);
|
76 |
| - } else if (graphlType instanceof NonNullType) { |
77 |
| - return getJavaType(mappingConfig, ((NonNullType) graphlType).getType(), name, parentTypeName); |
| 78 | + } else if (graphqlType instanceof NonNullType) { |
| 79 | + return getJavaType(mappingConfig, ((NonNullType) graphqlType).getType(), name, parentTypeName); |
78 | 80 | }
|
79 | 81 | return null;
|
80 | 82 | }
|
@@ -156,4 +158,13 @@ private static String wrapIntoJavaCollection(String type) {
|
156 | 158 | return String.format("Collection<%s>", type);
|
157 | 159 | }
|
158 | 160 |
|
| 161 | + static String wrapIntoSubscriptionIfRequired(MappingConfig mappingConfig, String javaTypeName, String parentTypeName) { |
| 162 | + if (parentTypeName.equalsIgnoreCase(Operation.SUBSCRIPTION.name()) |
| 163 | + && mappingConfig.getSubscriptionReturnType() != null |
| 164 | + && !mappingConfig.getSubscriptionReturnType().trim().isEmpty()) { |
| 165 | + return String.format("%s<%s>", mappingConfig.getSubscriptionReturnType(), javaTypeName); |
| 166 | + } |
| 167 | + return javaTypeName; |
| 168 | + } |
| 169 | + |
159 | 170 | }
|
0 commit comments