提交 7d3f79ba 编写于 作者: B Bogdan Kobylynskyi

Introduce apiNamePrefix and apiNameSuffix config options #136 (#166)

上级 25aa7108
......@@ -15,6 +15,8 @@
| `generateDataFetchingEnvironmentArgumentInApis` | Boolean | False | If true, then `graphql.schema.DataFetchingEnvironment env` will be added as a last argument to all methods of root type resolvers and field resolvers. |
| `generateEqualsAndHashCode` | Boolean | False | Specifies whether generated model classes should have equals and hashCode methods defined. |
| `generateToString` | Boolean | False | Specifies whether generated model classes should have toString method defined. |
| `apiNamePrefix` | String | Empty | Sets the prefix for GraphQL api classes (query, mutation, subscription). |
| `apiNameSuffix` | String | Resolver | Sets the suffix for GraphQL api classes (query, mutation, subscription). |
| `modelNamePrefix` | String | Empty | Sets the prefix for GraphQL model classes (type, input, interface, enum, union). |
| `modelNameSuffix` | String | Empty | Sets the suffix for GraphQL model classes (type, input, interface, enum, union). |
| `modelValidationAnnotation` | String | @javax.validation.<br>constraints.NotNull | Annotation for mandatory (NonNull) fields. Can be null/empty. |
......
package io.github.kobylynskyi.order.graphql.resolvers;
import graphql.kickstart.tools.GraphQLMutationResolver;
import io.github.kobylynskyi.order.graphql.api.Mutation;
import io.github.kobylynskyi.order.graphql.api.AddProductToOrderMutationResolver;
import io.github.kobylynskyi.order.graphql.api.CreateMutationResolver;
import io.github.kobylynskyi.order.graphql.mappers.OrderMapper;
import io.github.kobylynskyi.order.graphql.model.OrderTO;
import io.github.kobylynskyi.order.service.OrderService;
......@@ -9,7 +10,7 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component
public class MutationsResolver implements Mutation, GraphQLMutationResolver {
public class MutationsResolver implements CreateMutationResolver, AddProductToOrderMutationResolver, GraphQLMutationResolver {
@Autowired
private OrderService service;
......
package io.github.kobylynskyi.order.graphql.resolvers;
import graphql.kickstart.tools.GraphQLQueryResolver;
import io.github.kobylynskyi.order.graphql.api.Query;
import io.github.kobylynskyi.order.graphql.api.OrderByIdQueryResolver;
import io.github.kobylynskyi.order.graphql.api.OrdersQueryResolver;
import io.github.kobylynskyi.order.graphql.mappers.OrderMapper;
import io.github.kobylynskyi.order.graphql.model.OrderTO;
import io.github.kobylynskyi.order.model.OrderNotFoundException;
......@@ -14,7 +15,7 @@ import java.util.Collection;
import static java.util.stream.Collectors.toList;
@Component
public class QueriesResolver implements Query, GraphQLQueryResolver {
public class QueriesResolver implements OrdersQueryResolver, OrderByIdQueryResolver, GraphQLQueryResolver {
@Autowired
private OrderService service;
......
package io.github.kobylynskyi.product.graphql.resolvers;
import io.github.kobylynskyi.product.graphql.api.Mutation;
import io.github.kobylynskyi.product.graphql.api.CreateMutationResolver;
import io.github.kobylynskyi.product.graphql.mappers.ProductMapper;
import io.github.kobylynskyi.product.graphql.model.ProductInputTO;
import io.github.kobylynskyi.product.graphql.model.ProductTO;
......@@ -10,7 +10,7 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component
public class MutationsResolver implements Mutation {
public class MutationsResolver implements CreateMutationResolver {
@Autowired
private ProductService service;
......
package io.github.kobylynskyi.product.graphql.resolvers;
import io.github.kobylynskyi.product.graphql.api.Query;
import io.github.kobylynskyi.product.graphql.api.ProductByIdQueryResolver;
import io.github.kobylynskyi.product.graphql.api.ProductsByIdsQueryResolver;
import io.github.kobylynskyi.product.graphql.api.ProductsQueryResolver;
import io.github.kobylynskyi.product.graphql.mappers.ProductMapper;
import io.github.kobylynskyi.product.graphql.model.ProductTO;
import io.github.kobylynskyi.product.service.ProductService;
......@@ -12,7 +14,7 @@ import java.util.Collection;
import static java.util.stream.Collectors.toList;
@Component
public class QueriesResolver implements Query {
public class QueriesResolver implements ProductsQueryResolver, ProductsByIdsQueryResolver, ProductByIdQueryResolver {
@Autowired
private ProductService service;
......
......@@ -33,6 +33,8 @@ public class GraphQLCodegenGradleTask extends DefaultTask implements GraphQLCode
private Map<String, String> customAnnotationsMapping = new HashMap<>();
private String packageName;
private String apiPackageName;
private String apiNamePrefix;
private String apiNameSuffix;
private String modelPackageName;
private String modelNamePrefix;
private String modelNameSuffix;
......@@ -73,6 +75,8 @@ public class GraphQLCodegenGradleTask extends DefaultTask implements GraphQLCode
MappingConfig mappingConfig = new MappingConfig();
mappingConfig.setPackageName(packageName);
mappingConfig.setCustomTypesMapping(customTypesMapping);
mappingConfig.setApiNameSuffix(apiNameSuffix);
mappingConfig.setApiNamePrefix(apiNamePrefix);
mappingConfig.setModelNamePrefix(modelNamePrefix);
mappingConfig.setModelNameSuffix(modelNameSuffix);
mappingConfig.setApiPackageName(apiPackageName);
......@@ -232,6 +236,28 @@ public class GraphQLCodegenGradleTask extends DefaultTask implements GraphQLCode
this.apiPackageName = apiPackageName;
}
@Input
@Optional
@Override
public String getApiNamePrefix() {
return apiNamePrefix;
}
public void setApiNamePrefix(String apiNamePrefix) {
this.apiNamePrefix = apiNamePrefix;
}
@Input
@Optional
@Override
public String getApiNameSuffix() {
return apiNameSuffix;
}
public void setApiNameSuffix(String apiNameSuffix) {
this.apiNameSuffix = apiNameSuffix;
}
@Input
@Optional
@Override
......
package io.github.kobylynskyi.order.graphql.resolvers;
import graphql.kickstart.tools.GraphQLMutationResolver;
import io.github.kobylynskyi.order.graphql.api.Mutation;
import io.github.kobylynskyi.order.graphql.api.AddProductToOrderMutationResolver;
import io.github.kobylynskyi.order.graphql.api.CreateMutationResolver;
import io.github.kobylynskyi.order.graphql.mappers.OrderMapper;
import io.github.kobylynskyi.order.graphql.model.OrderTO;
import io.github.kobylynskyi.order.service.OrderService;
......@@ -9,7 +10,7 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component
public class MutationsResolver implements Mutation, GraphQLMutationResolver {
public class MutationsResolver implements CreateMutationResolver, AddProductToOrderMutationResolver, GraphQLMutationResolver {
@Autowired
private OrderService service;
......
package io.github.kobylynskyi.order.graphql.resolvers;
import graphql.kickstart.tools.GraphQLQueryResolver;
import io.github.kobylynskyi.order.graphql.api.Query;
import io.github.kobylynskyi.order.graphql.api.OrderByIdQueryResolver;
import io.github.kobylynskyi.order.graphql.api.OrdersQueryResolver;
import io.github.kobylynskyi.order.graphql.mappers.OrderMapper;
import io.github.kobylynskyi.order.graphql.model.OrderTO;
import io.github.kobylynskyi.order.model.OrderNotFoundException;
......@@ -14,7 +15,7 @@ import java.util.Collection;
import static java.util.stream.Collectors.toList;
@Component
public class QueriesResolver implements Query, GraphQLQueryResolver {
public class QueriesResolver implements OrdersQueryResolver, OrderByIdQueryResolver, GraphQLQueryResolver {
@Autowired
private OrderService service;
......
package io.github.kobylynskyi.product.graphql.resolvers;
import io.github.kobylynskyi.product.graphql.api.Mutation;
import io.github.kobylynskyi.product.graphql.api.CreateMutationResolver;
import io.github.kobylynskyi.product.graphql.mappers.ProductMapper;
import io.github.kobylynskyi.product.graphql.model.ProductInputTO;
import io.github.kobylynskyi.product.graphql.model.ProductTO;
......@@ -10,7 +10,7 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component
public class MutationsResolver implements Mutation {
public class MutationsResolver implements CreateMutationResolver {
@Autowired
private ProductService service;
......
package io.github.kobylynskyi.product.graphql.resolvers;
import io.github.kobylynskyi.product.graphql.api.Query;
import io.github.kobylynskyi.product.graphql.api.ProductByIdQueryResolver;
import io.github.kobylynskyi.product.graphql.api.ProductsByIdsQueryResolver;
import io.github.kobylynskyi.product.graphql.api.ProductsQueryResolver;
import io.github.kobylynskyi.product.graphql.mappers.ProductMapper;
import io.github.kobylynskyi.product.graphql.model.ProductTO;
import io.github.kobylynskyi.product.service.ProductService;
......@@ -12,7 +14,7 @@ import java.util.Collection;
import static java.util.stream.Collectors.toList;
@Component
public class QueriesResolver implements Query {
public class QueriesResolver implements ProductsQueryResolver, ProductsByIdsQueryResolver, ProductByIdQueryResolver {
@Autowired
private ProductService service;
......
......@@ -63,6 +63,12 @@ public class GraphQLCodegenMojo extends AbstractMojo implements GraphQLCodegenCo
@Parameter
private String apiPackageName;
@Parameter
private String apiNamePrefix;
@Parameter(defaultValue = MappingConfigConstants.DEFAULT_API_NAME_SUFFIX)
private String apiNameSuffix;
@Parameter
private String modelPackageName;
......@@ -138,6 +144,8 @@ public class GraphQLCodegenMojo extends AbstractMojo implements GraphQLCodegenCo
MappingConfig mappingConfig = new MappingConfig();
mappingConfig.setPackageName(packageName);
mappingConfig.setCustomTypesMapping(customTypesMapping != null ? customTypesMapping : new HashMap<>());
mappingConfig.setApiNameSuffix(apiNameSuffix);
mappingConfig.setApiNamePrefix(apiNamePrefix);
mappingConfig.setModelNamePrefix(modelNamePrefix);
mappingConfig.setModelNameSuffix(modelNameSuffix);
mappingConfig.setApiPackageName(apiPackageName);
......@@ -264,6 +272,24 @@ public class GraphQLCodegenMojo extends AbstractMojo implements GraphQLCodegenCo
this.apiPackageName = apiPackageName;
}
@Override
public String getApiNamePrefix() {
return apiNamePrefix;
}
public void setApiNamePrefix(String apiNamePrefix) {
this.apiNamePrefix = apiNamePrefix;
}
@Override
public String getApiNameSuffix() {
return apiNameSuffix;
}
public void setApiNameSuffix(String apiNameSuffix) {
this.apiNameSuffix = apiNameSuffix;
}
@Override
public String getModelPackageName() {
return modelPackageName;
......
......@@ -96,6 +96,9 @@ public class GraphQLCodegen {
if (mappingConfig.getGenerateApis() == null) {
mappingConfig.setGenerateApis(MappingConfigConstants.DEFAULT_GENERATE_APIS);
}
if (mappingConfig.getApiNameSuffix() == null) {
mappingConfig.setApiNameSuffix(MappingConfigConstants.DEFAULT_API_NAME_SUFFIX);
}
if (mappingConfig.getGenerateAsyncApi() == null) {
mappingConfig.setGenerateAsyncApi(MappingConfigConstants.DEFAULT_GENERATE_ASYNC_APIS);
}
......
......@@ -60,7 +60,7 @@ public class DefaultValueMapper {
private static String mapEnum(MappingContext mappingContext, Type<?> graphQLType, EnumValue defaultValue) {
if (graphQLType instanceof TypeName) {
String typeName = ((TypeName) graphQLType).getName();
typeName = MapperUtils.getClassNameWithPrefixAndSuffix(mappingContext, typeName);
typeName = MapperUtils.getModelClassNameWithPrefixAndSuffix(mappingContext, typeName);
return typeName + "." + defaultValue.getName();
}
if (graphQLType instanceof NonNullType) {
......
......@@ -40,7 +40,7 @@ public class EnumDefinitionToDataModelMapper {
Map<String, Object> dataModel = new HashMap<>();
// type/enum/input/interface/union classes do not require any imports
dataModel.put(PACKAGE, MapperUtils.getModelPackageName(mappingContext));
dataModel.put(CLASS_NAME, MapperUtils.getClassNameWithPrefixAndSuffix(mappingContext, definition));
dataModel.put(CLASS_NAME, MapperUtils.getModelClassNameWithPrefixAndSuffix(mappingContext, definition));
dataModel.put(IMPLEMENTS, getUnionInterfaces(mappingContext, definition));
dataModel.put(JAVA_DOC, definition.getJavaDoc());
dataModel.put(FIELDS, map(definition.getValueDefinitions()));
......@@ -53,7 +53,7 @@ public class EnumDefinitionToDataModelMapper {
.stream()
.filter(union -> union.isDefinitionPartOfUnion(definition))
.map(ExtendedUnionTypeDefinition::getName)
.map(unionName -> MapperUtils.getClassNameWithPrefixAndSuffix(mappingContext, unionName))
.map(unionName -> MapperUtils.getModelClassNameWithPrefixAndSuffix(mappingContext, unionName))
.collect(Collectors.toSet());
}
......
......@@ -9,10 +9,20 @@ import com.kobylynskyi.graphql.codegen.model.graphql.GraphQLOperation;
import com.kobylynskyi.graphql.codegen.utils.Utils;
import graphql.language.TypeName;
import java.util.*;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
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.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;
......@@ -53,13 +63,7 @@ public class FieldDefinitionsToResolverDataModelMapper {
ExtendedFieldDefinition fieldDefinition,
String rootTypeName,
List<String> fieldNames) {
String fieldDefinitionName = fieldDefinition.getName();
if (Collections.frequency(fieldNames, fieldDefinitionName) > 1) {
// Examples: EventsByIdsQuery, EventsByCategoryAndStatusQuery
fieldDefinitionName += MapperUtils.getClassNameSuffixWithInputValues(fieldDefinition);
}
// Examples: CreateEventMutation, EventsQuery, EventsByIdsQuery (rootTypeName is "Query" or the likes)
String className = Utils.capitalize(fieldDefinitionName) + rootTypeName;
String className = MapperUtils.getApiClassNameWithPrefixAndSuffix(mappingContext, fieldDefinition, rootTypeName, fieldNames);
List<ExtendedFieldDefinition> fieldDefs = Collections.singletonList(fieldDefinition);
return mapToResolverModel(mappingContext, rootTypeName, className, fieldDefs, fieldDefinition.getJavaDoc(),
getParentInterface(mappingContext, rootTypeName));
......@@ -74,12 +78,11 @@ public class FieldDefinitionsToResolverDataModelMapper {
*/
public static Map<String, Object> mapRootTypeFields(MappingContext mappingContext,
ExtendedObjectTypeDefinition definition) {
String parentTypeName = definition.getName();
String className = Utils.capitalize(parentTypeName);
String className = MapperUtils.getApiClassNameWithPrefixAndSuffix(mappingContext, definition);
// For root types like "Query", we create resolvers for all fields
return mapToResolverModel(mappingContext, parentTypeName, className,
return mapToResolverModel(mappingContext, definition.getName(), className,
definition.getFieldDefinitions(), definition.getJavaDoc(),
getParentInterface(mappingContext, parentTypeName));
getParentInterface(mappingContext, definition.getName()));
}
private static Map<String, Object> mapToResolverModel(MappingContext mappingContext, String parentTypeName,
......@@ -183,7 +186,7 @@ public class FieldDefinitionsToResolverDataModelMapper {
}
return mappingContext.getResolverParentInterface()
.replace(PARENT_INTERFACE_TYPE_PLACEHOLDER,
MapperUtils.getClassNameWithPrefixAndSuffix(mappingContext, typeName));
MapperUtils.getModelClassNameWithPrefixAndSuffix(mappingContext, typeName));
}
}
......@@ -100,7 +100,7 @@ class GraphqlTypeToJavaTypeMapper {
} else if (customTypesMapping.containsKey(graphQLType)) {
javaTypeName = customTypesMapping.get(graphQLType);
} else {
javaTypeName = MapperUtils.getClassNameWithPrefixAndSuffix(mappingContext, graphQLType);
javaTypeName = MapperUtils.getModelClassNameWithPrefixAndSuffix(mappingContext, graphQLType);
}
return new NamedDefinition(javaTypeName, mappingContext.getInterfaceNames().contains(graphQLType));
}
......
......@@ -26,7 +26,7 @@ public class InputDefinitionToDataModelMapper {
Map<String, Object> dataModel = new HashMap<>();
// type/enum/input/interface/union classes do not require any imports
dataModel.put(PACKAGE, MapperUtils.getModelPackageName(mappingContext));
dataModel.put(CLASS_NAME, MapperUtils.getClassNameWithPrefixAndSuffix(mappingContext, definition));
dataModel.put(CLASS_NAME, MapperUtils.getModelClassNameWithPrefixAndSuffix(mappingContext, definition));
dataModel.put(JAVA_DOC, definition.getJavaDoc());
dataModel.put(NAME, definition.getName());
dataModel.put(FIELDS, InputValueDefinitionToParameterMapper.map(mappingContext, definition.getValueDefinitions(), definition.getName()));
......
......@@ -26,7 +26,7 @@ public class InterfaceDefinitionToDataModelMapper {
Map<String, Object> dataModel = new HashMap<>();
// type/enum/input/interface/union classes do not require any imports
dataModel.put(PACKAGE, MapperUtils.getModelPackageName(mappingContext));
dataModel.put(CLASS_NAME, MapperUtils.getClassNameWithPrefixAndSuffix(mappingContext, definition));
dataModel.put(CLASS_NAME, MapperUtils.getModelClassNameWithPrefixAndSuffix(mappingContext, definition));
dataModel.put(JAVA_DOC, definition.getJavaDoc());
dataModel.put(FIELDS, FieldDefinitionToParameterMapper.mapFields(
mappingContext, definition.getFieldDefinitions(), definition.getName()));
......
......@@ -45,25 +45,25 @@ class MapperUtils {
}
/**
* Generates a class name including prefix and suffix (if any)
* Generates a model class name including prefix and suffix (if any)
*
* @param mappingContext Global mapping context
* @param extendedDefinition GraphQL extended definition
* @return Class name of GraphQL node
* @return Class name of GraphQL model node
*/
static String getClassNameWithPrefixAndSuffix(MappingContext mappingContext,
ExtendedDefinition<?, ?> extendedDefinition) {
return getClassNameWithPrefixAndSuffix(mappingContext, extendedDefinition.getName());
static String getModelClassNameWithPrefixAndSuffix(MappingContext mappingContext,
ExtendedDefinition<?, ?> extendedDefinition) {
return getModelClassNameWithPrefixAndSuffix(mappingContext, extendedDefinition.getName());
}
/**
* Generates a class name including prefix and suffix (if any)
* Generates a model class name including prefix and suffix (if any)
*
* @param mappingContext Global mapping context
* @param definitionName GraphQL node name
* @return Class name of GraphQL node
* @return Class name of GraphQL model node
*/
static String getClassNameWithPrefixAndSuffix(MappingContext mappingContext, String definitionName) {
static String getModelClassNameWithPrefixAndSuffix(MappingContext mappingContext, String definitionName) {
StringBuilder classNameBuilder = new StringBuilder();
if (Utils.isNotBlank(mappingContext.getModelNamePrefix())) {
classNameBuilder.append(mappingContext.getModelNamePrefix());
......@@ -75,6 +75,59 @@ class MapperUtils {
return classNameBuilder.toString();
}
/**
* Generates an api class name including prefix and suffix (if any)
* Examples: CreateEventMutationResolver, EventsQueryResolver, EventsByIdsQueryResolver (rootTypeName is "Query" or the likes)
*
* @param mappingContext Global mapping context
* @param fieldDefinition GraphQL field definition
* @param rootTypeName Object type (e.g.: "Query", "Mutation" or "Subscription")
* @param fieldNames Names of all fields inside the rootType. Used to detect duplicate
* @return Class name of GraphQL api node
*/
static String getApiClassNameWithPrefixAndSuffix(MappingContext mappingContext,
ExtendedFieldDefinition fieldDefinition,
String rootTypeName,
List<String> fieldNames) {
StringBuilder classNameBuilder = new StringBuilder();
if (Utils.isNotBlank(mappingContext.getApiNamePrefix())) {
classNameBuilder.append(mappingContext.getApiNamePrefix());
}
classNameBuilder.append(Utils.capitalize(fieldDefinition.getName()));
if (Collections.frequency(fieldNames, fieldDefinition.getName()) > 1) {
// Examples: EventsByIdsQuery, EventsByCategoryAndStatusQuery
classNameBuilder.append(MapperUtils.getClassNameSuffixWithInputValues(fieldDefinition));
}
if (Utils.isNotBlank(rootTypeName)) {
classNameBuilder.append(rootTypeName);
}
if (Utils.isNotBlank(mappingContext.getApiNameSuffix())) {
classNameBuilder.append(mappingContext.getApiNameSuffix());
}
return classNameBuilder.toString();
}
/**
* Generates an api class name including prefix and suffix (if any)
* Examples: MutationResolver, QueryResolver, etc
*
* @param mappingContext Global mapping context
* @param definition GraphQL object definition of a root type like Query
* @return Class name of GraphQL api node
*/
static String getApiClassNameWithPrefixAndSuffix(MappingContext mappingContext,
ExtendedObjectTypeDefinition definition) {
StringBuilder classNameBuilder = new StringBuilder();
if (Utils.isNotBlank(mappingContext.getApiNamePrefix())) {
classNameBuilder.append(mappingContext.getApiNamePrefix());
}
classNameBuilder.append(Utils.capitalize(definition.getName()));
if (Utils.isNotBlank(mappingContext.getApiNameSuffix())) {
classNameBuilder.append(mappingContext.getApiNameSuffix());
}
return classNameBuilder.toString();
}
/**
* Generates a class name for ParametrizedInput
*
......
......@@ -47,7 +47,7 @@ public class TypeDefinitionToDataModelMapper {
Map<String, Object> dataModel = new HashMap<>();
// type/enum/input/interface/union classes do not require any imports
dataModel.put(PACKAGE, MapperUtils.getModelPackageName(mappingContext));
dataModel.put(CLASS_NAME, MapperUtils.getClassNameWithPrefixAndSuffix(mappingContext, definition));
dataModel.put(CLASS_NAME, MapperUtils.getModelClassNameWithPrefixAndSuffix(mappingContext, definition));
dataModel.put(JAVA_DOC, definition.getJavaDoc());
dataModel.put(IMPLEMENTS, getInterfaces(mappingContext, definition));
dataModel.put(FIELDS, getFields(mappingContext, definition, document));
......@@ -107,7 +107,7 @@ public class TypeDefinitionToDataModelMapper {
.stream()
.filter(union -> union.isDefinitionPartOfUnion(definition))
.map(ExtendedUnionTypeDefinition::getName)
.map(unionName -> MapperUtils.getClassNameWithPrefixAndSuffix(mappingContext, unionName))
.map(unionName -> MapperUtils.getModelClassNameWithPrefixAndSuffix(mappingContext, unionName))
.collect(Collectors.toList());
Set<String> interfaceNames = definition.getImplements()
.stream()
......
......@@ -26,7 +26,7 @@ public class UnionDefinitionToDataModelMapper {
Map<String, Object> dataModel = new HashMap<>();
// type/enum/input/interface/union classes do not require any imports
dataModel.put(PACKAGE, MapperUtils.getModelPackageName(mappingContext));
dataModel.put(CLASS_NAME, MapperUtils.getClassNameWithPrefixAndSuffix(mappingContext, definition));
dataModel.put(CLASS_NAME, MapperUtils.getModelClassNameWithPrefixAndSuffix(mappingContext, definition));
dataModel.put(JAVA_DOC, definition.getJavaDoc());
return dataModel;
}
......
......@@ -73,10 +73,24 @@ public interface GraphQLCodegenConfiguration {
/**
* Sets the suffix for GraphQL model classes (type, input, interface, enum, union).
*
* @return The prefix for GraphQL model classes.
* @return The suffix for GraphQL model classes.
*/
String getModelNameSuffix();
/**
* Sets the prefix for GraphQL api classes (query, mutation, subscription).
*
* @return The prefix for GraphQL api classes.
*/
String getApiNamePrefix();
/**
* Sets the suffix for GraphQL api classes (query, mutation, subscription).
*
* @return The suffix for GraphQL api classes.
*/
String getApiNameSuffix();
/**
* Annotation for mandatory (NonNull) fields. Can be null/empty.
*
......
......@@ -24,6 +24,8 @@ public class MappingConfig implements GraphQLCodegenConfiguration, Combinable<Ma
private String modelPackageName;
private String modelNamePrefix;
private String modelNameSuffix;
private String apiNamePrefix;
private String apiNameSuffix;
private String modelValidationAnnotation;
private String subscriptionReturnType;
private Boolean generateBuilder;
......@@ -68,6 +70,8 @@ public class MappingConfig implements GraphQLCodegenConfiguration, Combinable<Ma
this.modelPackageName = source.modelPackageName != null ? source.modelPackageName : this.modelPackageName;
this.modelNamePrefix = source.modelNamePrefix != null ? source.modelNamePrefix : this.modelNamePrefix;
this.modelNameSuffix = source.modelNameSuffix != null ? source.modelNameSuffix : this.modelNameSuffix;
this.apiNamePrefix = source.apiNamePrefix != null ? source.apiNamePrefix : this.apiNamePrefix;
this.apiNameSuffix = source.apiNameSuffix != null ? source.apiNameSuffix : this.apiNameSuffix;
this.modelValidationAnnotation = source.modelValidationAnnotation != null ? source.modelValidationAnnotation : this.modelValidationAnnotation;
this.subscriptionReturnType = source.subscriptionReturnType != null ? source.subscriptionReturnType : this.subscriptionReturnType;
this.generateBuilder = source.generateBuilder != null ? source.generateBuilder : this.generateBuilder;
......
......@@ -6,6 +6,7 @@ public class MappingConfigConstants {
public static final String PARENT_INTERFACE_TYPE_PLACEHOLDER = "{{TYPE}}";
public static final boolean DEFAULT_GENERATE_APIS = true;
public static final String DEFAULT_GENERATE_APIS_STRING = "true";
public static final String DEFAULT_API_NAME_SUFFIX = "Resolver";
public static final boolean DEFAULT_GENERATE_ASYNC_APIS = false;
public static final String DEFAULT_GENERATE_ASYNC_APIS_STRING = "false";
public static final boolean DEFAULT_BUILDER = true;
......
......@@ -54,6 +54,16 @@ public class MappingContext implements GraphQLCodegenConfiguration {
return config.getModelNameSuffix();
}
@Override
public String getApiNamePrefix() {
return config.getApiNamePrefix();
}
@Override
public String getApiNameSuffix() {
return config.getApiNameSuffix();
}
@Override
public String getModelValidationAnnotation() {
return config.getModelValidationAnnotation();
......
......@@ -43,9 +43,9 @@ class GraphQLCodegenExtendTest {
File[] files = Objects.requireNonNull(outputJavaClassesDir.listFiles());
Set<String> generatedFileNames = Arrays.stream(files).map(File::getName).collect(toSet());
assertEquals(new HashSet<>(asList("Mutation.java", "Query.java",
"EventsQuery.java", "AssetsQuery.java",
"CreateEventMutation.java", "CreateAssetMutation.java",
assertEquals(new HashSet<>(asList("MutationResolver.java", "QueryResolver.java",
"EventsQueryResolver.java", "AssetsQueryResolver.java",
"CreateEventMutationResolver.java", "CreateAssetMutationResolver.java",
"Event.java", "Asset.java", "EventInput.java", "AssetInput.java",
"Node.java", "Status.java", "PinnableItem.java")), generatedFileNames);
......@@ -61,8 +61,8 @@ class GraphQLCodegenExtendTest {
schemaFinder.setIncludePattern("only-extend-queries.*\\.graphqls");
new GraphQLCodegen(schemaFinder.findSchemas(), outputBuildDir, mappingConfig).generate();
assertEquals(new HashSet<>(asList("Subscription.java", "UserQuery.java", "User.java",
"UsersCreatedSubscription.java", "CreateUserMutation.java", "Mutation.java", "Query.java",
assertEquals(new HashSet<>(asList("SubscriptionResolver.java", "UserQueryResolver.java", "User.java",
"UsersCreatedSubscriptionResolver.java", "CreateUserMutationResolver.java", "MutationResolver.java", "QueryResolver.java",
"UserInput.java")), Arrays.stream(Objects.requireNonNull(outputJavaClassesDir.listFiles()))
.map(File::getName).collect(toSet()));
}
......@@ -74,10 +74,10 @@ class GraphQLCodegenExtendTest {
File[] files = Objects.requireNonNull(outputJavaClassesDir.listFiles());
Set<String> generatedFileNames = Arrays.stream(files).map(File::getName).collect(toSet());
assertEquals(new HashSet<>(asList("Mutation.java", "Query.java",
"EventsQuery.java", "AssetsQuery.java",
assertEquals(new HashSet<>(asList("MutationResolver.java", "QueryResolver.java",
"EventsQueryResolver.java", "AssetsQueryResolver.java",
"EventResolver.java", "NodeResolver.java",
"CreateEventMutation.java", "CreateAssetMutation.java",
"CreateEventMutationResolver.java", "CreateAssetMutationResolver.java",
"Event.java", "Asset.java", "EventInput.java", "AssetInput.java",
"Node.java", "Status.java", "PinnableItem.java")), generatedFileNames);
......@@ -96,9 +96,9 @@ class GraphQLCodegenExtendTest {
File[] files = Objects.requireNonNull(outputJavaClassesDir.listFiles());
Set<String> generatedFileNames = Arrays.stream(files).map(File::getName).collect(toSet());
assertEquals(new HashSet<>(asList("Mutation.java", "Query.java",
"EventsQuery.java", "AssetsQuery.java",
"CreateEventMutation.java", "CreateAssetMutation.java",
assertEquals(new HashSet<>(asList("MutationResolver.java", "QueryResolver.java",
"EventsQueryResolver.java", "AssetsQueryResolver.java",
"CreateEventMutationResolver.java", "CreateAssetMutationResolver.java",
"Event.java", "Asset.java", "EventInput.java", "AssetInput.java",
"Node.java", "Status.java", "PinnableItem.java")), generatedFileNames);
......@@ -133,9 +133,9 @@ class GraphQLCodegenExtendTest {
File[] files = Objects.requireNonNull(outputJavaClassesDir.listFiles());
Set<String> generatedFileNames = Arrays.stream(files).map(File::getName).collect(toSet());
assertEquals(new HashSet<>(asList("Mutation.java", "Query.java",
"EventsQuery.java", "AssetsQuery.java",
"CreateEventMutation.java", "CreateAssetMutation.java",
assertEquals(new HashSet<>(asList("MutationResolver.java", "QueryResolver.java",
"EventsQueryResolver.java", "AssetsQueryResolver.java",
"CreateEventMutationResolver.java", "CreateAssetMutationResolver.java",
"Event.java", "Asset.java", "EventInput.java", "AssetInput.java",
"Node.java", "Status.java", "PinnableItem.java")), generatedFileNames);
......
......@@ -46,23 +46,23 @@ class GraphQLCodegenParentInterfacesTest {
File[] files = Objects.requireNonNull(outputJavaClassesDir.listFiles());
assertSameTrimmedContent(
new File("src/test/resources/expected-classes/parent-interfaces/Query.java.txt"),
getFileByName(files, "Query.java"));
new File("src/test/resources/expected-classes/parent-interfaces/QueryResolver.java.txt"),
getFileByName(files, "QueryResolver.java"));
assertSameTrimmedContent(
new File("src/test/resources/expected-classes/parent-interfaces/Mutation.java.txt"),
getFileByName(files, "Mutation.java"));
new File("src/test/resources/expected-classes/parent-interfaces/MutationResolver.java.txt"),
getFileByName(files, "MutationResolver.java"));
assertSameTrimmedContent(
new File("src/test/resources/expected-classes/parent-interfaces/Subscription.java.txt"),
getFileByName(files, "Subscription.java"));
new File("src/test/resources/expected-classes/parent-interfaces/SubscriptionResolver.java.txt"),
getFileByName(files, "SubscriptionResolver.java"));
assertSameTrimmedContent(
new File("src/test/resources/expected-classes/parent-interfaces/VersionQuery.java.txt"),
getFileByName(files, "VersionQuery.java"));
new File("src/test/resources/expected-classes/parent-interfaces/VersionQueryResolver.java.txt"),
getFileByName(files, "VersionQueryResolver.java"));
assertSameTrimmedContent(
new File("src/test/resources/expected-classes/parent-interfaces/CreateEventMutation.java.txt"),
getFileByName(files, "CreateEventMutation.java"));
new File("src/test/resources/expected-classes/parent-interfaces/CreateEventMutationResolver.java.txt"),
getFileByName(files, "CreateEventMutationResolver.java"));
assertSameTrimmedContent(
new File("src/test/resources/expected-classes/parent-interfaces/EventsCreatedSubscription.java.txt"),
getFileByName(files, "EventsCreatedSubscription.java"));
new File("src/test/resources/expected-classes/parent-interfaces/EventsCreatedSubscriptionResolver.java.txt"),
getFileByName(files, "EventsCreatedSubscriptionResolver.java"));
assertSameTrimmedContent(
new File("src/test/resources/expected-classes/parent-interfaces/EventResolver.java.txt"),
getFileByName(files, "EventResolver.java"));
......
......@@ -61,10 +61,10 @@ class GraphQLCodegenTest {
File[] files = Objects.requireNonNull(outputJavaClassesDir.listFiles());
List<String> generatedFileNames = Arrays.stream(files).map(File::getName).sorted().collect(toList());
assertEquals(Arrays.asList(
"CreateEventMutation.java", "Event.java", "EventByIdQuery.java", "EventProperty.java",
"EventStatus.java", "EventsByCategoryAndStatusQuery.java", "EventsByIdsQuery.java",
"EventsCreatedSubscription.java", "Mutation.java", "Query.java", "Subscription.java",
"VersionQuery.java"), generatedFileNames);
"CreateEventMutationResolver.java", "Event.java", "EventByIdQueryResolver.java", "EventProperty.java",
"EventStatus.java", "EventsByCategoryAndStatusQueryResolver.java", "EventsByIdsQueryResolver.java",
"EventsCreatedSubscriptionResolver.java", "MutationResolver.java", "QueryResolver.java", "SubscriptionResolver.java",
"VersionQueryResolver.java"), generatedFileNames);
for (File file : files) {
File expected = new File(String.format("src/test/resources/expected-classes/%s.txt", file.getName()));
......@@ -178,7 +178,7 @@ class GraphQLCodegenTest {
File[] files = Objects.requireNonNull(outputJavaClassesDir.listFiles());
assertFileContainsElements(files, "EventsCreatedSubscription.java",
assertFileContainsElements(files, "EventsCreatedSubscriptionResolver.java",
"org.reactivestreams.Publisher<java.util.Collection<Event>> eventsCreated() throws Exception;");
}
......@@ -204,9 +204,9 @@ class GraphQLCodegenTest {
File[] apiFiles = Objects.requireNonNull(new File(outputJavaClassesDir, "api").listFiles());
List<String> generatedApiFileNames = Arrays.stream(apiFiles).map(File::getName).sorted().collect(toList());
assertEquals(Arrays.asList("CreateEventMutation.java", "EventByIdQuery.java",
"EventsByCategoryAndStatusQuery.java", "EventsByIdsQuery.java", "EventsCreatedSubscription.java", "Mutation.java", "Query.java",
"Subscription.java", "VersionQuery.java"), generatedApiFileNames);
assertEquals(Arrays.asList("CreateEventMutationResolver.java", "EventByIdQueryResolver.java",
"EventsByCategoryAndStatusQueryResolver.java", "EventsByIdsQueryResolver.java", "EventsCreatedSubscriptionResolver.java", "MutationResolver.java", "QueryResolver.java",
"SubscriptionResolver.java", "VersionQueryResolver.java"), generatedApiFileNames);
for (File apiFile : apiFiles) {
assertThat(Utils.getFileContent(apiFile.getPath()),
......@@ -300,10 +300,10 @@ class GraphQLCodegenTest {
File[] files = Objects.requireNonNull(outputBuildDir.listFiles());
assertEquals(2, files.length);
assertSameTrimmedContent(new File("src/test/resources/expected-classes/EmptyMutation.java.txt"),
getFileByName(files, "Mutation.java"));
assertSameTrimmedContent(new File("src/test/resources/expected-classes/EmptyQuery.java.txt"),
getFileByName(files, "Query.java"));
assertSameTrimmedContent(new File("src/test/resources/expected-classes/EmptyMutationResolver.java.txt"),
getFileByName(files, "MutationResolver.java"));
assertSameTrimmedContent(new File("src/test/resources/expected-classes/EmptyQueryResolver.java.txt"),
getFileByName(files, "QueryResolver.java"));
}
@Test
......@@ -331,7 +331,7 @@ class GraphQLCodegenTest {
generator.generate();
File[] files = Objects.requireNonNull(outputJavaClassesDir.listFiles());
assertFileContainsElements(files, "VersionQuery.java", "String version()");
assertFileContainsElements(files, "VersionQueryResolver.java", "String version()");
}
@Test
......@@ -341,13 +341,13 @@ class GraphQLCodegenTest {
File[] files = Objects.requireNonNull(outputJavaClassesDir.listFiles());
assertFileContainsElements(files, "VersionQuery.java",
assertFileContainsElements(files, "VersionQueryResolver.java",
"java.util.concurrent.CompletableFuture<String> version()");
assertFileContainsElements(files, "EventsByCategoryAndStatusQuery.java",
assertFileContainsElements(files, "EventsByCategoryAndStatusQueryResolver.java",
"java.util.concurrent.CompletableFuture<java.util.Collection<Event>> eventsByCategoryAndStatus(");
assertFileContainsElements(files, "EventByIdQuery.java",
assertFileContainsElements(files, "EventByIdQueryResolver.java",
"java.util.concurrent.CompletableFuture<Event> eventById(");
}
......@@ -358,7 +358,7 @@ class GraphQLCodegenTest {
File[] files = Objects.requireNonNull(outputJavaClassesDir.listFiles());
assertFileContainsElements(files, "CreateEventMutation.java",
assertFileContainsElements(files, "CreateEventMutationResolver.java",
"java.util.concurrent.CompletableFuture<Event> createEvent(");
}
......@@ -369,8 +369,8 @@ class GraphQLCodegenTest {
File[] files = Objects.requireNonNull(outputJavaClassesDir.listFiles());
List<String> generatedFileNames = Arrays.stream(files).map(File::getName).sorted().collect(toList());
assertEquals(Arrays.asList("CreateEventMutation.java", "Event.java", "EventInput.java", "EventsQuery.java",
"Mutation.java", "Node.java", "PinnableItem.java", "Query.java", "Status.java"), generatedFileNames);
assertEquals(Arrays.asList("CreateEventMutationResolver.java", "Event.java", "EventInput.java", "EventsQueryResolver.java",
"MutationResolver.java", "Node.java", "PinnableItem.java", "QueryResolver.java", "Status.java"), generatedFileNames);
for (File file : files) {
assertSameTrimmedContent(
......@@ -386,11 +386,11 @@ class GraphQLCodegenTest {
File[] files = Objects.requireNonNull(outputJavaClassesDir.listFiles());
assertSameTrimmedContent(new File("src/test/resources/expected-classes/ProductsByCategoryIdAndStatusQuery.java.txt"),
getFileByName(files, "ProductsByCategoryIdAndStatusQuery.java"));
assertSameTrimmedContent(new File("src/test/resources/expected-classes/ProductsByCategoryIdAndStatusQueryResolver.java.txt"),
getFileByName(files, "ProductsByCategoryIdAndStatusQueryResolver.java"));
assertSameTrimmedContent(new File("src/test/resources/expected-classes/ProductsByIdsQuery.java.txt"),
getFileByName(files, "ProductsByIdsQuery.java"));
assertSameTrimmedContent(new File("src/test/resources/expected-classes/ProductsByIdsQueryResolver.java.txt"),
getFileByName(files, "ProductsByIdsQueryResolver.java"));
}
@Test
......
......@@ -4,7 +4,7 @@ package com.kobylynskyi.graphql.test1;
/**
* Create a new event.
*/
public interface CreateEventMutation {
public interface CreateEventMutationResolver {
/**
* Create a new event.
......
public interface Mutation {
}
\ No newline at end of file
public interface MutationResolver {
}
\ No newline at end of file
public interface Query {
}
\ No newline at end of file
public interface QueryResolver {
}
\ No newline at end of file
......@@ -4,7 +4,7 @@ package com.kobylynskyi.graphql.test1;
/**
* Single event by ID.
*/
public interface EventByIdQuery {
public interface EventByIdQueryResolver {
/**
* Single event by ID.
......
......@@ -4,7 +4,7 @@ package com.kobylynskyi.graphql.test1;
/**
* List of events of a specified category.
*/
public interface EventsByCategoryAndStatusQuery {
public interface EventsByCategoryAndStatusQueryResolver {
/**
* List of events of a specified category.
......
......@@ -4,7 +4,7 @@ package com.kobylynskyi.graphql.test1;
/**
* Events by IDs.
*/
public interface EventsByIdsQuery {
public interface EventsByIdsQueryResolver {
/**
* Events by IDs.
......
......@@ -4,7 +4,7 @@ package com.kobylynskyi.graphql.test1;
/**
* Subscribe to events
*/
public interface EventsCreatedSubscription {
public interface EventsCreatedSubscriptionResolver {
/**
* Subscribe to events
......
package com.kobylynskyi.graphql.test1;
public interface Mutation {
public interface MutationResolver {
/**
* Create a new event.
......
package com.kobylynskyi.graphql.test1;
public interface ProductsByCategoryIdAndStatusQuery {
public interface ProductsByCategoryIdAndStatusQueryResolver {
java.util.Collection<Product> products(String categoryId, String status) throws Exception;
......
package com.kobylynskyi.graphql.test1;
public interface ProductsByIdsQuery {
public interface ProductsByIdsQueryResolver {
java.util.Collection<Product> products(java.util.Collection<String> ids) throws Exception;
......
package com.kobylynskyi.graphql.test1;
public interface Query {
public interface QueryResolver {
/**
* Version of the application.
......
package com.kobylynskyi.graphql.test1;
public interface Subscription {
public interface SubscriptionResolver {
/**
* Subscribe to events
......
......@@ -4,7 +4,7 @@ package com.kobylynskyi.graphql.test1;
/**
* Version of the application.
*/
public interface VersionQuery {
public interface VersionQueryResolver {
/**
* Version of the application.
......
package com.kobylynskyi.graphql.test1;
public interface CreateEventMutation {
public interface CreateEventMutationResolver {
@Deprecated
@javax.validation.constraints.NotNull
......
package com.kobylynskyi.graphql.test1;
public interface EventsQuery {
public interface EventsQueryResolver {
@Deprecated
@javax.validation.constraints.NotNull
......
package com.kobylynskyi.graphql.test1;
public interface Mutation {
public interface MutationResolver {
@Deprecated
@javax.validation.constraints.NotNull
......
package com.kobylynskyi.graphql.test1;
public interface Query {
public interface QueryResolver {
@Deprecated
@javax.validation.constraints.NotNull
......
public interface AssetsQuery {
public interface AssetsQueryResolver {
@javax.validation.constraints.NotNull
java.util.Collection<Asset> assets() throws Exception;
......
public interface CreateAssetMutation {
public interface CreateAssetMutationResolver {
@javax.validation.constraints.NotNull
Asset createAsset(AssetInput input) throws Exception;
......
public interface CreateEventMutation {
public interface CreateEventMutationResolver {
@javax.validation.constraints.NotNull
Event createEvent(EventInput input) throws Exception;
......
public interface EventsQuery {
public interface EventsQueryResolver {
@javax.validation.constraints.NotNull
java.util.Collection<Event> events() throws Exception;
......
public interface Mutation {
public interface MutationResolver {
@javax.validation.constraints.NotNull
Event createEvent(EventInput input) throws Exception;
......
......@@ -2,7 +2,7 @@
* Queries related to events
* Queries related to assets
*/
public interface Query {
public interface QueryResolver {
@javax.validation.constraints.NotNull
java.util.Collection<Event> events() throws Exception;
......
public interface AssetsQuery {
public interface AssetsQueryResolver {
@javax.validation.constraints.NotNull
java.util.Collection<Asset> assets() throws Exception;
......
public interface CreateAssetMutation {
public interface CreateAssetMutationResolver {
@javax.validation.constraints.NotNull
Asset createAsset(AssetInput input) throws Exception;
......
public interface CreateEventMutation {
public interface CreateEventMutationResolver {
@javax.validation.constraints.NotNull
Event createEvent(EventInput input) throws Exception;
......
public interface EventsQuery {
public interface EventsQueryResolver {
@javax.validation.constraints.NotNull
java.util.Collection<Event> events() throws Exception;
......
public interface Mutation {
public interface MutationResolver {
@javax.validation.constraints.NotNull
Event createEvent(EventInput input) throws Exception;
......
......@@ -2,7 +2,7 @@
* Queries related to events
* Queries related to assets
*/
public interface Query {
public interface QueryResolver {
@javax.validation.constraints.NotNull
java.util.Collection<Event> events() throws Exception;
......
......@@ -4,7 +4,7 @@ package com.kobylynskyi.graphql.interfaces;
/**
* Create a new event.
*/
public interface CreateEventMutation extends graphql.kickstart.tools.GraphQLMutationResolver {
public interface CreateEventMutationResolver extends graphql.kickstart.tools.GraphQLMutationResolver {
/**
* Create a new event.
......
......@@ -4,7 +4,7 @@ package com.kobylynskyi.graphql.interfaces;
/**
* Subscribe to events
*/
public interface EventsCreatedSubscription extends graphql.kickstart.tools.GraphQLSubscriptionResolver {
public interface EventsCreatedSubscriptionResolver extends graphql.kickstart.tools.GraphQLSubscriptionResolver {
/**
* Subscribe to events
......
package com.kobylynskyi.graphql.interfaces;
public interface Mutation extends graphql.kickstart.tools.GraphQLMutationResolver {
public interface MutationResolver extends graphql.kickstart.tools.GraphQLMutationResolver {
/**
* Create a new event.
......
package com.kobylynskyi.graphql.interfaces;
public interface Query extends graphql.kickstart.tools.GraphQLQueryResolver {
public interface QueryResolver extends graphql.kickstart.tools.GraphQLQueryResolver {
/**
* Version of the application.
......
package com.kobylynskyi.graphql.interfaces;
public interface Subscription extends graphql.kickstart.tools.GraphQLSubscriptionResolver {
public interface SubscriptionResolver extends graphql.kickstart.tools.GraphQLSubscriptionResolver {
/**
* Subscribe to events
......
......@@ -4,7 +4,7 @@ package com.kobylynskyi.graphql.interfaces;
/**
* Version of the application.
*/
public interface VersionQuery extends graphql.kickstart.tools.GraphQLQueryResolver {
public interface VersionQueryResolver extends graphql.kickstart.tools.GraphQLQueryResolver {
/**
* Version of the application.
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册