Skip to content

Add java arrays basic support #171

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
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 @@ -1031,6 +1031,8 @@ private GraphQLOutputType getGraphQLTypeFromJavaType(Class<?> clazz) {
classCache.putIfAbsent(clazz, enumType);

return enumType;
} else if (clazz.isArray()) {
return GraphQLList.list(JavaScalars.of(clazz.getComponentType()));
}

return JavaScalars.of(clazz);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,16 @@
package com.introproventures.graphql.jpa.query.schema;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.BDDAssertions.then;
import static org.assertj.core.api.BDDAssertions.thenCode;

import java.util.stream.Collectors;
import java.util.Optional;

import javax.persistence.EntityManager;

import com.introproventures.graphql.jpa.query.schema.impl.GraphQLJpaSchemaBuilder;
import graphql.schema.GraphQLFieldDefinition;
import graphql.schema.GraphQLList;
import graphql.schema.GraphQLSchema;
import org.junit.Before;
import org.junit.Test;
Expand Down Expand Up @@ -99,18 +102,13 @@ public void correctlyDerivesToManyOptionalFromGivenEntities() {
.isNotNull();

//then
assertThat(schema.getQueryType()
.getFieldDefinition("Book")
.getType()
.getChildren()
.stream()
.map(GraphQLFieldDefinition.class::cast)
.collect(Collectors.toList())
)
.filteredOn("name", "author")
.extracting(it -> it.getArgument("optional"))
.extractingResultOf("getDefaultValue", Object.class)
.containsExactly(new Boolean(false));
assertThat(getFieldForType("author",
"Book",
schema))
.isPresent().get()
.extracting(it -> it.getArgument("optional"))
.extracting("defaultValue")
.containsExactly(Boolean.FALSE);
}

@Test
Expand All @@ -124,20 +122,45 @@ public void correctlyDerivesToOneOptionalFromGivenEntities() {
.isNotNull();

//then
assertThat(schema.getQueryType()
.getFieldDefinition("Author")
.getType()
.getChildren()
.stream()
.map(GraphQLFieldDefinition.class::cast)
.collect(Collectors.toList())
)
.filteredOn("name", "books")
.extracting(it -> it.getArgument("optional"))
.extractingResultOf("getDefaultValue", Object.class)
.containsExactly(new Boolean(true));
assertThat(getFieldForType("books",
"Author",
schema))
.isPresent().get()
.extracting(it -> it.getArgument("optional"))
.extracting("defaultValue")
.containsExactly(Boolean.TRUE);
}


@Test
public void shouldBuildSchemaWithStringArrayAsStringListType() {
//given
//there is a property in the model that is of array type

//when
GraphQLSchema schema = builder.build();

//then
Optional<GraphQLFieldDefinition> tags = getFieldForType("tags",
"SuperBook",
schema);
then(tags)
.isPresent().get()
.extracting(GraphQLFieldDefinition::getType)
.isInstanceOf(GraphQLList.class)
.extracting("wrappedType")
.extracting("name")
.containsOnly("String");
}

@Test
public void shouldBuildSchemaWithStringArrayAsStringListTypeWithoutAnyError() {
//given
//there is a property in the model that is of array type

//then
thenCode(() -> builder.build()).doesNotThrowAnyException();
}

@Test
public void testBuildSchema(){
//given
Expand All @@ -146,5 +169,18 @@ public void testBuildSchema(){
//then
assertThat(schema).isNotNull();
}


private Optional<GraphQLFieldDefinition> getFieldForType(String fieldName,
String type,
GraphQLSchema schema) {
return schema.getQueryType()
.getFieldDefinition(type)
.getType()
.getChildren()
.stream()
.map(GraphQLFieldDefinition.class::cast)
.filter(graphQLFieldDefinition -> graphQLFieldDefinition.getName().equals(fieldName))
.findFirst();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,6 @@ public class BaseBook {
SuperGenre genre;

Date publicationDate;

String[] tags;
}