Skip to content

Commit 36703f0

Browse files
authored
feat(GH-235): added Relay Connection support (#236)
* feat(GH-235): added Relay Connection support * feat: Added Relay example Spring Boot test
1 parent 3db0e51 commit 36703f0

File tree

26 files changed

+1541
-633
lines changed

26 files changed

+1541
-633
lines changed

graphql-jpa-query-autoconfigure/src/main/java/com/introproventures/graphql/jpa/query/autoconfigure/GraphQLJpaQueryProperties.java

+15
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,11 @@ public class GraphQLJpaQueryProperties {
5050
* Set default value for optional argument for join fetch collections.
5151
*/
5252
private boolean toManyDefaultOptional = true;
53+
54+
/**
55+
* Enable or disable GraphQL Relay Connection support. Default is false
56+
*/
57+
private boolean enableRelay = false;
5358

5459
/**
5560
* Enable or disable QraphQL module services.
@@ -158,5 +163,15 @@ public boolean isToManyDefaultOptional() {
158163
public void setToManyDefaultOptional(boolean toManyDefaultOptional) {
159164
this.toManyDefaultOptional = toManyDefaultOptional;
160165
}
166+
167+
168+
public boolean isEnableRelay() {
169+
return enableRelay;
170+
}
171+
172+
173+
public void setEnableRelay(boolean enableRelay) {
174+
this.enableRelay = enableRelay;
175+
}
161176

162177
}

graphql-jpa-query-boot-starter/src/main/java/com/introproventures/graphql/jpa/query/boot/autoconfigure/GraphQLJpaQueryAutoConfiguration.java

+16-14
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,16 @@
1616
package com.introproventures.graphql.jpa.query.boot.autoconfigure;
1717

1818
import javax.persistence.EntityManager;
19+
import javax.persistence.EntityManagerFactory;
1920

2021
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
2122
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
2223
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
24+
import org.springframework.boot.autoconfigure.condition.ConditionalOnSingleCandidate;
2325
import org.springframework.context.annotation.Bean;
2426
import org.springframework.context.annotation.Configuration;
2527

2628
import com.introproventures.graphql.jpa.query.autoconfigure.GraphQLSchemaConfigurer;
27-
import com.introproventures.graphql.jpa.query.autoconfigure.GraphQLShemaRegistration;
2829
import com.introproventures.graphql.jpa.query.schema.GraphQLExecutor;
2930
import com.introproventures.graphql.jpa.query.schema.GraphQLSchemaBuilder;
3031
import com.introproventures.graphql.jpa.query.schema.impl.GraphQLJpaExecutor;
@@ -38,21 +39,22 @@
3839
@ConditionalOnProperty(name="spring.graphql.jpa.query.enabled", havingValue="true", matchIfMissing=true)
3940
public class GraphQLJpaQueryAutoConfiguration {
4041

41-
@Configuration
42-
public static class GraphQLJpaQuerySchemaConfigurer implements GraphQLSchemaConfigurer {
43-
44-
private final GraphQLSchemaBuilder graphQLSchemaBuilder;
45-
46-
public GraphQLJpaQuerySchemaConfigurer(GraphQLSchemaBuilder graphQLSchemaBuilder) {
47-
this.graphQLSchemaBuilder = graphQLSchemaBuilder;
48-
}
49-
50-
@Override
51-
public void configure(GraphQLShemaRegistration registry) {
52-
registry.register(graphQLSchemaBuilder.build());
53-
}
42+
@Bean
43+
@ConditionalOnMissingBean
44+
@ConditionalOnSingleCandidate(EntityManagerFactory.class)
45+
public GraphQLSchemaBuilder graphQLSchemaBuilder(final EntityManagerFactory entityManagerFactory) {
46+
return new GraphQLJpaSchemaBuilder(entityManagerFactory.createEntityManager());
5447
}
5548

49+
@Bean
50+
@ConditionalOnMissingBean
51+
public GraphQLSchemaConfigurer graphQLJpaQuerySchemaConfigurer(GraphQLSchemaBuilder graphQLSchemaBuilder) {
52+
53+
return (registry) -> {
54+
registry.register(graphQLSchemaBuilder.build());
55+
};
56+
}
57+
5658
@Configuration
5759
public static class DefaultGraphQLJpaQueryConfiguration {
5860

+67
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0"
2+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
6+
<artifactId>graphql-jpa-query-example-relay</artifactId>
7+
8+
<parent>
9+
<groupId>com.introproventures</groupId>
10+
<artifactId>graphql-jpa-query-build</artifactId>
11+
<version>0.3.38-SNAPSHOT</version>
12+
<relativePath>../graphql-jpa-query-build</relativePath>
13+
</parent>
14+
15+
<properties>
16+
<maven.deploy.skip>true</maven.deploy.skip>
17+
<skipDocker>false</skipDocker>
18+
</properties>
19+
20+
<dependencies>
21+
22+
<dependency>
23+
<groupId>com.introproventures</groupId>
24+
<artifactId>graphql-jpa-query-boot-starter</artifactId>
25+
</dependency>
26+
27+
<dependency>
28+
<groupId>com.introproventures</groupId>
29+
<artifactId>graphql-jpa-query-graphiql</artifactId>
30+
</dependency>
31+
32+
<dependency>
33+
<groupId>com.introproventures</groupId>
34+
<artifactId>graphql-jpa-query-example-model-books</artifactId>
35+
</dependency>
36+
37+
<dependency>
38+
<groupId>com.h2database</groupId>
39+
<artifactId>h2</artifactId>
40+
</dependency>
41+
42+
<dependency>
43+
<groupId>org.projectlombok</groupId>
44+
<artifactId>lombok</artifactId>
45+
</dependency>
46+
47+
</dependencies>
48+
49+
<build>
50+
<finalName>${project.artifactId}</finalName>
51+
<plugins>
52+
<plugin>
53+
<groupId>org.springframework.boot</groupId>
54+
<artifactId>spring-boot-maven-plugin</artifactId>
55+
<version>1.5.6.RELEASE</version>
56+
<executions>
57+
<execution>
58+
<goals>
59+
<goal>repackage</goal>
60+
</goals>
61+
</execution>
62+
</executions>
63+
</plugin>
64+
</plugins>
65+
</build>
66+
67+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
/*
2+
* Copyright 2017 IntroPro Ventures, Inc. and/or its affiliates.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.introproventures.graphql.jpa.query.example.relay;
17+
18+
import javax.persistence.EntityManager;
19+
20+
import org.springframework.beans.factory.annotation.Autowired;
21+
import org.springframework.boot.SpringApplication;
22+
import org.springframework.boot.autoconfigure.SpringBootApplication;
23+
import org.springframework.boot.autoconfigure.domain.EntityScan;
24+
import org.springframework.context.annotation.Configuration;
25+
26+
import com.introproventures.graphql.jpa.query.autoconfigure.GraphQLJpaQueryProperties;
27+
import com.introproventures.graphql.jpa.query.autoconfigure.GraphQLSchemaConfigurer;
28+
import com.introproventures.graphql.jpa.query.autoconfigure.GraphQLShemaRegistration;
29+
import com.introproventures.graphql.jpa.query.schema.impl.GraphQLJpaSchemaBuilder;
30+
import com.introproventures.graphql.jpa.query.schema.model.book.Book;
31+
32+
/**
33+
* GraphQL JPA Query Example Relay with Spring Boot Autoconfiguration
34+
*
35+
* You can configure GraphQL JPA Query properties in application.yml
36+
*
37+
* @author Igor Dianov
38+
*
39+
*/
40+
@SpringBootApplication
41+
@EntityScan(basePackageClasses=Book.class)
42+
public class Application {
43+
44+
public static void main(String[] args) {
45+
SpringApplication.run(Application.class, args);
46+
}
47+
48+
@Configuration
49+
public static class GraphQLJpaQuerySchemaConfigurer implements GraphQLSchemaConfigurer {
50+
51+
private final EntityManager entityManager;
52+
53+
@Autowired
54+
private GraphQLJpaQueryProperties properties;
55+
56+
public GraphQLJpaQuerySchemaConfigurer(EntityManager entityManager) {
57+
this.entityManager = entityManager;
58+
}
59+
60+
@Override
61+
public void configure(GraphQLShemaRegistration registry) {
62+
registry.register(
63+
new GraphQLJpaSchemaBuilder(entityManager)
64+
.name(properties.getName())
65+
.description(properties.getDescription())
66+
.useDistinctParameter(properties.isUseDistinctParameter())
67+
.defaultDistinct(properties.isDefaultDistinct())
68+
.enableRelay(properties.isEnableRelay())
69+
.build()
70+
);
71+
}
72+
}
73+
74+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
spring:
2+
jpa:
3+
hibernate.ddl-auto: create-drop
4+
generate-ddl: true
5+
show-sql: true
6+
open-in-view: false
7+
properties:
8+
hibernate.format_sql: true
9+
h2:
10+
console.enabled: true
11+
12+
graphql:
13+
jpa:
14+
query:
15+
name: BooksRelay
16+
description: GraphQL Jpa Query Books Relay Example
17+
enable-relay: true
18+
19+
logging:
20+
level:
21+
com.introproventures.graphql.jpa.query.schema: DEBUG
22+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
-- Books
2+
insert into author (id, name, genre) values (1, 'Leo Tolstoy', 'NOVEL');
3+
insert into book (id, title, author_id, genre, publication_date, description)
4+
values (2, 'War and Peace', 1, 'NOVEL', '1869-01-01', 'The novel chronicles the history of the French invasion of Russia and the impact of the Napoleonic era on Tsarist society through the stories of five Russian aristocratic families.');
5+
insert into book (id, title, author_id, genre, publication_date, description)
6+
values (3, 'Anna Karenina', 1, 'NOVEL', '1877-04-01', 'A complex novel in eight parts, with more than a dozen major characters, it is spread over more than 800 pages (depending on the translation), typically contained in two volumes.');
7+
insert into author (id, name, genre) values (4, 'Anton Chekhov', 'PLAY');
8+
insert into book (id, title, author_id, genre, publication_date, description)
9+
values (5, 'The Cherry Orchard', 4, 'PLAY', '1904-01-17', 'The play concerns an aristocratic Russian landowner who returns to her family estate (which includes a large and well-known cherry orchard) just before it is auctioned to pay the mortgage.');
10+
insert into book (id, title, author_id, genre, publication_date, description)
11+
values (6, 'The Seagull', 4, 'PLAY', '1896-10-17', 'It dramatises the romantic and artistic conflicts between four characters');
12+
insert into book (id, title, author_id, genre, publication_date, description)
13+
values (7, 'Three Sisters', 4, 'PLAY', '1900-01-01', 'The play is sometimes included on the short list of Chekhov''s outstanding plays, along with The Cherry Orchard, The Seagull and Uncle Vanya.[1]');
14+
insert into author (id, name, genre) values (8, 'Igor Dianov', 'JAVA');
15+
16+
insert into book_tags (book_id, tags) values (2, 'war'), (2, 'piece');
17+
insert into book_tags (book_id, tags) values (3, 'anna'), (3, 'karenina');
18+
insert into book_tags (book_id, tags) values (5, 'cherry'), (5, 'orchard');
19+
insert into book_tags (book_id, tags) values (6, 'seagull');
20+
insert into book_tags (book_id, tags) values (7, 'three'), (7, 'sisters');
21+
22+
insert into author_phone_numbers(phone_number, author_id) values
23+
('1-123-1234', 1),
24+
('1-123-5678', 1),
25+
('4-123-1234', 4),
26+
('4-123-5678', 4);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.introproventures.graphql.jpa.query.example.relay;
2+
3+
import org.junit.Test;
4+
import org.junit.runner.RunWith;
5+
import org.springframework.boot.test.context.SpringBootTest;
6+
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
7+
import org.springframework.test.context.junit4.SpringRunner;
8+
9+
@RunWith(SpringRunner.class)
10+
@SpringBootTest(webEnvironment=WebEnvironment.RANDOM_PORT)
11+
public class ApplicationTest {
12+
13+
@Test
14+
public void contextLoads() {
15+
// success
16+
}
17+
18+
}

0 commit comments

Comments
 (0)