Skip to content

Commit a5f72bc

Browse files
anotenderigdianov
authored andcommitted
Add java arrays basic support (#171)
1 parent 5a4ddad commit a5f72bc

File tree

3 files changed

+67
-27
lines changed

3 files changed

+67
-27
lines changed

graphql-jpa-query-schema/src/main/java/com/introproventures/graphql/jpa/query/schema/impl/GraphQLJpaSchemaBuilder.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1031,6 +1031,8 @@ private GraphQLOutputType getGraphQLTypeFromJavaType(Class<?> clazz) {
10311031
classCache.putIfAbsent(clazz, enumType);
10321032

10331033
return enumType;
1034+
} else if (clazz.isArray()) {
1035+
return GraphQLList.list(JavaScalars.of(clazz.getComponentType()));
10341036
}
10351037

10361038
return JavaScalars.of(clazz);

graphql-jpa-query-schema/src/test/java/com/introproventures/graphql/jpa/query/schema/BooksSchemaBuildTest.java

Lines changed: 63 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,16 @@
1717
package com.introproventures.graphql.jpa.query.schema;
1818

1919
import static org.assertj.core.api.Assertions.assertThat;
20+
import static org.assertj.core.api.BDDAssertions.then;
21+
import static org.assertj.core.api.BDDAssertions.thenCode;
2022

21-
import java.util.stream.Collectors;
23+
import java.util.Optional;
2224

2325
import javax.persistence.EntityManager;
2426

2527
import com.introproventures.graphql.jpa.query.schema.impl.GraphQLJpaSchemaBuilder;
2628
import graphql.schema.GraphQLFieldDefinition;
29+
import graphql.schema.GraphQLList;
2730
import graphql.schema.GraphQLSchema;
2831
import org.junit.Before;
2932
import org.junit.Test;
@@ -99,18 +102,13 @@ public void correctlyDerivesToManyOptionalFromGivenEntities() {
99102
.isNotNull();
100103

101104
//then
102-
assertThat(schema.getQueryType()
103-
.getFieldDefinition("Book")
104-
.getType()
105-
.getChildren()
106-
.stream()
107-
.map(GraphQLFieldDefinition.class::cast)
108-
.collect(Collectors.toList())
109-
)
110-
.filteredOn("name", "author")
111-
.extracting(it -> it.getArgument("optional"))
112-
.extractingResultOf("getDefaultValue", Object.class)
113-
.containsExactly(new Boolean(false));
105+
assertThat(getFieldForType("author",
106+
"Book",
107+
schema))
108+
.isPresent().get()
109+
.extracting(it -> it.getArgument("optional"))
110+
.extracting("defaultValue")
111+
.containsExactly(Boolean.FALSE);
114112
}
115113

116114
@Test
@@ -124,20 +122,45 @@ public void correctlyDerivesToOneOptionalFromGivenEntities() {
124122
.isNotNull();
125123

126124
//then
127-
assertThat(schema.getQueryType()
128-
.getFieldDefinition("Author")
129-
.getType()
130-
.getChildren()
131-
.stream()
132-
.map(GraphQLFieldDefinition.class::cast)
133-
.collect(Collectors.toList())
134-
)
135-
.filteredOn("name", "books")
136-
.extracting(it -> it.getArgument("optional"))
137-
.extractingResultOf("getDefaultValue", Object.class)
138-
.containsExactly(new Boolean(true));
125+
assertThat(getFieldForType("books",
126+
"Author",
127+
schema))
128+
.isPresent().get()
129+
.extracting(it -> it.getArgument("optional"))
130+
.extracting("defaultValue")
131+
.containsExactly(Boolean.TRUE);
139132
}
140-
133+
134+
@Test
135+
public void shouldBuildSchemaWithStringArrayAsStringListType() {
136+
//given
137+
//there is a property in the model that is of array type
138+
139+
//when
140+
GraphQLSchema schema = builder.build();
141+
142+
//then
143+
Optional<GraphQLFieldDefinition> tags = getFieldForType("tags",
144+
"SuperBook",
145+
schema);
146+
then(tags)
147+
.isPresent().get()
148+
.extracting(GraphQLFieldDefinition::getType)
149+
.isInstanceOf(GraphQLList.class)
150+
.extracting("wrappedType")
151+
.extracting("name")
152+
.containsOnly("String");
153+
}
154+
155+
@Test
156+
public void shouldBuildSchemaWithStringArrayAsStringListTypeWithoutAnyError() {
157+
//given
158+
//there is a property in the model that is of array type
159+
160+
//then
161+
thenCode(() -> builder.build()).doesNotThrowAnyException();
162+
}
163+
141164
@Test
142165
public void testBuildSchema(){
143166
//given
@@ -146,5 +169,18 @@ public void testBuildSchema(){
146169
//then
147170
assertThat(schema).isNotNull();
148171
}
149-
172+
173+
private Optional<GraphQLFieldDefinition> getFieldForType(String fieldName,
174+
String type,
175+
GraphQLSchema schema) {
176+
return schema.getQueryType()
177+
.getFieldDefinition(type)
178+
.getType()
179+
.getChildren()
180+
.stream()
181+
.map(GraphQLFieldDefinition.class::cast)
182+
.filter(graphQLFieldDefinition -> graphQLFieldDefinition.getName().equals(fieldName))
183+
.findFirst();
184+
}
185+
150186
}

graphql-jpa-query-schema/src/test/java/com/introproventures/graphql/jpa/query/schema/model/book_superclass/BaseBook.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,6 @@ public class BaseBook {
2020
SuperGenre genre;
2121

2222
Date publicationDate;
23+
24+
String[] tags;
2325
}

0 commit comments

Comments
 (0)