Skip to content

Commit 866a5ec

Browse files
authored
feat(GH-224): Added support for entity path class name prefix in GraphQLJpaSchemaBuilder (#245)
* feat(GH-224): Added support for entity path prefix schema builder * fix: Polish test * fix: Corrected failing tests
1 parent e60eb50 commit 866a5ec

File tree

2 files changed

+39
-4
lines changed

2 files changed

+39
-4
lines changed

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

+13-3
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,8 @@ public class GraphQLJpaSchemaBuilder implements GraphQLSchemaBuilder {
131131

132132
private final Relay relay = new Relay();
133133

134+
private final List<String> entityPaths = new ArrayList<>();
135+
134136
public GraphQLJpaSchemaBuilder(EntityManager entityManager) {
135137
this.entityManager = entityManager;
136138
}
@@ -1064,7 +1066,12 @@ private boolean isIdentity(Attribute<?,?> attribute) {
10641066
}
10651067

10661068
private boolean isNotIgnored(EntityType<?> entityType) {
1067-
return isNotIgnored(entityType.getJavaType());
1069+
return isNotIgnored(entityType.getJavaType()) && isNotIgnored(entityType.getJavaType().getName());
1070+
}
1071+
1072+
private boolean isNotIgnored(String name) {
1073+
return entityPaths.isEmpty() || entityPaths.stream()
1074+
.anyMatch(prefix -> name.startsWith(prefix));
10681075
}
10691076

10701077
private boolean isNotIgnored(Member member) {
@@ -1276,13 +1283,16 @@ public Object parseLiteral(Object input) {
12761283
}
12771284

12781285
@Override
1279-
public GraphQLSchemaBuilder entityPath(String path) {
1286+
public GraphQLJpaSchemaBuilder entityPath(String path) {
12801287
Assert.assertNotNull(path, "path is null");
1288+
1289+
entityPaths.add(path);
1290+
12811291
return this;
12821292
}
12831293

12841294
@Override
1285-
public GraphQLSchemaBuilder namingStrategy(NamingStrategy instance) {
1295+
public GraphQLJpaSchemaBuilder namingStrategy(NamingStrategy instance) {
12861296
Assert.assertNotNull(instance, "instance is null");
12871297

12881298
this.namingStrategy = instance;

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

+26-1
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,11 @@
3333

3434
import com.introproventures.graphql.jpa.query.AbstractSpringBootTestSupport;
3535
import com.introproventures.graphql.jpa.query.schema.impl.GraphQLJpaSchemaBuilder;
36+
import com.introproventures.graphql.jpa.query.schema.model.book.Author;
37+
import com.introproventures.graphql.jpa.query.schema.model.book.Book;
38+
import com.introproventures.graphql.jpa.query.schema.model.book_superclass.SuperAuthor;
39+
import com.introproventures.graphql.jpa.query.schema.model.book_superclass.SuperBook;
40+
import com.introproventures.graphql.jpa.query.schema.model.uuid.Thing;
3641

3742
import graphql.schema.GraphQLFieldDefinition;
3843
import graphql.schema.GraphQLList;
@@ -44,9 +49,14 @@ public class BooksSchemaBuildTest extends AbstractSpringBootTestSupport {
4449
@SpringBootApplication
4550
static class TestConfiguration {
4651
@Bean
47-
public GraphQLJpaSchemaBuilder graphQLSchemaBuilder(EntityManager entityManager) {
52+
public GraphQLSchemaBuilder graphQLSchemaBuilder(EntityManager entityManager) {
4853
return new GraphQLJpaSchemaBuilder(entityManager)
4954
.name("BooksExampleSchema")
55+
.entityPath(Book.class.getName())
56+
.entityPath(SuperBook.class.getName())
57+
.entityPath(SuperAuthor.class.getName())
58+
.entityPath(Author.class.getName())
59+
.entityPath(Thing.class.getPackage().getName())
5060
.description("Books Example Schema");
5161
}
5262
}
@@ -88,6 +98,21 @@ public void correctlyDerivesSchemaFromGivenEntities() {
8898
.isNotNull();
8999
}
90100

101+
@Test
102+
public void correctlyBuildsSchemaForEntitiesWithEntityPath() {
103+
//when
104+
GraphQLSchema schema = builder.build();
105+
106+
// then
107+
assertThat(schema)
108+
.describedAs("Ensure the result is returned")
109+
.isNotNull();
110+
111+
assertThat(schema.getQueryType()
112+
.getFieldDefinitions()).extracting(GraphQLFieldDefinition::getName)
113+
.containsOnly("Book", "Books", "Author", "Authors", "Thing", "Things", "SuperBook", "SuperBooks", "SuperAuthor", "SuperAuthors");
114+
}
115+
91116
@Test
92117
public void correctlyDerivesToManyOptionalFromGivenEntities() {
93118
//when

0 commit comments

Comments
 (0)