Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
184 changes: 91 additions & 93 deletions docs/codegen-options.md

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,8 @@ public class GraphQLCodegenGradleTask extends DefaultTask implements GraphQLCode
private Set<String> fieldsWithResolvers = new HashSet<>();
private Set<String> fieldsWithoutResolvers = new HashSet<>();
private Set<String> typesAsInterfaces = new HashSet<>();
private Set<String> resolverArgumentAnnotations = new HashSet<>();
private Set<String> parametrizedResolverAnnotations = new HashSet<>();
private final RelayConfig relayConfig = new RelayConfig();


Expand Down Expand Up @@ -164,6 +166,10 @@ public void generate() throws Exception {
fieldsWithoutResolvers != null ? fieldsWithoutResolvers : new HashSet<>());
mappingConfig.setTypesAsInterfaces(
typesAsInterfaces != null ? typesAsInterfaces : new HashSet<>());
mappingConfig.setResolverArgumentAnnotations(
resolverArgumentAnnotations != null ? resolverArgumentAnnotations : new HashSet<>());
mappingConfig.setParametrizedResolverAnnotations(
parametrizedResolverAnnotations != null ? parametrizedResolverAnnotations : new HashSet<>());
mappingConfig.setRelayConfig(relayConfig);

mappingConfig.setGenerateClient(generateClient);
Expand Down Expand Up @@ -689,6 +695,28 @@ public void setTypesAsInterfaces(Set<String> typesAsInterfaces) {
this.typesAsInterfaces = typesAsInterfaces;
}

@Input
@Optional
@Override
public Set<String> getResolverArgumentAnnotations() {
return resolverArgumentAnnotations;
}

public void setResolverArgumentAnnotations(Set<String> resolverArgumentAnnotations) {
this.resolverArgumentAnnotations = resolverArgumentAnnotations;
}

@Input
@Optional
@Override
public Set<String> getParametrizedResolverAnnotations() {
return parametrizedResolverAnnotations;
}

public void setParametrizedResolverAnnotations(Set<String> parametrizedResolverAnnotations) {
this.parametrizedResolverAnnotations = parametrizedResolverAnnotations;
}

@Nested
@Optional
@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,12 @@ public class GraphQLCodegenMojo extends AbstractMojo implements GraphQLCodegenCo
@Parameter
private String[] typesAsInterfaces;

@Parameter
private String[] resolverArgumentAnnotations;

@Parameter
private String[] parametrizedResolverAnnotations;

@Parameter(defaultValue = MappingConfigConstants.DEFAULT_RESPONSE_PROJECTION_MAX_DEPTH_STRING)
private int responseProjectionMaxDepth;

Expand Down Expand Up @@ -271,6 +277,8 @@ public void execute() throws MojoExecutionException {
mappingConfig.setResponseProjectionMaxDepth(responseProjectionMaxDepth);
mappingConfig.setUseObjectMapperForRequestSerialization(mapToHashSet(useObjectMapperForRequestSerialization));
mappingConfig.setTypesAsInterfaces(mapToHashSet(typesAsInterfaces));
mappingConfig.setResolverArgumentAnnotations(mapToHashSet(resolverArgumentAnnotations));
mappingConfig.setParametrizedResolverAnnotations(mapToHashSet(parametrizedResolverAnnotations));

mappingConfig.setResolverParentInterface(getResolverParentInterface());
mappingConfig.setQueryResolverParentInterface(getQueryResolverParentInterface());
Expand Down Expand Up @@ -590,6 +598,16 @@ public Set<String> getTypesAsInterfaces() {
return mapToHashSet(typesAsInterfaces);
}

@Override
public Set<String> getResolverArgumentAnnotations() {
return mapToHashSet(resolverArgumentAnnotations);
}

@Override
public Set<String> getParametrizedResolverAnnotations() {
return mapToHashSet(parametrizedResolverAnnotations);
}

@Override
public String getQueryResolverParentInterface() {
return parentInterfaces.getQueryResolver();
Expand Down
10 changes: 10 additions & 0 deletions src/main/java/com/kobylynskyi/graphql/codegen/GraphQLCodegen.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import static java.util.stream.Collectors.toList;

Expand Down Expand Up @@ -96,6 +97,15 @@ private static void sanitizeValues(MappingConfig mappingConfig) {
mappingConfig.setModelValidationAnnotation(
Utils.replaceLeadingAtSign(mappingConfig.getModelValidationAnnotation()));

if (mappingConfig.getResolverArgumentAnnotations() != null) {
mappingConfig.setResolverArgumentAnnotations(mappingConfig.getResolverArgumentAnnotations().stream()
.map(Utils::replaceLeadingAtSign).collect(Collectors.toSet()));
}
if (mappingConfig.getParametrizedResolverAnnotations() != null) {
mappingConfig.setParametrizedResolverAnnotations(mappingConfig.getParametrizedResolverAnnotations().stream()
.map(Utils::replaceLeadingAtSign).collect(Collectors.toSet()));
}

Map<String, List<String>> customAnnotationsMapping = mappingConfig.getCustomAnnotationsMapping();
if (customAnnotationsMapping != null) {
for (Map.Entry<String, List<String>> entry : customAnnotationsMapping.entrySet()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import com.kobylynskyi.graphql.codegen.generators.FreeMarkerTemplateFilesCreator;
import com.kobylynskyi.graphql.codegen.generators.FreeMarkerTemplateType;
import com.kobylynskyi.graphql.codegen.mapper.DataModelMapperFactory;
import com.kobylynskyi.graphql.codegen.mapper.FieldDefinitionToParameterMapper;
import com.kobylynskyi.graphql.codegen.mapper.FieldDefinitionsToResolverDataModelMapper;
import com.kobylynskyi.graphql.codegen.model.MappingContext;
import com.kobylynskyi.graphql.codegen.model.definitions.ExtendedDefinition;
Expand All @@ -17,6 +16,7 @@
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Set;

import static java.util.stream.Collectors.toList;

Expand Down Expand Up @@ -51,9 +51,9 @@ private List<File> generate(List<ExtendedFieldDefinition> fieldDefinitions,
if (!Boolean.TRUE.equals(mappingContext.getGenerateApis())) {
return Collections.emptyList();
}
Set<String> fieldNamesWithResolvers = mappingContext.getFieldNamesWithResolvers();
List<ExtendedFieldDefinition> fieldDefsWithResolvers = fieldDefinitions.stream()
.filter(fieldDef -> FieldDefinitionToParameterMapper.generateResolversForField(
mappingContext, fieldDef, parentDefinition))
.filter(fieldDef -> fieldNamesWithResolvers.contains(parentDefinition.getName() + "." + fieldDef.getName()))
.collect(toList());

List<File> generatedFiles = new ArrayList<>();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.kobylynskyi.graphql.codegen.mapper;

import com.kobylynskyi.graphql.codegen.model.MappingConfigConstants;
import com.kobylynskyi.graphql.codegen.model.MappingContext;
import com.kobylynskyi.graphql.codegen.model.definitions.ExtendedDefinition;
import com.kobylynskyi.graphql.codegen.model.definitions.ExtendedFieldDefinition;
Expand Down Expand Up @@ -120,6 +121,18 @@ public List<String> getAnnotations(MappingContext mappingContext, String graphQL
annotations.addAll(getAnnotationsForDirective(mappingContext, directiveAnnotations, directive));
}
}
// 6. Add annotations for resolver arguments
if (!Utils.isEmpty(mappingContext.getResolverArgumentAnnotations())
&& mappingContext.getOperationsName().contains(parentTypeName)) {
annotations.addAll(mappingContext.getResolverArgumentAnnotations());
}
// 7. Add annotations for parametrized resolvers
if (!Utils.isEmpty(mappingContext.getParametrizedResolverAnnotations())
&& mappingContext.getFieldNamesWithResolvers().contains(parentTypeName + "." + name)) {
for (String annotation : mappingContext.getParametrizedResolverAnnotations()) {
annotations.add(annotation.replace(MappingConfigConstants.TYPE_NAME_PLACEHOLDER, parentTypeName));
}
}
return annotations;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -432,6 +432,30 @@ public interface GraphQLCodegenConfiguration {
*/
Set<String> getTypesAsInterfaces();

/**
* Annotations that will be added to all resolver arguments.
*
* <p>E.g.:
* <ul>
* <li>{@code @org.springframework.graphql.data.method.annotation.Argument}</li>
* </ul>
*
* @return Set of annotations that every resolver argument will have.
*/
Set<String> getResolverArgumentAnnotations();

/**
* Annotations that will be added to all parametrized resolver methods.
*
* <p>E.g.:
* <ul>
* <li>{@code @org.springframework.graphql.data.method.annotation.Argument}</li>
* </ul>
*
* @return Set of annotations that every parametrized resolver method will have.
*/
Set<String> getParametrizedResolverAnnotations();

/**
* Generate code with lang
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,6 @@
*/
public class MappingConfig implements GraphQLCodegenConfiguration, Combinable<MappingConfig> {

private Map<String, String> customTypesMapping = new HashMap<>();
private Map<String, List<String>> customAnnotationsMapping = new HashMap<>();
private Map<String, List<String>> directiveAnnotationsMapping = new HashMap<>();

// package name configs:
private String packageName;
private String apiPackageName;
Expand Down Expand Up @@ -77,6 +73,14 @@ public class MappingConfig implements GraphQLCodegenConfiguration, Combinable<Ma
private Integer responseProjectionMaxDepth;
private Set<String> useObjectMapperForRequestSerialization = new HashSet<>();

// annotations:
private Map<String, List<String>> customAnnotationsMapping = new HashMap<>();
private Map<String, List<String>> directiveAnnotationsMapping = new HashMap<>();
private Set<String> resolverArgumentAnnotations = new HashSet<>();
private Set<String> parametrizedResolverAnnotations = new HashSet<>();

private Map<String, String> customTypesMapping = new HashMap<>();

private Set<String> typesAsInterfaces = new HashSet<>();

private boolean generateModelOpenClasses;
Expand Down Expand Up @@ -179,6 +183,8 @@ public void combine(MappingConfig source) {
customTypesMapping = combineMap(customTypesMapping, source.customTypesMapping);
customAnnotationsMapping = combineMap(customAnnotationsMapping, source.customAnnotationsMapping);
directiveAnnotationsMapping = combineMap(directiveAnnotationsMapping, source.directiveAnnotationsMapping);
resolverArgumentAnnotations = combineSet(resolverArgumentAnnotations, source.resolverArgumentAnnotations);
parametrizedResolverAnnotations = combineSet(parametrizedResolverAnnotations, source.parametrizedResolverAnnotations);
generateAllMethodInProjection = getValueOrDefaultToThis(source,
GraphQLCodegenConfiguration::getGenerateAllMethodInProjection);
responseProjectionMaxDepth = getValueOrDefaultToThis(source,
Expand Down Expand Up @@ -641,6 +647,24 @@ public void setUseObjectMapperForRequestSerialization(Set<String> useObjectMappe
this.useObjectMapperForRequestSerialization = useObjectMapperForRequestSerialization;
}

@Override
public Set<String> getResolverArgumentAnnotations() {
return resolverArgumentAnnotations;
}

public void setResolverArgumentAnnotations(Set<String> resolverArgumentAnnotations) {
this.resolverArgumentAnnotations = resolverArgumentAnnotations;
}

@Override
public Set<String> getParametrizedResolverAnnotations() {
return parametrizedResolverAnnotations;
}

public void setParametrizedResolverAnnotations(Set<String> parametrizedResolverAnnotations) {
this.parametrizedResolverAnnotations = parametrizedResolverAnnotations;
}

@Override
public Set<String> getTypesAsInterfaces() {
return typesAsInterfaces;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ public class MappingConfigConstants {

public static final String DEFAULT_VALIDATION_ANNOTATION = "javax.validation.constraints.NotNull";
public static final String PARENT_INTERFACE_TYPE_PLACEHOLDER = "{{TYPE}}";
public static final String TYPE_NAME_PLACEHOLDER = "{{TYPE_NAME}}";
public static final boolean DEFAULT_GENERATE_APIS = true;
public static final String DEFAULT_GENERATE_APIS_STRING = "true";
public static final boolean DEFAULT_BUILDER = true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,17 @@

import com.kobylynskyi.graphql.codegen.mapper.DataModelMapper;
import com.kobylynskyi.graphql.codegen.mapper.DataModelMapperFactory;
import com.kobylynskyi.graphql.codegen.mapper.FieldDefinitionToParameterMapper;
import com.kobylynskyi.graphql.codegen.model.definitions.ExtendedDefinition;
import com.kobylynskyi.graphql.codegen.model.definitions.ExtendedDocument;
import com.kobylynskyi.graphql.codegen.model.definitions.ExtendedEnumTypeDefinition;
import com.kobylynskyi.graphql.codegen.model.definitions.ExtendedFieldDefinition;
import com.kobylynskyi.graphql.codegen.model.definitions.ExtendedInterfaceTypeDefinition;
import com.kobylynskyi.graphql.codegen.model.definitions.ExtendedObjectTypeDefinition;

import java.io.File;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
Expand All @@ -26,11 +29,13 @@ public class MappingContext implements GraphQLCodegenConfiguration {
private final ExtendedDocument document;
private final Set<String> typesUnionsInterfacesNames;
private final Set<String> interfacesName;
private final Set<String> operationsName;
private final Map<String, Set<String>> interfaceChildren;
private final GeneratedInformation generatedInformation;
private final DataModelMapperFactory dataModelMapperFactory;
private Set<String> enumImportItSelfInScala;
private Map<String, Set<String>> parentInterfaceProperties;
private Set<String> fieldNamesWithResolvers;

private MappingContext(File outputDirectory,
MappingConfig mappingConfig,
Expand All @@ -44,6 +49,7 @@ private MappingContext(File outputDirectory,
this.interfacesName = document.getInterfacesNames();
this.interfaceChildren = document.getInterfaceChildren();
this.generatedInformation = generatedInformation;
this.operationsName = document.getOperationsNames();
this.dataModelMapperFactory = dataModelMapperFactory;
}

Expand All @@ -63,7 +69,6 @@ public Boolean isInitializeNullableTypes() {
}



@Override
public Boolean isGenerateSealedInterfaces() {
return config.isGenerateSealedInterfaces();
Expand Down Expand Up @@ -309,6 +314,16 @@ public Set<String> getTypesAsInterfaces() {
return config.getTypesAsInterfaces();
}

@Override
public Set<String> getResolverArgumentAnnotations() {
return config.getResolverArgumentAnnotations();
}

@Override
public Set<String> getParametrizedResolverAnnotations() {
return config.getParametrizedResolverAnnotations();
}

@Override
public Boolean isSupportUnknownFields() {
return config.isSupportUnknownFields();
Expand All @@ -331,6 +346,10 @@ public Set<String> getInterfacesName() {
return interfacesName;
}

public Set<String> getOperationsName() {
return operationsName;
}

public Map<String, Set<String>> getInterfaceChildren() {
return interfaceChildren;
}
Expand Down Expand Up @@ -381,6 +400,25 @@ public Map<String, Set<String>> getParentInterfaceProperties() {
return parentInterfaceProperties;
}

public Set<String> getFieldNamesWithResolvers() {
if (fieldNamesWithResolvers == null) {
fieldNamesWithResolvers = new HashSet<>();
for (ExtendedObjectTypeDefinition definition : document.getTypeDefinitions()) {
definition.getFieldDefinitions().stream()
.filter(fieldDef -> FieldDefinitionToParameterMapper.generateResolversForField(
this, fieldDef, definition))
.forEach(fieldDef -> fieldNamesWithResolvers.add(definition.getName() + "." + fieldDef.getName()));
}
for (ExtendedInterfaceTypeDefinition definition : document.getInterfaceDefinitions()) {
definition.getFieldDefinitions().stream()
.filter(fieldDef -> FieldDefinitionToParameterMapper.generateResolversForField(
this, fieldDef, definition))
.forEach(fieldDef -> fieldNamesWithResolvers.add(definition.getName() + "." + fieldDef.getName()));
}
}
return fieldNamesWithResolvers;
}

private String getModelClassNameWithPrefixAndSuffix(ExtendedEnumTypeDefinition extendedEnumTypeDefinition) {
return DataModelMapper.getModelClassNameWithPrefixAndSuffix(this, extendedEnumTypeDefinition.getName());
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.kobylynskyi.graphql.codegen.model.definitions;

import graphql.language.FieldDefinition;
import graphql.language.Type;
import graphql.language.TypeName;

Expand Down Expand Up @@ -87,6 +88,14 @@ public Set<String> getInterfacesNames() {
.collect(Collectors.toSet());
}

public Set<String> getOperationsNames() {
return operationDefinitions.stream()
.map(ExtendedObjectTypeDefinition::getFieldDefinitions)
.flatMap(Collection::stream)
.map(FieldDefinition::getName)
.collect(Collectors.toSet());
}

public Collection<ExtendedObjectTypeDefinition> getOperationDefinitions() {
return operationDefinitions;
}
Expand Down
Loading