Skip to content
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

Support for Nested @Embedded #367

Closed
Closed
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 @@ -968,6 +968,7 @@ private GraphQLType getEmbeddableType(EmbeddableType<?> embeddableType, boolean
if (embeddableInputCache.containsKey(embeddableType.getJavaType())) {
return embeddableInputCache.get(embeddableType.getJavaType());
}

graphQLType =
GraphQLInputObjectType
.newInputObject()
Expand All @@ -981,10 +982,31 @@ private GraphQLType getEmbeddableType(EmbeddableType<?> embeddableType, boolean
.getAttributes()
.stream()
.filter(this::isNotIgnored)
.map(this::getInputObjectField)
.map(attribute -> {
if (isEmbeddable(attribute)) {
EmbeddableType nestedEmbeddableType = (EmbeddableType) (
(SingularAttribute) attribute
).getType();
return GraphQLInputObjectField
.newInputObjectField()
.name(attribute.getName())
.description(getSchemaDescription(attribute))
.type(
(GraphQLInputType) getEmbeddableType(
nestedEmbeddableType,
input,
searchByIdArg
)
)
.build();
} else {
return getInputObjectField(attribute);
}
})
.collect(Collectors.toList())
)
.build();

embeddableInputCache.put(embeddableType.getJavaType(), (GraphQLInputObjectType) graphQLType);
return graphQLType;
}
Expand All @@ -994,8 +1016,10 @@ private GraphQLType getEmbeddableType(EmbeddableType<?> embeddableType, boolean
if (embeddableOutputCache.containsKey(embeddableType.getJavaType())) {
return embeddableOutputCache.get(embeddableType.getJavaType());
}

String embeddableTypeName =
namingStrategy.singularize(embeddableType.getJavaType().getSimpleName()) + "EmbeddableType";

graphQLType =
GraphQLObjectType
.newObject()
Expand All @@ -1006,12 +1030,35 @@ private GraphQLType getEmbeddableType(EmbeddableType<?> embeddableType, boolean
.getAttributes()
.stream()
.filter(this::isNotIgnored)
.map(this::getObjectField)
.map(attribute -> {
if (isEmbeddable(attribute)) {
EmbeddableType nestedEmbeddableType = (EmbeddableType) (
(SingularAttribute) attribute
).getType();

return GraphQLFieldDefinition
.newFieldDefinition()
.name(attribute.getName())
.description(getSchemaDescription(attribute))
.type(
(GraphQLOutputType) getEmbeddableType(
nestedEmbeddableType,
input,
searchByIdArg
)
)
.build();
} else {
return getObjectField(attribute);
}
})
.collect(Collectors.toList())
)
.build();

embeddableOutputCache.putIfAbsent(embeddableType.getJavaType(), (GraphQLObjectType) graphQLType);
}

return graphQLType;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

import com.introproventures.graphql.jpa.query.AbstractSpringBootTestSupport;
import com.introproventures.graphql.jpa.query.schema.impl.GraphQLJpaSchemaBuilder;
import com.introproventures.graphql.jpa.query.schema.model.book.Address;
import com.introproventures.graphql.jpa.query.schema.model.book.Author;
import com.introproventures.graphql.jpa.query.schema.model.book.Book;
import com.introproventures.graphql.jpa.query.schema.model.book_superclass.SuperAuthor;
Expand Down Expand Up @@ -154,6 +155,23 @@ public void correctlyDerivesToOneOptionalFromGivenEntities() {
.isEqualTo(Boolean.TRUE);
}

@Test
public void correctlyDerivesNestedEmbeddableFromGivenEntities() {
//when
GraphQLSchema schema = builder.build();

// then
assertThat(schema).describedAs("Ensure the schema is generated").isNotNull();

//then
assertThat(getFieldForType(Author.Fields.homeAddress, "Author", schema))
.isPresent()
.get()
.extracting(it -> getField(Address.Fields.zipcode, it).isPresent())
.describedAs("Ensure that a nested embeddable can be queried on")
.isEqualTo(Boolean.TRUE);
}

@Test
public void shouldBuildSchemaWithStringArrayAsStringListType() {
//given
Expand Down Expand Up @@ -193,9 +211,11 @@ public void testBuildSchema() {
}

private Optional<GraphQLFieldDefinition> getFieldForType(String fieldName, String type, GraphQLSchema schema) {
return schema
.getQueryType()
.getFieldDefinition(type)
return getField(fieldName, schema.getQueryType().getFieldDefinition(type));
}

private Optional<GraphQLFieldDefinition> getField(String fieldName, GraphQLFieldDefinition fieldDefinition) {
return fieldDefinition
.getType()
.getChildren()
.stream()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.introproventures.graphql.jpa.query.schema.model.book;

import jakarta.persistence.Embeddable;
import jakarta.persistence.Embedded;
import java.io.Serializable;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import lombok.experimental.FieldNameConstants;

@Getter
@Setter
@ToString
@FieldNameConstants
@Embeddable
public class Address implements Serializable {

String street;

String state;

String country;

String city;

@Embedded
Zipcode zipcode;
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import jakarta.persistence.CollectionTable;
import jakarta.persistence.Column;
import jakarta.persistence.ElementCollection;
import jakarta.persistence.Embedded;
import jakarta.persistence.Entity;
import jakarta.persistence.EnumType;
import jakarta.persistence.Enumerated;
Expand All @@ -33,11 +34,13 @@
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import lombok.experimental.FieldNameConstants;

@Entity
@Getter
@Setter
@ToString
@FieldNameConstants
@EqualsAndHashCode(exclude = { "books", "phoneNumbers" }) // Fixes NPE in Hibernate when initializing loaded collections #1
public class Author {

Expand All @@ -57,4 +60,7 @@ public class Author {

@Enumerated(EnumType.STRING)
Genre genre;

@Embedded
Address homeAddress;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.introproventures.graphql.jpa.query.schema.model.book;

import jakarta.persistence.Embeddable;
import java.io.Serializable;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;

@Getter
@Setter
@ToString
@Embeddable
public class Zipcode implements Serializable {

String mainCode;

String codeSuffix;
}