Skip to content

Parameterized field getters in type interface are missing parameters #859 #913

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 16, 2022
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
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,14 @@ public class DataModelMapperFactory {
public DataModelMapperFactory(MapperFactory mapperFactory) {
this.mapperFactory = mapperFactory;

this.fieldDefToParamMapper = new FieldDefinitionToParameterMapper(mapperFactory);
InputValueDefinitionToParameterMapper inputValueDefToParamMapper = new InputValueDefinitionToParameterMapper(
mapperFactory);
this.fieldDefToParamMapper = new FieldDefinitionToParameterMapper(mapperFactory, inputValueDefToParamMapper);
this.enumDefToDataModelMapper = new EnumDefinitionToDataModelMapper(mapperFactory);
this.unionDefToDataModelMapper = new UnionDefinitionToDataModelMapper(mapperFactory);
this.typeDefToDataModelMapper = new TypeDefinitionToDataModelMapper(mapperFactory, fieldDefToParamMapper);
this.interfaceDefToDataModelMapper = new InterfaceDefinitionToDataModelMapper(
mapperFactory, fieldDefToParamMapper);
InputValueDefinitionToParameterMapper inputValueDefToParamMapper = new InputValueDefinitionToParameterMapper(
mapperFactory);
this.inputDefToDataModelMapper = new InputDefinitionToDataModelMapper(
mapperFactory, inputValueDefToParamMapper);
this.fieldDefsToResolverDataModelMapper = new FieldDefinitionsToResolverDataModelMapper(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,14 @@ public class FieldDefinitionToParameterMapper {
private final GraphQLTypeMapper graphQLTypeMapper;
private final DataModelMapper dataModelMapper;
private final AnnotationsMapper annotationsMapper;
private final InputValueDefinitionToParameterMapper inputValueDefinitionToParameterMapper;

public FieldDefinitionToParameterMapper(MapperFactory mapperFactory) {
public FieldDefinitionToParameterMapper(MapperFactory mapperFactory,
InputValueDefinitionToParameterMapper inputValueDefToParamMapper) {
this.graphQLTypeMapper = mapperFactory.getGraphQLTypeMapper();
this.dataModelMapper = mapperFactory.getDataModelMapper();
this.annotationsMapper = mapperFactory.getAnnotationsMapper();
this.inputValueDefinitionToParameterMapper = inputValueDefToParamMapper;
}

/**
Expand Down Expand Up @@ -137,6 +140,8 @@ private ParameterDefinition mapField(MappingContext mappingContext, ExtendedFiel
parameter.setDeprecated(DeprecatedDefinitionBuilder.build(mappingContext, fieldDef));
parameter.setMandatory(namedDefinition.isMandatory());
parameter.setSerializeUsingObjectMapper(namedDefinition.isSerializeUsingObjectMapper());
parameter.setInputParameters(inputValueDefinitionToParameterMapper.map(
mappingContext, fieldDef.getInputValueDefinitions(), fieldDef.getName()));
return parameter;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ public class ParameterDefinition {
private List<String> javaDoc = new ArrayList<>();
private DeprecatedDefinition deprecated;
private boolean serializeUsingObjectMapper;
/**
* If the type is parametrized then input parameters will be defined here
*/
private List<ParameterDefinition> inputParameters;
/**
* Definition of the same type, but defined in the parent
*/
Expand Down Expand Up @@ -124,4 +128,12 @@ public ParameterDefinition getDefinitionInParentType() {
public void setDefinitionInParentType(ParameterDefinition definitionInParentType) {
this.definitionInParentType = definitionInParentType;
}

public List<ParameterDefinition> getInputParameters() {
return inputParameters;
}

public void setInputParameters(List<ParameterDefinition> inputParameters) {
this.inputParameters = inputParameters;
}
}
2 changes: 1 addition & 1 deletion src/main/resources/templates/java-lang/interface.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public interface ${className} <#if implements?has_content>extends <#list impleme
<#list field.annotations as annotation>
@${annotation}
</#list>
${field.type} get${field.name?cap_first}();
${field.type} get${field.name?cap_first}(<#list field.inputParameters as param><#list param.annotations as paramAnnotation>@${paramAnnotation}<#if param.annotations?has_content> </#if></#list>${param.type} ${param.name}<#if param_has_next>, </#if></#list>);

</#list>
</#if>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@
import org.junit.jupiter.api.Test;

import java.io.File;
import java.util.Collections;
import java.util.HashSet;
import java.util.Objects;

import static com.kobylynskyi.graphql.codegen.TestUtils.assertSameTrimmedContent;
import static com.kobylynskyi.graphql.codegen.TestUtils.getFileByName;
import static java.util.Arrays.asList;
import static java.util.Collections.singleton;
import static java.util.Collections.singletonList;

class GraphQLCodegenTypesAsInterfacesTest {
Expand All @@ -26,7 +26,9 @@ class GraphQLCodegenTypesAsInterfacesTest {
@BeforeEach
void init() {
mappingConfig.setPackageName("com.github.graphql");
mappingConfig.setFieldsWithResolvers(Collections.singleton("@customResolver"));
mappingConfig.setFieldsWithResolvers(singleton("@customResolver"));
mappingConfig.setFieldsWithoutResolvers(singleton("@noResolver"));
mappingConfig.setTypesAsInterfaces(new HashSet<>(singleton("@asInterface")));
}

@AfterEach
Expand All @@ -36,7 +38,7 @@ void cleanup() {

@Test
void generate_typesAsInterfaces() throws Exception {
mappingConfig.setTypesAsInterfaces(new HashSet<>(asList("@asInterface", "Order")));
mappingConfig.getTypesAsInterfaces().add("Order");

new JavaGraphQLCodegen(singletonList("src/test/resources/schemas/types-as-interfaces.graphqls"),
outputBuildDir, mappingConfig, TestUtils.getStaticGeneratedInfo()).generate();
Expand Down Expand Up @@ -68,16 +70,31 @@ void generate_typesAsInterfacesExtendsInterface() throws Exception {
assertSameTrimmedContent(new File("src/test/resources/expected-classes/" +
"types-as-interfaces-extends-interface/Node.java.txt"), getFileByName(files, "Node.java"));
assertSameTrimmedContent(new File("src/test/resources/expected-classes/" +
"types-as-interfaces-extends-interface/Profile.java.txt"),
"types-as-interfaces-extends-interface/Profile.java.txt"),
getFileByName(files, "Profile.java"));
assertSameTrimmedContent(new File("src/test/resources/expected-classes/" +
"types-as-interfaces-extends-interface/QueryResolver.java.txt"),
"types-as-interfaces-extends-interface/QueryResolver.java.txt"),
getFileByName(files, "QueryResolver.java"));
assertSameTrimmedContent(new File("src/test/resources/expected-classes/" +
"types-as-interfaces-extends-interface/User.java.txt"), getFileByName(files, "User.java"));
assertSameTrimmedContent(new File("src/test/resources/expected-classes/" +
"types-as-interfaces-extends-interface/UserCurrentQueryResolver.java.txt"),
"types-as-interfaces-extends-interface/UserCurrentQueryResolver.java.txt"),
getFileByName(files, "UserCurrentQueryResolver.java"));
}

@Test
void generate_typeAsInterfaceParametrized() throws Exception {
mappingConfig.setGenerateParameterizedFieldsResolvers(true);

new JavaGraphQLCodegen(singletonList("src/test/resources/schemas/" +
"types-as-interfaces-parametrized.graphqls"),
outputBuildDir, mappingConfig, TestUtils.getStaticGeneratedInfo()).generate();

File[] files = Objects.requireNonNull(outputJavaClassesDir.listFiles());

assertSameTrimmedContent(new File(
"src/test/resources/expected-classes/types-as-interfaces-parametrized/Foo.java.txt"),
getFileByName(files, "Foo.java"));
}

}
6 changes: 3 additions & 3 deletions src/test/resources/expected-classes/ProfileOwner.java.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ package com.github.graphql;
)
public interface ProfileOwner {

boolean getAnyPinnableItems();
boolean getAnyPinnableItems(PinnableItemType type);

String getEmail();

Expand All @@ -25,10 +25,10 @@ public interface ProfileOwner {
String getName();

@javax.validation.constraints.NotNull
PinnableItemConnection getPinnableItems();
PinnableItemConnection getPinnableItems(String after, String before, Integer first, Integer last, @javax.validation.constraints.NotNull java.util.List<PinnableItemType> types);

@javax.validation.constraints.NotNull
PinnableItemConnection getPinnedItems();
PinnableItemConnection getPinnedItems(String after, String before, Integer first, Integer last, @javax.validation.constraints.NotNull java.util.List<PinnableItemType> types);

int getPinnedItemsRemaining();

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.github.graphql;


@javax.annotation.Generated(
value = "com.kobylynskyi.graphql.codegen.GraphQLCodegen",
date = "2020-12-31T23:59:59-0500"
)
public interface Foo {

@javax.validation.constraints.NotNull
String getSimpleField();

@javax.validation.constraints.NotNull
String getParameterizedField(int count);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
"""
used with typesAsInterfaces config
"""
directive @asInterface on OBJECT

"""
used with fieldsWithResolvers config
"""
directive @noResolver on FIELD_DEFINITION

type Foo @asInterface {
simpleField: String!
parameterizedField(count: Int!): String! @noResolver
}