Skip to content

Commit 5dcf533

Browse files
committed
- Added connection to PostgreSQL database
- Added business logic that both graphql and rest will call, in order to do performance testing between them
1 parent c057d44 commit 5dcf533

27 files changed

Lines changed: 873 additions & 82 deletions

build.gradle

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,13 @@ buildscript {
77
}
88
}
99

10-
apply plugin: 'java'
11-
apply plugin: 'eclipse'
12-
apply plugin: 'idea'
10+
plugins {
11+
id 'java'
12+
id 'eclipse'
13+
id 'idea'
14+
id 'net.ltgt.apt' version '0.8'
15+
}
16+
1317
apply plugin: 'org.springframework.boot'
1418

1519
jar {
@@ -27,8 +31,17 @@ targetCompatibility = 1.8
2731
dependencies {
2832
compile 'com.graphql-java:graphql-java:2.2.0'
2933
compile "com.graphql-java:graphql-java-annotations:0.13.1"
34+
compile "postgresql:postgresql:9.1-901-1.jdbc4"
35+
compile "org.hsqldb:hsqldb:2.3.2"
36+
compile "org.json:json:20140107"
37+
compile("org.springframework.boot:spring-boot-starter-data-jpa")
38+
compile("org.springframework.boot:spring-boot-starter-data-rest")
39+
compile 'org.mapstruct:mapstruct-jdk8:1.1.0.Final'
40+
compile "org.mapstruct:mapstruct-processor:1.1.0.Final"
3041
compile("org.springframework.boot:spring-boot-starter-web")
3142
testCompile('org.springframework.boot:spring-boot-starter-test')
3243
testCompile('com.jayway.jsonpath:json-path')
44+
45+
apt 'org.mapstruct:mapstruct-processor:1.1.0.Final'
3346
}
3447

src/main/java/Application.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import org.springframework.boot.SpringApplication;
2+
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
3+
import org.springframework.boot.autoconfigure.SpringBootApplication;
4+
import org.springframework.boot.autoconfigure.domain.EntityScan;
5+
import org.springframework.context.annotation.ComponentScan;
6+
import org.springframework.context.annotation.Configuration;
7+
import org.springframework.context.annotation.aspectj.EnableSpringConfigured;
8+
9+
@Configuration
10+
@EnableAutoConfiguration
11+
@EnableSpringConfigured
12+
@SpringBootApplication
13+
@ComponentScan("configuration")
14+
public class Application {
15+
public static void main(String[] args) {
16+
SpringApplication.run(Application.class, args);
17+
}
18+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package bl;
2+
3+
import domain.Product;
4+
import jpa.ProductRepository;
5+
import mappers.ProductMapper;
6+
import org.springframework.beans.factory.annotation.Autowired;
7+
import org.springframework.stereotype.Service;
8+
9+
import java.util.List;
10+
11+
@Service
12+
public class ProductRetriever {
13+
14+
@Autowired
15+
ProductRepository productRepository;
16+
17+
@Autowired
18+
ProductMapper productMapper;
19+
20+
public List<Product> getProducts()
21+
{
22+
List<jpa.Product> all = productRepository.findAll();
23+
List<Product> products = productMapper.productsToProductsApi(all);
24+
return products;
25+
}
26+
27+
public List<Product> getProductsByStoreId(int store_id)
28+
{
29+
List<jpa.Product> all = productRepository.findByStore_id(store_id);
30+
List<Product> products = productMapper.productsToProductsApi(all);
31+
return products;
32+
}
33+
34+
public Product getProduct(int product_id)
35+
{
36+
jpa.Product all = productRepository.findByid(product_id);
37+
Product product = productMapper.productToProductApi(all);
38+
return product;
39+
}
40+
}
41+
42+
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package bl;
2+
3+
import domain.Store;
4+
import jpa.StoreRepository;
5+
import mappers.StoreMapper;
6+
import org.springframework.beans.factory.annotation.Autowired;
7+
import org.springframework.stereotype.Component;
8+
9+
import java.util.List;
10+
11+
@Component
12+
public class StoreRetriever {
13+
@Autowired
14+
StoreRepository storeRepository;
15+
16+
@Autowired
17+
StoreMapper storeMapper;
18+
19+
public List<Store> getStores() {
20+
List<jpa.Store> all = storeRepository.findAll();
21+
List<Store> stores = storeMapper.storesToStoresApi(all);
22+
return stores;
23+
}
24+
25+
public Store getStore(int storeId) {
26+
jpa.Store all = storeRepository.findByid(storeId);
27+
Store store = storeMapper.storeToStoreApi(all);
28+
return store;
29+
}
30+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package configuration;
2+
3+
import org.springframework.context.annotation.ComponentScan;
4+
import org.springframework.context.annotation.Configuration;
5+
6+
@Configuration
7+
@ComponentScan
8+
@ComponentScan(basePackages = {"graphql", "bl", "domain", "mappers"})
9+
public class AppConfig {
10+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package configuration;
2+
3+
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
4+
import org.springframework.context.annotation.ComponentScan;
5+
import org.springframework.context.annotation.Configuration;
6+
7+
@Configuration
8+
@EnableAutoConfiguration
9+
@ComponentScan(basePackages = {"endpoints"})
10+
public class MvcConfig {
11+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package configuration;
2+
3+
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
4+
import org.springframework.boot.autoconfigure.domain.EntityScan;
5+
import org.springframework.context.annotation.Configuration;
6+
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
7+
8+
@Configuration
9+
@EnableJpaRepositories(basePackages = "jpa")
10+
@EnableAutoConfiguration
11+
@EntityScan(basePackages = {"jpa"})
12+
public class RepositoryConfig {
13+
}

src/main/java/domain/Product.java

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
package domain;
2+
3+
import graphql.fetchers.ProductStoreFetcher;
4+
import graphql.annotations.GraphQLDataFetcher;
5+
import graphql.annotations.GraphQLField;
6+
import graphql.annotations.GraphQLName;
7+
import graphql.annotations.GraphQLType;
8+
9+
@GraphQLType
10+
public class Product {
11+
@GraphQLField
12+
@GraphQLName("product_id")
13+
private int id;
14+
15+
@GraphQLField
16+
private int store_id;
17+
18+
@GraphQLField
19+
@GraphQLDataFetcher(ProductStoreFetcher.class)
20+
private Store store;
21+
22+
@GraphQLField
23+
private String name;
24+
25+
@GraphQLField
26+
private String description;
27+
28+
@GraphQLField
29+
private double price;
30+
31+
@GraphQLField
32+
private double score;
33+
34+
public int getId() {
35+
return id;
36+
}
37+
38+
public void setId(int id) {
39+
this.id = id;
40+
}
41+
42+
public String getName() {
43+
return name;
44+
}
45+
46+
public void setName(String name) {
47+
this.name = name;
48+
}
49+
50+
public String getDescription() {
51+
return description;
52+
}
53+
54+
public void setDescription(String description) {
55+
this.description = description;
56+
}
57+
58+
public double getPrice() {
59+
return price;
60+
}
61+
62+
public void setPrice(double price) {
63+
this.price = price;
64+
}
65+
66+
public double getScore() {
67+
return score;
68+
}
69+
70+
public void setScore(double score) {
71+
this.score = score;
72+
}
73+
74+
public Store getStore() {
75+
return store;
76+
}
77+
78+
public void setStore(Store store) {
79+
this.store = store;
80+
}
81+
82+
public int getStore_id() {
83+
return store_id;
84+
}
85+
86+
public void setStore_id(int store_id) {
87+
this.store_id = store_id;
88+
}
89+
90+
91+
}

src/main/java/domain/Store.java

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
package domain;
2+
3+
import graphql.fetchers.StoreProductsFetcher;
4+
import graphql.annotations.GraphQLDataFetcher;
5+
import graphql.annotations.GraphQLField;
6+
import graphql.annotations.GraphQLType;
7+
8+
import java.util.List;
9+
10+
@GraphQLType
11+
public class Store {
12+
@GraphQLField
13+
private int id;
14+
15+
@GraphQLField
16+
private String name;
17+
18+
@GraphQLField
19+
private String address;
20+
21+
@GraphQLField
22+
private int owner;
23+
24+
@GraphQLField
25+
private int creation_date;
26+
27+
@GraphQLField
28+
@GraphQLDataFetcher(StoreProductsFetcher.class)
29+
private List<Product> products;
30+
31+
public int getId() {
32+
return id;
33+
}
34+
35+
public void setId(int id) {
36+
this.id = id;
37+
}
38+
39+
public String getName() {
40+
return name;
41+
}
42+
43+
public void setName(String name) {
44+
this.name = name;
45+
}
46+
47+
public String getAddress() {
48+
return address;
49+
}
50+
51+
public void setAddress(String address) {
52+
this.address = address;
53+
}
54+
55+
public int getOwner() {
56+
return owner;
57+
}
58+
59+
public void setOwner(int owner) {
60+
this.owner = owner;
61+
}
62+
63+
public int getCreation_date() {
64+
return creation_date;
65+
}
66+
67+
public void setCreation_date(int creation_date) {
68+
this.creation_date = creation_date;
69+
}
70+
71+
72+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package endpoints;
2+
3+
import graphql.RepositorySchema;
4+
import org.json.JSONObject;
5+
import org.springframework.beans.factory.annotation.Autowired;
6+
import org.springframework.web.bind.annotation.*;
7+
8+
@RestController
9+
@CrossOrigin(origins = "*", maxAge = 3600)
10+
public class GraphQLController {
11+
12+
@Autowired
13+
private RepositorySchema repositorySchema;
14+
15+
@RequestMapping(value = "/graphql", method = RequestMethod.POST)
16+
public Object graphql(@RequestBody String input) throws IllegalAccessException, NoSuchMethodException, InstantiationException {
17+
String queryString = getQueryString(input);
18+
Object execute = repositorySchema.execute(queryString);
19+
return execute;
20+
}
21+
22+
private static String getQueryString(String input) {
23+
JSONObject requestQuery = new JSONObject(input);
24+
return requestQuery.getString("query");
25+
}
26+
}

0 commit comments

Comments
 (0)