Skip to content

Commit 12caf3a

Browse files
authored
feat: Created example to merge two JPA data sources into one schema (#79)
1 parent ca18221 commit 12caf3a

File tree

22 files changed

+42232
-17
lines changed

22 files changed

+42232
-17
lines changed
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
version: "3.2"
2+
services:
3+
gpaphql-jpa-query-example-merge:
4+
volumes:
5+
- "/var/run/docker.sock:/var/run/docker.sock"
6+
ports:
7+
- 8080:8080
8+
image: introproventures/graphql-jpa-query-example-merge:latest
9+
networks:
10+
- application
11+
networks:
12+
application:
13+
driver: overlay
Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
2+
<modelVersion>4.0.0</modelVersion>
3+
4+
<artifactId>graphql-jpa-query-example-merge</artifactId>
5+
<name>graphql-jpa-query-example-merge</name>
6+
7+
<parent>
8+
<groupId>com.introproventures</groupId>
9+
<artifactId>graphql-jpa-query-build</artifactId>
10+
<version>0.3.14-SNAPSHOT</version>
11+
<relativePath>../graphql-jpa-query-build</relativePath>
12+
</parent>
13+
14+
<properties>
15+
<maven.deploy.skip>true</maven.deploy.skip>
16+
</properties>
17+
18+
<dependencies>
19+
20+
<dependency>
21+
<groupId>com.introproventures</groupId>
22+
<artifactId>graphql-jpa-query-web</artifactId>
23+
</dependency>
24+
25+
<dependency>
26+
<groupId>com.introproventures</groupId>
27+
<artifactId>graphql-jpa-query-schema</artifactId>
28+
</dependency>
29+
30+
<dependency>
31+
<groupId>com.introproventures</groupId>
32+
<artifactId>graphql-jpa-query-autoconfigure</artifactId>
33+
</dependency>
34+
35+
<dependency>
36+
<groupId>com.introproventures</groupId>
37+
<artifactId>graphql-jpa-query-autoconfigure</artifactId>
38+
</dependency>
39+
40+
<dependency>
41+
<groupId>org.springframework.boot</groupId>
42+
<artifactId>spring-boot-starter-web</artifactId>
43+
</dependency>
44+
45+
<dependency>
46+
<groupId>org.springframework.boot</groupId>
47+
<artifactId>spring-boot-starter-data-jpa</artifactId>
48+
</dependency>
49+
50+
<dependency>
51+
<groupId>com.h2database</groupId>
52+
<artifactId>h2</artifactId>
53+
</dependency>
54+
55+
<dependency>
56+
<groupId>org.projectlombok</groupId>
57+
<artifactId>lombok</artifactId>
58+
</dependency>
59+
60+
</dependencies>
61+
62+
<build>
63+
64+
<finalName>${project.artifactId}</finalName>
65+
66+
<plugins>
67+
<plugin>
68+
<groupId>org.springframework.boot</groupId>
69+
<artifactId>spring-boot-maven-plugin</artifactId>
70+
<version>1.5.6.RELEASE</version>
71+
<executions>
72+
<execution>
73+
<goals>
74+
<goal>repackage</goal>
75+
</goals>
76+
</execution>
77+
</executions>
78+
</plugin>
79+
<plugin>
80+
<groupId>com.spotify</groupId>
81+
<artifactId>docker-maven-plugin</artifactId>
82+
<version>0.4.14</version>
83+
<dependencies>
84+
<dependency>
85+
<groupId>javax.activation</groupId>
86+
<artifactId>activation</artifactId>
87+
<version>1.1.1</version>
88+
</dependency>
89+
</dependencies>
90+
<executions>
91+
<execution>
92+
<id>build-image</id>
93+
<phase>package</phase>
94+
<goals>
95+
<goal>build</goal>
96+
</goals>
97+
</execution>
98+
<execution>
99+
<id>push-image</id>
100+
<phase>deploy</phase>
101+
<goals>
102+
<goal>push</goal>
103+
</goals>
104+
<configuration>
105+
<pushImage>true</pushImage>
106+
</configuration>
107+
</execution>
108+
</executions>
109+
<configuration>
110+
<serverId>docker-hub</serverId>
111+
<registryUrl>https://index.docker.io/v1/</registryUrl>
112+
<imageName>introproventures/${project.artifactId}</imageName>
113+
<dockerDirectory>${project.basedir}/src/main/docker</dockerDirectory>
114+
<resources>
115+
<resource>
116+
<targetPath>/</targetPath>
117+
<directory>${project.build.directory}</directory>
118+
<include>${project.build.finalName}.jar</include>
119+
</resource>
120+
</resources>
121+
122+
<!-- optionally overwrite tags every time image is built with docker:build -->
123+
<forceTags>true</forceTags>
124+
<imageTags>
125+
<imageTag>${project.version}</imageTag>
126+
<imageTag>latest</imageTag>
127+
</imageTags>
128+
129+
</configuration>
130+
131+
</plugin>
132+
133+
</plugins>
134+
</build>
135+
</project>
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
FROM openjdk:8-jdk-alpine
2+
3+
VOLUME /var/log/
4+
VOLUME /tmp
5+
6+
EXPOSE 8080
7+
8+
ADD graphql-jpa-query-example-merge.jar app.jar
9+
10+
ENV JAVA_OPTS=""
11+
12+
ENTRYPOINT exec java $JAVA_OPTS -Djava.security.egd=file:/dev/./urandom -jar /app.jar
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
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;
17+
18+
import com.introproventures.graphql.jpa.query.schema.GraphQLExecutor;
19+
import com.introproventures.graphql.jpa.query.schema.impl.GraphQLJpaExecutor;
20+
import graphql.schema.GraphQLSchema;
21+
import org.springframework.boot.SpringApplication;
22+
import org.springframework.boot.autoconfigure.SpringBootApplication;
23+
import org.springframework.context.annotation.Bean;
24+
import org.springframework.transaction.annotation.EnableTransactionManagement;
25+
26+
/**
27+
* GraphQL JPA Query Example with Spring Boot Autoconfiguration
28+
*
29+
* You can configure GraphQL JPA Query properties in application.yml
30+
*
31+
* @author Igor Dianov
32+
*
33+
*/
34+
@SpringBootApplication
35+
@EnableTransactionManagement
36+
public class Application {
37+
38+
public static void main(String[] args) {
39+
SpringApplication.run(Application.class, args);
40+
}
41+
42+
@Bean
43+
public GraphQLExecutor graphQLExecutor(GraphQLSchema graphQLSchema) {
44+
return new GraphQLJpaExecutor(graphQLSchema);
45+
}
46+
47+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
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+
17+
package com.introproventures.graphql.jpa.query.example.books;
18+
19+
import java.util.Collection;
20+
21+
import javax.persistence.Entity;
22+
import javax.persistence.Id;
23+
import javax.persistence.OneToMany;
24+
25+
import lombok.Data;
26+
27+
@Data
28+
@Entity
29+
public class Author {
30+
@Id
31+
Long id;
32+
33+
String name;
34+
35+
@OneToMany(mappedBy="author")
36+
Collection<Book> books;
37+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
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+
17+
package com.introproventures.graphql.jpa.query.example.books;
18+
19+
import javax.persistence.Entity;
20+
import javax.persistence.EnumType;
21+
import javax.persistence.Enumerated;
22+
import javax.persistence.Id;
23+
import javax.persistence.ManyToOne;
24+
25+
import lombok.Data;
26+
27+
@Data
28+
@Entity
29+
public class Book {
30+
@Id
31+
Long id;
32+
33+
String title;
34+
35+
@ManyToOne
36+
Author author;
37+
38+
@Enumerated(EnumType.STRING)
39+
Genre genre;
40+
}
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
package com.introproventures.graphql.jpa.query.example.books;
2+
3+
import java.util.HashMap;
4+
import java.util.Map;
5+
6+
import javax.persistence.EntityManager;
7+
import javax.persistence.EntityManagerFactory;
8+
import javax.sql.DataSource;
9+
10+
import com.introproventures.graphql.jpa.query.autoconfigure.GraphQLSchemaConfigurer;
11+
import com.introproventures.graphql.jpa.query.autoconfigure.GraphQLShemaRegistration;
12+
import com.introproventures.graphql.jpa.query.schema.impl.GraphQLJpaSchemaBuilder;
13+
import org.hibernate.cfg.AvailableSettings;
14+
import org.hibernate.dialect.H2Dialect;
15+
import org.springframework.beans.factory.annotation.Qualifier;
16+
import org.springframework.boot.context.properties.ConfigurationProperties;
17+
import org.springframework.boot.jdbc.DataSourceBuilder;
18+
import org.springframework.boot.orm.jpa.EntityManagerFactoryBuilder;
19+
import org.springframework.context.annotation.Bean;
20+
import org.springframework.context.annotation.Configuration;
21+
import org.springframework.core.io.DefaultResourceLoader;
22+
import org.springframework.core.io.ResourceLoader;
23+
import org.springframework.jdbc.datasource.init.DataSourceInitializer;
24+
import org.springframework.jdbc.datasource.init.ResourceDatabasePopulator;
25+
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
26+
27+
@Configuration
28+
public class BooksSchemaConfiguration {
29+
30+
@Bean
31+
@ConfigurationProperties(prefix = "books")
32+
@Qualifier("bookDataSource")
33+
public DataSource bookDataSource() {
34+
return DataSourceBuilder.create()
35+
.build();
36+
}
37+
38+
@Bean
39+
@Qualifier("bookEntityManager")
40+
public LocalContainerEntityManagerFactoryBean bookEntityManagerFactory(
41+
EntityManagerFactoryBuilder builder) {
42+
Map<String, Object> properties = new HashMap<>();
43+
properties.put(AvailableSettings.HBM2DDL_AUTO, "create-drop");
44+
properties.put(AvailableSettings.HBM2DLL_CREATE_SCHEMAS, "true");
45+
properties.put(AvailableSettings.DIALECT, H2Dialect.class.getName());
46+
properties.put(AvailableSettings.SHOW_SQL, "true");
47+
properties.put(AvailableSettings.FORMAT_SQL, "true");
48+
49+
return builder
50+
.dataSource(bookDataSource())
51+
.packages(Book.class)
52+
.persistenceUnit("books")
53+
.properties(properties)
54+
.build();
55+
}
56+
57+
@Bean
58+
public DataSourceInitializer booksDataSourceInitializer(@Qualifier("bookDataSource") DataSource bookDataSource) {
59+
DataSourceInitializer dataSourceInitializer = new DataSourceInitializer();
60+
ResourceLoader resourceLoader = new DefaultResourceLoader();
61+
62+
ResourceDatabasePopulator databasePopulator = new ResourceDatabasePopulator();
63+
databasePopulator.addScript(resourceLoader.getResource("books.sql"));
64+
65+
dataSourceInitializer.setDataSource(bookDataSource);
66+
dataSourceInitializer.setDatabasePopulator(databasePopulator);
67+
68+
return dataSourceInitializer;
69+
}
70+
71+
72+
@Configuration
73+
public static class GraphQLJpaQuerySchemaConfigurer implements GraphQLSchemaConfigurer {
74+
75+
private final EntityManager entityManager;
76+
77+
public GraphQLJpaQuerySchemaConfigurer(@Qualifier("bookEntityManager") EntityManagerFactory entityManager) {
78+
this.entityManager = entityManager.createEntityManager();
79+
}
80+
81+
@Override
82+
public void configure(GraphQLShemaRegistration registry) {
83+
84+
registry.register(new GraphQLJpaSchemaBuilder(entityManager).name("GraphQLBooks").build());
85+
}
86+
}
87+
88+
}

0 commit comments

Comments
 (0)