From f4be8916b8f8ecb739092f183ef6ef5a926556a5 Mon Sep 17 00:00:00 2001 From: Haroon Khan Date: Wed, 12 Oct 2022 16:38:39 +0100 Subject: [PATCH] [JAVA-15353] Update code for GraphQL vs REST article (#12849) --- spring-boot-modules/pom.xml | 1 - .../spring-boot-graphql/README.md | 1 + .../GraphqlVsRestApplication.java | 10 ++-- .../configuration/GraphqlConfiguration.java | 8 +++ .../controller/OrderController.java | 9 ++-- .../controller/ProductController.java | 16 ++++-- .../controller/ProductGraphQLController.java | 51 +++++++++++++++++++ .../baeldung/graphqlvsrest/entity/Order.java | 30 +++++------ .../graphqlvsrest/entity/Product.java | 36 ++++++------- .../graphqlvsrest/model/ProductModel.java | 24 ++++----- .../repository/OrderRepository.java | 2 - .../repository/ProductRepository.java | 0 .../repository/impl/OrderRepositoryImpl.java | 19 +++---- .../impl/ProductRepositoryImpl.java | 43 +++++++++------- .../resources/application-rest-vs-graphql.yml | 11 ++++ .../graphql-vs-rest}/schema.graphqls | 20 ++++---- .../README.md | 7 --- .../spring-boot-libraries-comparison/pom.xml | 46 ----------------- .../configuration/GraphqlConfiguration.java | 35 ------------- .../graphqlvsrest/resolver/Mutation.java | 22 -------- .../resolver/ProductResolver.java | 18 ------- .../graphqlvsrest/resolver/Query.java | 27 ---------- .../src/main/resources/application.properties | 0 23 files changed, 182 insertions(+), 254 deletions(-) rename spring-boot-modules/{spring-boot-libraries-comparison => spring-boot-graphql}/src/main/java/com/baeldung/graphqlvsrest/GraphqlVsRestApplication.java (64%) create mode 100644 spring-boot-modules/spring-boot-graphql/src/main/java/com/baeldung/graphqlvsrest/configuration/GraphqlConfiguration.java rename spring-boot-modules/{spring-boot-libraries-comparison => spring-boot-graphql}/src/main/java/com/baeldung/graphqlvsrest/controller/OrderController.java (66%) rename spring-boot-modules/{spring-boot-libraries-comparison => spring-boot-graphql}/src/main/java/com/baeldung/graphqlvsrest/controller/ProductController.java (58%) create mode 100644 spring-boot-modules/spring-boot-graphql/src/main/java/com/baeldung/graphqlvsrest/controller/ProductGraphQLController.java rename spring-boot-modules/{spring-boot-libraries-comparison => spring-boot-graphql}/src/main/java/com/baeldung/graphqlvsrest/entity/Order.java (50%) rename spring-boot-modules/{spring-boot-libraries-comparison => spring-boot-graphql}/src/main/java/com/baeldung/graphqlvsrest/entity/Product.java (70%) rename spring-boot-modules/{spring-boot-libraries-comparison => spring-boot-graphql}/src/main/java/com/baeldung/graphqlvsrest/model/ProductModel.java (76%) rename spring-boot-modules/{spring-boot-libraries-comparison => spring-boot-graphql}/src/main/java/com/baeldung/graphqlvsrest/repository/OrderRepository.java (67%) rename spring-boot-modules/{spring-boot-libraries-comparison => spring-boot-graphql}/src/main/java/com/baeldung/graphqlvsrest/repository/ProductRepository.java (100%) rename spring-boot-modules/{spring-boot-libraries-comparison => spring-boot-graphql}/src/main/java/com/baeldung/graphqlvsrest/repository/impl/OrderRepositoryImpl.java (56%) rename spring-boot-modules/{spring-boot-libraries-comparison => spring-boot-graphql}/src/main/java/com/baeldung/graphqlvsrest/repository/impl/ProductRepositoryImpl.java (63%) create mode 100644 spring-boot-modules/spring-boot-graphql/src/main/resources/application-rest-vs-graphql.yml rename spring-boot-modules/{spring-boot-libraries-comparison/src/main/resources/graphql => spring-boot-graphql/src/main/resources/graphql-vs-rest}/schema.graphqls (77%) delete mode 100644 spring-boot-modules/spring-boot-libraries-comparison/README.md delete mode 100644 spring-boot-modules/spring-boot-libraries-comparison/pom.xml delete mode 100644 spring-boot-modules/spring-boot-libraries-comparison/src/main/java/com/baeldung/graphqlvsrest/configuration/GraphqlConfiguration.java delete mode 100644 spring-boot-modules/spring-boot-libraries-comparison/src/main/java/com/baeldung/graphqlvsrest/resolver/Mutation.java delete mode 100644 spring-boot-modules/spring-boot-libraries-comparison/src/main/java/com/baeldung/graphqlvsrest/resolver/ProductResolver.java delete mode 100644 spring-boot-modules/spring-boot-libraries-comparison/src/main/java/com/baeldung/graphqlvsrest/resolver/Query.java delete mode 100644 spring-boot-modules/spring-boot-libraries-comparison/src/main/resources/application.properties diff --git a/spring-boot-modules/pom.xml b/spring-boot-modules/pom.xml index eaf12d128c..337504e5e0 100644 --- a/spring-boot-modules/pom.xml +++ b/spring-boot-modules/pom.xml @@ -50,7 +50,6 @@ spring-boot-keycloak-2 spring-boot-libraries spring-boot-libraries-2 - spring-boot-libraries-comparison spring-boot-logging-log4j2 spring-boot-mvc spring-boot-mvc-2 diff --git a/spring-boot-modules/spring-boot-graphql/README.md b/spring-boot-modules/spring-boot-graphql/README.md index 93387741b8..d0886eb7c2 100644 --- a/spring-boot-modules/spring-boot-graphql/README.md +++ b/spring-boot-modules/spring-boot-graphql/README.md @@ -10,6 +10,7 @@ The "REST With Spring" Classes: http://bit.ly/restwithspring - [Getting Started with GraphQL and Spring Boot](https://www.baeldung.com/spring-graphql) - [Expose GraphQL Field with Different Name](https://www.baeldung.com/graphql-field-name) - [Error Handling in GraphQL With Spring Boot](https://www.baeldung.com/spring-graphql-error-handling) +- [GraphQL vs REST](https://www.baeldung.com/graphql-vs-rest) ### GraphQL sample queries diff --git a/spring-boot-modules/spring-boot-libraries-comparison/src/main/java/com/baeldung/graphqlvsrest/GraphqlVsRestApplication.java b/spring-boot-modules/spring-boot-graphql/src/main/java/com/baeldung/graphqlvsrest/GraphqlVsRestApplication.java similarity index 64% rename from spring-boot-modules/spring-boot-libraries-comparison/src/main/java/com/baeldung/graphqlvsrest/GraphqlVsRestApplication.java rename to spring-boot-modules/spring-boot-graphql/src/main/java/com/baeldung/graphqlvsrest/GraphqlVsRestApplication.java index 29a3ef1e0f..b4bb31c86c 100644 --- a/spring-boot-modules/spring-boot-libraries-comparison/src/main/java/com/baeldung/graphqlvsrest/GraphqlVsRestApplication.java +++ b/spring-boot-modules/spring-boot-graphql/src/main/java/com/baeldung/graphqlvsrest/GraphqlVsRestApplication.java @@ -1,18 +1,20 @@ package com.baeldung.graphqlvsrest; -import com.baeldung.graphqlvsrest.configuration.GraphqlConfiguration; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration; import org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration; -import org.springframework.context.annotation.Import; @SpringBootApplication -@Import(GraphqlConfiguration.class) -@EnableAutoConfiguration(exclude = {SecurityAutoConfiguration.class}) +@EnableAutoConfiguration(exclude = { + SecurityAutoConfiguration.class, + HibernateJpaAutoConfiguration.class +}) public class GraphqlVsRestApplication { public static void main(String[] args) { + System.setProperty("spring.profiles.default", "rest-vs-graphql"); SpringApplication.run(GraphqlVsRestApplication.class, args); } diff --git a/spring-boot-modules/spring-boot-graphql/src/main/java/com/baeldung/graphqlvsrest/configuration/GraphqlConfiguration.java b/spring-boot-modules/spring-boot-graphql/src/main/java/com/baeldung/graphqlvsrest/configuration/GraphqlConfiguration.java new file mode 100644 index 0000000000..aab94aaed6 --- /dev/null +++ b/spring-boot-modules/spring-boot-graphql/src/main/java/com/baeldung/graphqlvsrest/configuration/GraphqlConfiguration.java @@ -0,0 +1,8 @@ +package com.baeldung.graphqlvsrest.configuration; + +import org.springframework.context.annotation.Configuration; + +@Configuration +public class GraphqlConfiguration { + +} diff --git a/spring-boot-modules/spring-boot-libraries-comparison/src/main/java/com/baeldung/graphqlvsrest/controller/OrderController.java b/spring-boot-modules/spring-boot-graphql/src/main/java/com/baeldung/graphqlvsrest/controller/OrderController.java similarity index 66% rename from spring-boot-modules/spring-boot-libraries-comparison/src/main/java/com/baeldung/graphqlvsrest/controller/OrderController.java rename to spring-boot-modules/spring-boot-graphql/src/main/java/com/baeldung/graphqlvsrest/controller/OrderController.java index 14f0468bbd..960b3dd279 100644 --- a/spring-boot-modules/spring-boot-libraries-comparison/src/main/java/com/baeldung/graphqlvsrest/controller/OrderController.java +++ b/spring-boot-modules/spring-boot-graphql/src/main/java/com/baeldung/graphqlvsrest/controller/OrderController.java @@ -1,13 +1,12 @@ package com.baeldung.graphqlvsrest.controller; import com.baeldung.graphqlvsrest.entity.Order; -import com.baeldung.graphqlvsrest.entity.Product; -import com.baeldung.graphqlvsrest.model.ProductModel; import com.baeldung.graphqlvsrest.repository.OrderRepository; -import com.baeldung.graphqlvsrest.repository.ProductRepository; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.data.domain.Pageable; -import org.springframework.web.bind.annotation.*; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.RestController; import java.util.List; diff --git a/spring-boot-modules/spring-boot-libraries-comparison/src/main/java/com/baeldung/graphqlvsrest/controller/ProductController.java b/spring-boot-modules/spring-boot-graphql/src/main/java/com/baeldung/graphqlvsrest/controller/ProductController.java similarity index 58% rename from spring-boot-modules/spring-boot-libraries-comparison/src/main/java/com/baeldung/graphqlvsrest/controller/ProductController.java rename to spring-boot-modules/spring-boot-graphql/src/main/java/com/baeldung/graphqlvsrest/controller/ProductController.java index 2fdee8765a..490db1070f 100644 --- a/spring-boot-modules/spring-boot-libraries-comparison/src/main/java/com/baeldung/graphqlvsrest/controller/ProductController.java +++ b/spring-boot-modules/spring-boot-graphql/src/main/java/com/baeldung/graphqlvsrest/controller/ProductController.java @@ -5,7 +5,13 @@ import com.baeldung.graphqlvsrest.model.ProductModel; import com.baeldung.graphqlvsrest.repository.ProductRepository; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.domain.Pageable; -import org.springframework.web.bind.annotation.*; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.PutMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; import java.util.List; @@ -17,22 +23,22 @@ public class ProductController { ProductRepository productRepository; @GetMapping - public List getProducts(Pageable pageable){ + public List getProducts(Pageable pageable) { return productRepository.getProducts(pageable.getPageSize(), pageable.getPageNumber()); } @GetMapping("/{product-id}") - public Product getProducts(@PathVariable("product-id") Integer productId){ + public Product getProduct(@PathVariable("product-id") Integer productId) { return productRepository.getProduct(productId); } @PostMapping - public Product save(@RequestBody ProductModel productModel){ + public Product save(@RequestBody ProductModel productModel) { return productRepository.save(productModel); } @PutMapping("/{product-id}") - public Product update(@PathVariable("product-id") Integer productId, @RequestBody ProductModel productModel){ + public Product update(@PathVariable("product-id") Integer productId, @RequestBody ProductModel productModel) { return productRepository.update(productId, productModel); } } diff --git a/spring-boot-modules/spring-boot-graphql/src/main/java/com/baeldung/graphqlvsrest/controller/ProductGraphQLController.java b/spring-boot-modules/spring-boot-graphql/src/main/java/com/baeldung/graphqlvsrest/controller/ProductGraphQLController.java new file mode 100644 index 0000000000..a73f9ec135 --- /dev/null +++ b/spring-boot-modules/spring-boot-graphql/src/main/java/com/baeldung/graphqlvsrest/controller/ProductGraphQLController.java @@ -0,0 +1,51 @@ +package com.baeldung.graphqlvsrest.controller; + +import com.baeldung.graphqlvsrest.entity.Order; +import com.baeldung.graphqlvsrest.entity.Product; +import com.baeldung.graphqlvsrest.model.ProductModel; +import com.baeldung.graphqlvsrest.repository.OrderRepository; +import com.baeldung.graphqlvsrest.repository.ProductRepository; +import org.springframework.graphql.data.method.annotation.Argument; +import org.springframework.graphql.data.method.annotation.MutationMapping; +import org.springframework.graphql.data.method.annotation.QueryMapping; +import org.springframework.graphql.data.method.annotation.SchemaMapping; +import org.springframework.stereotype.Controller; + +import java.util.List; + +@Controller +public class ProductGraphQLController { + + private final ProductRepository productRepository; + + private final OrderRepository orderRepository; + + public ProductGraphQLController(ProductRepository productRepository, OrderRepository orderRepository) { + this.productRepository = productRepository; + this.orderRepository = orderRepository; + } + + @QueryMapping + public List products(@Argument int size, @Argument int page) { + return productRepository.getProducts(size, page); + } + + @QueryMapping + public Product product(@Argument int id) { + return productRepository.getProduct(id); + } + + @MutationMapping + public Product saveProduct(@Argument ProductModel product) { + return productRepository.save(product); + } + + @MutationMapping + public Product updateProduct(@Argument Integer id, @Argument ProductModel product) { + return productRepository.update(id, product); + } + @SchemaMapping(typeName="Product", field="orders") + public List getOrders(Product product) { + return orderRepository.getOrdersByProduct(product.getId()); + } +} diff --git a/spring-boot-modules/spring-boot-libraries-comparison/src/main/java/com/baeldung/graphqlvsrest/entity/Order.java b/spring-boot-modules/spring-boot-graphql/src/main/java/com/baeldung/graphqlvsrest/entity/Order.java similarity index 50% rename from spring-boot-modules/spring-boot-libraries-comparison/src/main/java/com/baeldung/graphqlvsrest/entity/Order.java rename to spring-boot-modules/spring-boot-graphql/src/main/java/com/baeldung/graphqlvsrest/entity/Order.java index 89606e9897..0891d1fdee 100644 --- a/spring-boot-modules/spring-boot-libraries-comparison/src/main/java/com/baeldung/graphqlvsrest/entity/Order.java +++ b/spring-boot-modules/spring-boot-graphql/src/main/java/com/baeldung/graphqlvsrest/entity/Order.java @@ -2,11 +2,11 @@ package com.baeldung.graphqlvsrest.entity; public class Order { private Integer id; - private Integer product_id; - private String customer_uuid; + private Integer productId; + private String customerId; private String status; private String address; - private String creation_date; + private String creationDate; public Integer getId() { return id; @@ -16,12 +16,12 @@ public class Order { this.id = id; } - public Integer getProduct_id() { - return product_id; + public Integer getProductId() { + return productId; } - public void setProduct_id(Integer product_id) { - this.product_id = product_id; + public void setProductId(Integer productId) { + this.productId = productId; } public String getStatus() { @@ -32,12 +32,12 @@ public class Order { this.status = status; } - public String getCustomer_uuid() { - return customer_uuid; + public String getCustomerId() { + return customerId; } - public void setCustomer_uuid(String customer_uuid) { - this.customer_uuid = customer_uuid; + public void setCustomerId(String customerId) { + this.customerId = customerId; } public String getAddress() { @@ -48,11 +48,11 @@ public class Order { this.address = address; } - public String getCreation_date() { - return creation_date; + public String getCreationDate() { + return creationDate; } - public void setCreation_date(String creation_date) { - this.creation_date = creation_date; + public void setCreationDate(String creationDate) { + this.creationDate = creationDate; } } diff --git a/spring-boot-modules/spring-boot-libraries-comparison/src/main/java/com/baeldung/graphqlvsrest/entity/Product.java b/spring-boot-modules/spring-boot-graphql/src/main/java/com/baeldung/graphqlvsrest/entity/Product.java similarity index 70% rename from spring-boot-modules/spring-boot-libraries-comparison/src/main/java/com/baeldung/graphqlvsrest/entity/Product.java rename to spring-boot-modules/spring-boot-graphql/src/main/java/com/baeldung/graphqlvsrest/entity/Product.java index 2da9244c92..66c64fa2c3 100644 --- a/spring-boot-modules/spring-boot-libraries-comparison/src/main/java/com/baeldung/graphqlvsrest/entity/Product.java +++ b/spring-boot-modules/spring-boot-graphql/src/main/java/com/baeldung/graphqlvsrest/entity/Product.java @@ -11,10 +11,10 @@ public class Product { private String status; private String currency; private Double price; - private List image_url; - private List video_url; + private List imageUrls; + private List videoUrls; private Integer stock; - private Float average_rating; + private Float averageRating; public Product(Integer id, ProductModel productModel) { this.id = id; @@ -23,9 +23,9 @@ public class Product { this.currency = productModel.getCurrency(); this.price = productModel.getPrice(); this.stock = productModel.getStock(); - this.image_url = productModel.getImage_url(); - this.video_url = productModel.getVideo_url(); - this.average_rating = 0F; + this.imageUrls = productModel.getImageUrls(); + this.videoUrls = productModel.getVideoUrls(); + this.averageRating = 0F; this.status = productModel.getStatus(); } @@ -81,20 +81,20 @@ public class Product { this.price = price; } - public List getImage_url() { - return image_url; + public List getImageUrls() { + return imageUrls; } - public void setImage_url(List image_url) { - this.image_url = image_url; + public void setImageUrls(List imageUrls) { + this.imageUrls = imageUrls; } - public List getVideo_url() { - return video_url; + public List getVideoUrls() { + return videoUrls; } - public void setVideo_url(List video_url) { - this.video_url = video_url; + public void setVideoUrls(List videoUrls) { + this.videoUrls = videoUrls; } public Integer getStock() { @@ -105,11 +105,11 @@ public class Product { this.stock = stock; } - public Float getAverage_rating() { - return average_rating; + public Float getAverageRating() { + return averageRating; } - public void setAverage_rating(Float average_rating) { - this.average_rating = average_rating; + public void setAverageRating(Float averageRating) { + this.averageRating = averageRating; } } diff --git a/spring-boot-modules/spring-boot-libraries-comparison/src/main/java/com/baeldung/graphqlvsrest/model/ProductModel.java b/spring-boot-modules/spring-boot-graphql/src/main/java/com/baeldung/graphqlvsrest/model/ProductModel.java similarity index 76% rename from spring-boot-modules/spring-boot-libraries-comparison/src/main/java/com/baeldung/graphqlvsrest/model/ProductModel.java rename to spring-boot-modules/spring-boot-graphql/src/main/java/com/baeldung/graphqlvsrest/model/ProductModel.java index db7a3ba54e..d5f6d91ed7 100644 --- a/spring-boot-modules/spring-boot-libraries-comparison/src/main/java/com/baeldung/graphqlvsrest/model/ProductModel.java +++ b/spring-boot-modules/spring-boot-graphql/src/main/java/com/baeldung/graphqlvsrest/model/ProductModel.java @@ -8,8 +8,8 @@ public class ProductModel { private String status; private String currency; private Double price; - private List image_url; - private List video_url; + private List imageUrls; + private List videoUrls; private Integer stock; public String getName() { @@ -52,20 +52,20 @@ public class ProductModel { this.price = price; } - public List getImage_url() { - return image_url; + public List getImageUrls() { + return imageUrls; } - public void setImage_url(List image_url) { - this.image_url = image_url; + public void setImageUrls(List imageUrls) { + this.imageUrls = imageUrls; } - public List getVideo_url() { - return video_url; + public List getVideoUrls() { + return videoUrls; } - public void setVideo_url(List video_url) { - this.video_url = video_url; + public void setVideoUrls(List videoUrls) { + this.videoUrls = videoUrls; } public Integer getStock() { @@ -84,8 +84,8 @@ public class ProductModel { ", status='" + status + '\'' + ", currency='" + currency + '\'' + ", price=" + price + - ", image_url=" + image_url + - ", video_url=" + video_url + + ", imageUrls=" + imageUrls + + ", videoUrls=" + videoUrls + ", stock=" + stock + '}'; } diff --git a/spring-boot-modules/spring-boot-libraries-comparison/src/main/java/com/baeldung/graphqlvsrest/repository/OrderRepository.java b/spring-boot-modules/spring-boot-graphql/src/main/java/com/baeldung/graphqlvsrest/repository/OrderRepository.java similarity index 67% rename from spring-boot-modules/spring-boot-libraries-comparison/src/main/java/com/baeldung/graphqlvsrest/repository/OrderRepository.java rename to spring-boot-modules/spring-boot-graphql/src/main/java/com/baeldung/graphqlvsrest/repository/OrderRepository.java index 92cc288426..bf4557baa7 100644 --- a/spring-boot-modules/spring-boot-libraries-comparison/src/main/java/com/baeldung/graphqlvsrest/repository/OrderRepository.java +++ b/spring-boot-modules/spring-boot-graphql/src/main/java/com/baeldung/graphqlvsrest/repository/OrderRepository.java @@ -1,8 +1,6 @@ package com.baeldung.graphqlvsrest.repository; import com.baeldung.graphqlvsrest.entity.Order; -import com.baeldung.graphqlvsrest.entity.Product; -import com.baeldung.graphqlvsrest.model.ProductModel; import java.util.List; diff --git a/spring-boot-modules/spring-boot-libraries-comparison/src/main/java/com/baeldung/graphqlvsrest/repository/ProductRepository.java b/spring-boot-modules/spring-boot-graphql/src/main/java/com/baeldung/graphqlvsrest/repository/ProductRepository.java similarity index 100% rename from spring-boot-modules/spring-boot-libraries-comparison/src/main/java/com/baeldung/graphqlvsrest/repository/ProductRepository.java rename to spring-boot-modules/spring-boot-graphql/src/main/java/com/baeldung/graphqlvsrest/repository/ProductRepository.java diff --git a/spring-boot-modules/spring-boot-libraries-comparison/src/main/java/com/baeldung/graphqlvsrest/repository/impl/OrderRepositoryImpl.java b/spring-boot-modules/spring-boot-graphql/src/main/java/com/baeldung/graphqlvsrest/repository/impl/OrderRepositoryImpl.java similarity index 56% rename from spring-boot-modules/spring-boot-libraries-comparison/src/main/java/com/baeldung/graphqlvsrest/repository/impl/OrderRepositoryImpl.java rename to spring-boot-modules/spring-boot-graphql/src/main/java/com/baeldung/graphqlvsrest/repository/impl/OrderRepositoryImpl.java index e4f316c865..6b1972c723 100644 --- a/spring-boot-modules/spring-boot-libraries-comparison/src/main/java/com/baeldung/graphqlvsrest/repository/impl/OrderRepositoryImpl.java +++ b/spring-boot-modules/spring-boot-graphql/src/main/java/com/baeldung/graphqlvsrest/repository/impl/OrderRepositoryImpl.java @@ -13,24 +13,25 @@ import java.util.stream.Collectors; @Repository public class OrderRepositoryImpl implements OrderRepository { - private static List orderList = new ArrayList<>(); + private static final List ORDER_LIST = new ArrayList<>(); public OrderRepositoryImpl() { - for (int i = 1; i <= 100; i++){ + for (int i = 1; i <= 100; i++) { Order order = new Order(); order.setId(i); - order.setProduct_id(i%10); - order.setAddress(UUID.randomUUID().toString()); - order.setCustomer_uuid(UUID.randomUUID().toString()); - order.setCreation_date(new Date(System.currentTimeMillis()).toString()); + order.setProductId(i % 10); + order.setAddress(i + " A Street"); + order.setCustomerId(UUID.randomUUID().toString()); + order.setCreationDate(new Date(System.currentTimeMillis()).toString()); order.setStatus("Delivered"); - orderList.add(order); + ORDER_LIST.add(order); } } - @Override public List getOrdersByProduct(Integer productId) { - return orderList.stream().filter(order -> order.getProduct_id().equals(productId)).collect(Collectors.toList()); + return ORDER_LIST.stream() + .filter(order -> order.getProductId().equals(productId)) + .collect(Collectors.toList()); } } diff --git a/spring-boot-modules/spring-boot-libraries-comparison/src/main/java/com/baeldung/graphqlvsrest/repository/impl/ProductRepositoryImpl.java b/spring-boot-modules/spring-boot-graphql/src/main/java/com/baeldung/graphqlvsrest/repository/impl/ProductRepositoryImpl.java similarity index 63% rename from spring-boot-modules/spring-boot-libraries-comparison/src/main/java/com/baeldung/graphqlvsrest/repository/impl/ProductRepositoryImpl.java rename to spring-boot-modules/spring-boot-graphql/src/main/java/com/baeldung/graphqlvsrest/repository/impl/ProductRepositoryImpl.java index 845472faea..b7ba322b43 100644 --- a/spring-boot-modules/spring-boot-libraries-comparison/src/main/java/com/baeldung/graphqlvsrest/repository/impl/ProductRepositoryImpl.java +++ b/spring-boot-modules/spring-boot-graphql/src/main/java/com/baeldung/graphqlvsrest/repository/impl/ProductRepositoryImpl.java @@ -6,68 +6,75 @@ import com.baeldung.graphqlvsrest.repository.ProductRepository; import org.springframework.stereotype.Repository; import java.util.ArrayList; -import java.util.Arrays; import java.util.List; import java.util.Optional; import java.util.stream.Collectors; +import static java.util.Collections.singletonList; + @Repository public class ProductRepositoryImpl implements ProductRepository { - private static List productList = new ArrayList<>(); + private static final List PRODUCT_LIST = new ArrayList<>(); public ProductRepositoryImpl() { - for (int i = 1; i <= 10; i++){ + for (int i = 1; i <= 10; i++) { Product product = new Product(); product.setId(i); product.setName(String.format("Product %d", i)); product.setDescription(String.format("Product %d description", i)); product.setCurrency(String.format("Product %d currency", i)); - product.setPrice(Double.valueOf(i^2)); + product.setPrice((double) (i ^ 2)); product.setStock(10); - product.setAverage_rating(0F); - product.setImage_url(Arrays.asList(String.format("www.baeldung.com/imageurl/%d", i))); - product.setVideo_url(Arrays.asList(String.format("www.baeldung.com/videourl/%d", i))); - productList.add(product); + product.setAverageRating(0F); + product.setImageUrls(singletonList(String.format("www.baeldung.com/imageurl/%d", i))); + product.setVideoUrls(singletonList(String.format("www.baeldung.com/videourl/%d", i))); + + PRODUCT_LIST.add(product); } } @Override public List getProducts(Integer pageSize, Integer pageNumber) { - return productList.stream().skip(pageSize*pageNumber).limit(pageSize).collect(Collectors.toList()); + return PRODUCT_LIST.stream() + .skip((long) pageSize * pageNumber) + .limit(pageSize) + .collect(Collectors.toList()); } @Override public Product getProduct(Integer id) { - return productList.stream().filter(product -> product.getId().equals(id)).findFirst().orElse(null); + return PRODUCT_LIST.stream() + .filter(product -> product.getId().equals(id)) + .findFirst().orElse(null); } @Override public Product save(ProductModel productModel) { - Product product = new Product(productList.size()+1, productModel); - productList.add(product); + Product product = new Product(PRODUCT_LIST.size() + 1, productModel); + PRODUCT_LIST.add(product); return product; } @Override public Product update(Integer productId, ProductModel productModel) { - Product product = getProduct(productId); - if (product != null){ + Product product = getProduct(productId); + if (product != null) { update(product, productModel); } return product; } - private void update(Product product, ProductModel productModel){ + private void update(Product product, ProductModel productModel) { if (productModel != null) { - System.out.println(productModel.toString()); + System.out.println(productModel); Optional.ofNullable(productModel.getName()).ifPresent(product::setName); Optional.ofNullable(productModel.getDescription()).ifPresent(product::setDescription); Optional.ofNullable(productModel.getCurrency()).ifPresent(product::setCurrency); - Optional.ofNullable(productModel.getImage_url()).ifPresent(product::setImage_url); + Optional.ofNullable(productModel.getImageUrls()).ifPresent(product::setImageUrls); Optional.ofNullable(productModel.getStock()).ifPresent(product::setStock); Optional.ofNullable(productModel.getStatus()).ifPresent(product::setStatus); - Optional.ofNullable(productModel.getVideo_url()).ifPresent(product::setVideo_url); + Optional.ofNullable(productModel.getVideoUrls()).ifPresent(product::setVideoUrls); Optional.ofNullable(productModel.getPrice()).ifPresent(product::setPrice); } } diff --git a/spring-boot-modules/spring-boot-graphql/src/main/resources/application-rest-vs-graphql.yml b/spring-boot-modules/spring-boot-graphql/src/main/resources/application-rest-vs-graphql.yml new file mode 100644 index 0000000000..58069ddd72 --- /dev/null +++ b/spring-boot-modules/spring-boot-graphql/src/main/resources/application-rest-vs-graphql.yml @@ -0,0 +1,11 @@ +server: + port: 8081 + +spring: + graphql: + schema: + locations: classpath:graphql-vs-rest/ + + jackson: + parser: + allow-unquoted-control-chars: true \ No newline at end of file diff --git a/spring-boot-modules/spring-boot-libraries-comparison/src/main/resources/graphql/schema.graphqls b/spring-boot-modules/spring-boot-graphql/src/main/resources/graphql-vs-rest/schema.graphqls similarity index 77% rename from spring-boot-modules/spring-boot-libraries-comparison/src/main/resources/graphql/schema.graphqls rename to spring-boot-modules/spring-boot-graphql/src/main/resources/graphql-vs-rest/schema.graphqls index 2709510d72..520f26648c 100644 --- a/spring-boot-modules/spring-boot-libraries-comparison/src/main/resources/graphql/schema.graphqls +++ b/spring-boot-modules/spring-boot-graphql/src/main/resources/graphql-vs-rest/schema.graphqls @@ -5,20 +5,20 @@ type Product { status: String currency: String! price: Float - image_url: [String] - video_url: [String] + imageUrls: [String] + videoUrls: [String] stock: Int - average_rating: Float + averageRating: Float orders:[Order] } type Order{ id:ID - product_id:Int - customer_uuid:String + productId:Int + customerId:String address:String status:String - creation_date:String + creationDate:String } input ProductModel { @@ -27,8 +27,8 @@ input ProductModel { status: String currency: String! price: Float - image_url: [String] - video_url: [String] + imageUrls: [String] + videoUrls: [String] stock: Int } @@ -38,8 +38,8 @@ input ProductUpdateModel { status: String currency: String price: Float - image_url: [String] - video_url: [String] + imageUrls: [String] + videoUrls: [String] stock: Int } diff --git a/spring-boot-modules/spring-boot-libraries-comparison/README.md b/spring-boot-modules/spring-boot-libraries-comparison/README.md deleted file mode 100644 index d373f91b3b..0000000000 --- a/spring-boot-modules/spring-boot-libraries-comparison/README.md +++ /dev/null @@ -1,7 +0,0 @@ -## Spring Boot Libraries - -This module contains articles about various Spring Boot libraries Comparison - -### Relevant Articles: - -- [GraphQL vs REST](https://www.baeldung.com/graphql-vs-rest) diff --git a/spring-boot-modules/spring-boot-libraries-comparison/pom.xml b/spring-boot-modules/spring-boot-libraries-comparison/pom.xml deleted file mode 100644 index 59d0e75be3..0000000000 --- a/spring-boot-modules/spring-boot-libraries-comparison/pom.xml +++ /dev/null @@ -1,46 +0,0 @@ - - - 4.0.0 - spring-boot-libraries-comparison - - - com.baeldung.spring-boot-modules - spring-boot-modules - 1.0.0-SNAPSHOT - - - - - org.springframework.boot - spring-boot-starter-web - - - org.springframework.data - spring-data-jpa - - - com.graphql-java - graphql-spring-boot-starter - ${graphql-spring-boot-starter.version} - - - com.graphql-java - graphql-java-tools - ${graphql-java-tools.version} - - - com.graphql-java - graphiql-spring-boot-starter - ${graphql-spring-boot-starter.version} - - - - - - 5.0.2 - 5.2.4 - - - \ No newline at end of file diff --git a/spring-boot-modules/spring-boot-libraries-comparison/src/main/java/com/baeldung/graphqlvsrest/configuration/GraphqlConfiguration.java b/spring-boot-modules/spring-boot-libraries-comparison/src/main/java/com/baeldung/graphqlvsrest/configuration/GraphqlConfiguration.java deleted file mode 100644 index c100a03143..0000000000 --- a/spring-boot-modules/spring-boot-libraries-comparison/src/main/java/com/baeldung/graphqlvsrest/configuration/GraphqlConfiguration.java +++ /dev/null @@ -1,35 +0,0 @@ -package com.baeldung.graphqlvsrest.configuration; - -import com.baeldung.graphqlvsrest.repository.OrderRepository; -import com.baeldung.graphqlvsrest.resolver.Mutation; -import com.baeldung.graphqlvsrest.resolver.ProductResolver; -import com.baeldung.graphqlvsrest.resolver.Query; -import com.baeldung.graphqlvsrest.repository.ProductRepository; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.Configuration; - -@Configuration -public class GraphqlConfiguration { - - @Autowired - ProductRepository productRepository; - - @Autowired - OrderRepository orderRepository; - - @Bean - public Query query() { - return new Query(productRepository); - } - - @Bean - public ProductResolver productResolver(){ - return new ProductResolver(orderRepository); - } - - @Bean - public Mutation mutation() { - return new Mutation(productRepository); - } -} diff --git a/spring-boot-modules/spring-boot-libraries-comparison/src/main/java/com/baeldung/graphqlvsrest/resolver/Mutation.java b/spring-boot-modules/spring-boot-libraries-comparison/src/main/java/com/baeldung/graphqlvsrest/resolver/Mutation.java deleted file mode 100644 index 3d643f97e6..0000000000 --- a/spring-boot-modules/spring-boot-libraries-comparison/src/main/java/com/baeldung/graphqlvsrest/resolver/Mutation.java +++ /dev/null @@ -1,22 +0,0 @@ -package com.baeldung.graphqlvsrest.resolver; - -import com.baeldung.graphqlvsrest.entity.Product; -import com.baeldung.graphqlvsrest.model.ProductModel; -import com.baeldung.graphqlvsrest.repository.ProductRepository; -import com.coxautodev.graphql.tools.GraphQLMutationResolver; - -public class Mutation implements GraphQLMutationResolver { - - private ProductRepository productRepository; - public Mutation(ProductRepository productRepository){ - this.productRepository = productRepository; - } - - public Product saveProduct(ProductModel productModel) { - return productRepository.save(productModel); - } - - public Product updateProduct(Integer productId, ProductModel productModel) { - return productRepository.update(productId, productModel); - } -} diff --git a/spring-boot-modules/spring-boot-libraries-comparison/src/main/java/com/baeldung/graphqlvsrest/resolver/ProductResolver.java b/spring-boot-modules/spring-boot-libraries-comparison/src/main/java/com/baeldung/graphqlvsrest/resolver/ProductResolver.java deleted file mode 100644 index f20b8d5920..0000000000 --- a/spring-boot-modules/spring-boot-libraries-comparison/src/main/java/com/baeldung/graphqlvsrest/resolver/ProductResolver.java +++ /dev/null @@ -1,18 +0,0 @@ -package com.baeldung.graphqlvsrest.resolver; - -import com.baeldung.graphqlvsrest.entity.Order; -import com.baeldung.graphqlvsrest.entity.Product; -import com.baeldung.graphqlvsrest.repository.OrderRepository; -import com.coxautodev.graphql.tools.GraphQLResolver; - -import java.util.List; - -public class ProductResolver implements GraphQLResolver { - private OrderRepository orderRepository; - public ProductResolver(OrderRepository orderRepository){ - this.orderRepository = orderRepository; - } - public List getOrders(Product product){ - return orderRepository.getOrdersByProduct(product.getId()); - } -} diff --git a/spring-boot-modules/spring-boot-libraries-comparison/src/main/java/com/baeldung/graphqlvsrest/resolver/Query.java b/spring-boot-modules/spring-boot-libraries-comparison/src/main/java/com/baeldung/graphqlvsrest/resolver/Query.java deleted file mode 100644 index 0d218261b2..0000000000 --- a/spring-boot-modules/spring-boot-libraries-comparison/src/main/java/com/baeldung/graphqlvsrest/resolver/Query.java +++ /dev/null @@ -1,27 +0,0 @@ -package com.baeldung.graphqlvsrest.resolver; - -import com.baeldung.graphqlvsrest.entity.Order; -import com.baeldung.graphqlvsrest.entity.Product; -import com.baeldung.graphqlvsrest.repository.OrderRepository; -import com.baeldung.graphqlvsrest.repository.ProductRepository; -import com.coxautodev.graphql.tools.GraphQLQueryResolver; - -import java.util.List; - -public class Query implements GraphQLQueryResolver { - - private ProductRepository productRepository; - public Query(ProductRepository productRepository){ - this.productRepository = productRepository; - } - - public List getProducts(int pageSize, int pageNumber) { - return productRepository.getProducts(pageSize, pageNumber); - } - - public Product getProduct(int id) { - return productRepository.getProduct(id); - } - - -} diff --git a/spring-boot-modules/spring-boot-libraries-comparison/src/main/resources/application.properties b/spring-boot-modules/spring-boot-libraries-comparison/src/main/resources/application.properties deleted file mode 100644 index e69de29bb2..0000000000