Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
…l-java-codegen into meistermeier-issue/gh-673
  • Loading branch information
kobylynskyi committed Sep 4, 2021
2 parents 1cb8024 + aa85c36 commit 4730207
Show file tree
Hide file tree
Showing 11 changed files with 201 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,9 @@ public String map(MappingContext mappingContext, Value<?> value, Type<?> graphQL
}

private String mapEnum(MappingContext mappingContext, EnumValue value, Type<?> graphQLType) {
if (graphQLType == null) {
return value.getName();
}
if (graphQLType instanceof TypeName) {
String typeName = ((TypeName) graphQLType).getName();
typeName = DataModelMapper.getModelClassNameWithPrefixAndSuffix(mappingContext, typeName);
Expand Down
18 changes: 16 additions & 2 deletions src/main/resources/templates/scala-lang/type.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,37 @@ import com.kobylynskyi.graphql.codegen.model.graphql.GraphQLRequestSerializer
import java.util.Objects
</#if>
import scala.collection.JavaConverters._
<#assign duplicateEnumImports = [] />
<#assign enumImports = [] />
<#if fields?has_content>
<#if enumImportItSelfInScala?has_content>
<#list fields as field>
<#list enumImportItSelfInScala as enum>
<#if MapperUtil.isScalaCollection(field.type)>
<#if enum == MapperUtil.getGenericParameter(field.type)>
import ${enum}._
<#assign duplicateEnumImports = duplicateEnumImports + [enum] />
</#if>
<#else >
<#if enum == field.type>
import ${enum}._
<#assign duplicateEnumImports = duplicateEnumImports + [enum] />
</#if>
</#if>
</#list>
</#list>
</#if>
</#if>
<#if duplicateEnumImports?has_content>
<#list duplicateEnumImports as duplicateEnumImport>
<#if !enumImports?seq_contains(duplicateEnumImport)>
<#assign enumImports = enumImports + [duplicateEnumImport]>
</#if>
</#list>
</#if>
<#if enumImports?has_content>
<#list enumImports as enumImport>
import ${enumImport}._
</#list>
</#if>
<#assign duplicateParentInterfaces = [] />
<#assign parentInterfaces = [] />
<#if fields?has_content>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,8 @@ void generate_Directives() throws Exception {
"n={{n?toString}})"));
directiveAnnotationsMapping.put("valid", singletonList("@javax.validation.Valid"));
directiveAnnotationsMapping.put("customResolver", singletonList("@com.example.CustomAnnotation"));
directiveAnnotationsMapping.put("relationship",
singletonList("@com.example.Relationship(type = {{type}}, direction = {{direction}})"));
mappingConfig.setDirectiveAnnotationsMapping(directiveAnnotationsMapping);

new JavaGraphQLCodegen(singletonList("src/test/resources/schemas/test.graphqls"),
Expand All @@ -259,6 +261,9 @@ void generate_Directives() throws Exception {
assertSameTrimmedContent(
new File("src/test/resources/expected-classes/annotation/EventProperty.java.txt"),
getFileByName(files, "EventProperty.java"));
assertSameTrimmedContent(
new File("src/test/resources/expected-classes/annotation/User.java.txt"),
getFileByName(files, "User.java"));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ void generate_DoNotGenerateApiInterfaceForOperations() throws IOException {
File[] files = Objects.requireNonNull(outputJavaClassesDir.listFiles());
assertEquals(Arrays.asList(
"Event.java", "EventProperty.java", "EventPropertyResolver.java", "EventStatus.java",
"MutationResolver.java", "QueryResolver.java", "SubscriptionResolver.java"),
"MutationResolver.java", "QueryResolver.java", "SubscriptionResolver.java", "User.java"),
Arrays.stream(files).map(File::getName).sorted().collect(toList()));
}

Expand All @@ -155,7 +155,8 @@ void generate_DoNotGenerateRootApiInterfaces() throws IOException {
assertEquals(Arrays.asList(
"CreateEventMutationResolver.java", "Event.java", "EventByIdQueryResolver.java", "EventProperty.java",
"EventPropertyResolver.java", "EventStatus.java", "EventsByCategoryAndStatusQueryResolver.java",
"EventsByIdsQueryResolver.java", "EventsCreatedSubscriptionResolver.java", "VersionQueryResolver.java"),
"EventsByIdsQueryResolver.java", "EventsCreatedSubscriptionResolver.java", "User.java",
"VersionQueryResolver.java"),
Arrays.stream(files).map(File::getName).sorted().collect(toList()));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ void generate_CheckFiles_generateApisFalse() throws Exception {
File[] files = Objects.requireNonNull(outputJavaClassesDir.listFiles());
List<String> generatedFileNames = Arrays.stream(files).map(File::getName).sorted().collect(toList());
assertEquals(Arrays.asList("Event.java", "EventProperty.java", "EventStatus.java",
"Mutation.java", "Query.java", "Subscription.java"),
"Mutation.java", "Query.java", "Subscription.java", "User.java"),
generatedFileNames);
}

Expand All @@ -89,7 +89,8 @@ void generate_CheckFiles_generateApisTrue_CustomTypeResolverSuffix() throws Exce
"EventsByCategoryAndStatusQueryResolver.java", "EventsByIdsQueryResolver.java",
"EventsCreatedSubscriptionResolver.java", "Mutation.java", "MutationResolver.java",
"MutationTypeResolver.java", "Query.java", "QueryResolver.java", "QueryTypeResolver.java",
"Subscription.java", "SubscriptionResolver.java", "VersionQueryResolver.java"), generatedFileNames);
"Subscription.java", "SubscriptionResolver.java", "User.java", "VersionQueryResolver.java"),
generatedFileNames);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ void generate_CheckFiles() throws Exception {
"CreateEventMutationResolver.java", "Event.java", "EventByIdQueryResolver.java", "EventProperty.java",
"EventStatus.java", "EventsByCategoryAndStatusQueryResolver.java", "EventsByIdsQueryResolver.java",
"EventsCreatedSubscriptionResolver.java", "MutationResolver.java", "QueryResolver.java",
"SubscriptionResolver.java",
"SubscriptionResolver.java", "User.java",
"VersionQueryResolver.java"), generatedFileNames);

for (File file : files) {
Expand Down Expand Up @@ -170,7 +170,8 @@ void generate_CustomModelAndApiPackages() throws Exception {

File[] modelFiles = Objects.requireNonNull(new File(outputJavaClassesDir, "model").listFiles());
List<String> generatedModelFileNames = Arrays.stream(modelFiles).map(File::getName).sorted().collect(toList());
assertEquals(Arrays.asList("Event.java", "EventProperty.java", "EventStatus.java"), generatedModelFileNames);
assertEquals(Arrays.asList("Event.java", "EventProperty.java", "EventStatus.java", "User.java"),
generatedModelFileNames);

for (File modelFile : modelFiles) {
assertThat(Utils.getFileContent(modelFile.getPath()),
Expand Down Expand Up @@ -297,7 +298,8 @@ void generate_OnlyModel() throws Exception {

File[] files = Objects.requireNonNull(outputJavaClassesDir.listFiles());
List<String> generatedFileNames = Arrays.stream(files).map(File::getName).sorted().collect(toList());
assertEquals(Arrays.asList("Event.java", "EventProperty.java", "EventStatus.java"), generatedFileNames);
assertEquals(Arrays.asList("Event.java", "EventProperty.java", "EventStatus.java", "User.java"),
generatedFileNames);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,8 @@ void generate_Directives() throws Exception {
"@com.example.CustomAnnotation(roles={{roles?toArray}}, " +
"boo={{boo?toArray}}, float={{float?toArrayOfStrings}}, int={{int}}, " +
"n={{n?toString}})"));
directiveAnnotationsMapping.put("relationship",
singletonList("@com.example.Relationship(type = {{type}}, direction = {{direction}})"));
mappingConfig.setDirectiveAnnotationsMapping(directiveAnnotationsMapping);

new ScalaGraphQLCodegen(singletonList("src/test/resources/schemas/test.graphqls"),
Expand All @@ -183,6 +185,9 @@ void generate_Directives() throws Exception {
assertSameTrimmedContent(
new File("src/test/resources/expected-classes/scala/annotation/MutationResolver.scala.txt"),
getFileByName(files, "MutationResolver.scala"));
assertSameTrimmedContent(
new File("src/test/resources/expected-classes/scala/annotation/User.scala.txt"),
getFileByName(files, "User.scala"));
}

}
68 changes: 68 additions & 0 deletions src/test/resources/expected-classes/User.java.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package com.kobylynskyi.graphql.test1;


/**
* type with directive using enum value
*/
@javax.annotation.Generated(
value = "com.kobylynskyi.graphql.codegen.GraphQLCodegen",
date = "2020-12-31T23:59:59-0500"
)
public class User implements java.io.Serializable {

private String name;
private java.util.List<User> friends;

public User() {
}

public User(String name, java.util.List<User> friends) {
this.name = name;
this.friends = friends;
}

public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}

public java.util.List<User> getFriends() {
return friends;
}
public void setFriends(java.util.List<User> friends) {
this.friends = friends;
}



public static User.Builder builder() {
return new User.Builder();
}

public static class Builder {

private String name;
private java.util.List<User> friends;

public Builder() {
}

public Builder setName(String name) {
this.name = name;
return this;
}

public Builder setFriends(java.util.List<User> friends) {
this.friends = friends;
return this;
}


public User build() {
return new User(name, friends);
}

}
}
69 changes: 69 additions & 0 deletions src/test/resources/expected-classes/annotation/User.java.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package com.kobylynskyi.graphql.test1;


/**
* type with directive using enum value
*/
@javax.annotation.Generated(
value = "com.kobylynskyi.graphql.codegen.GraphQLCodegen",
date = "2020-12-31T23:59:59-0500"
)
public class User implements java.io.Serializable {

private String name;
@com.example.Relationship(type = "FRIEND_WITH", direction = OUT)
private java.util.List<User> friends;

public User() {
}

public User(String name, java.util.List<User> friends) {
this.name = name;
this.friends = friends;
}

public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}

public java.util.List<User> getFriends() {
return friends;
}
public void setFriends(java.util.List<User> friends) {
this.friends = friends;
}



public static User.Builder builder() {
return new User.Builder();
}

public static class Builder {

private String name;
private java.util.List<User> friends;

public Builder() {
}

public Builder setName(String name) {
this.name = name;
return this;
}

public Builder setFriends(java.util.List<User> friends) {
this.friends = friends;
return this;
}


public User build() {
return new User(name, friends);
}

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.kobylynskyi.graphql.test1

import scala.collection.JavaConverters._

/**
* type with directive using enum value
*/
@javax.annotation.Generated(
value = Array("com.kobylynskyi.graphql.codegen.GraphQLCodegen"),
date = "2020-12-31T23:59:59-0500"
)
case class User(
name: String,
@com.example.Relationship(type = "FRIEND_WITH", direction = OUT)
friends: scala.Seq[User]
) {

}
6 changes: 6 additions & 0 deletions src/test/resources/schemas/test.graphqls
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,12 @@ enum EventStatus {
LOGGED
}

# type with directive using enum value
type User {
name: String,
friends: [User] @relationship(type: "FRIEND_WITH", direction: OUT)
}

directive @auth (
roles: [String] = ["viewer"],
float: [Float] = [],
Expand Down

0 comments on commit 4730207

Please sign in to comment.