[JAVA-15353] Update code for GraphQL vs REST article (#12849)
This commit is contained in:
		
							parent
							
								
									7cffc487ea
								
							
						
					
					
						commit
						f4be8916b8
					
				| @ -50,7 +50,6 @@ | |||||||
|         <module>spring-boot-keycloak-2</module> |         <module>spring-boot-keycloak-2</module> | ||||||
|         <module>spring-boot-libraries</module> |         <module>spring-boot-libraries</module> | ||||||
|         <module>spring-boot-libraries-2</module> |         <module>spring-boot-libraries-2</module> | ||||||
|         <module>spring-boot-libraries-comparison</module> |  | ||||||
|         <module>spring-boot-logging-log4j2</module> |         <module>spring-boot-logging-log4j2</module> | ||||||
|         <module>spring-boot-mvc</module> |         <module>spring-boot-mvc</module> | ||||||
|         <module>spring-boot-mvc-2</module> |         <module>spring-boot-mvc-2</module> | ||||||
|  | |||||||
| @ -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) | - [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) | - [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) | - [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 | ### GraphQL sample queries | ||||||
| 
 | 
 | ||||||
|  | |||||||
| @ -1,18 +1,20 @@ | |||||||
| package com.baeldung.graphqlvsrest; | package com.baeldung.graphqlvsrest; | ||||||
| 
 | 
 | ||||||
| import com.baeldung.graphqlvsrest.configuration.GraphqlConfiguration; |  | ||||||
| import org.springframework.boot.SpringApplication; | import org.springframework.boot.SpringApplication; | ||||||
| import org.springframework.boot.autoconfigure.EnableAutoConfiguration; | import org.springframework.boot.autoconfigure.EnableAutoConfiguration; | ||||||
| import org.springframework.boot.autoconfigure.SpringBootApplication; | 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.boot.autoconfigure.security.servlet.SecurityAutoConfiguration; | ||||||
| import org.springframework.context.annotation.Import; |  | ||||||
| 
 | 
 | ||||||
| @SpringBootApplication | @SpringBootApplication | ||||||
| @Import(GraphqlConfiguration.class) | @EnableAutoConfiguration(exclude = { | ||||||
| @EnableAutoConfiguration(exclude = {SecurityAutoConfiguration.class}) |   SecurityAutoConfiguration.class, | ||||||
|  |   HibernateJpaAutoConfiguration.class | ||||||
|  | }) | ||||||
| public class GraphqlVsRestApplication { | public class GraphqlVsRestApplication { | ||||||
| 
 | 
 | ||||||
|     public static void main(String[] args) { |     public static void main(String[] args) { | ||||||
|  |         System.setProperty("spring.profiles.default", "rest-vs-graphql"); | ||||||
|         SpringApplication.run(GraphqlVsRestApplication.class, args); |         SpringApplication.run(GraphqlVsRestApplication.class, args); | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
| @ -0,0 +1,8 @@ | |||||||
|  | package com.baeldung.graphqlvsrest.configuration; | ||||||
|  | 
 | ||||||
|  | import org.springframework.context.annotation.Configuration; | ||||||
|  | 
 | ||||||
|  | @Configuration | ||||||
|  | public class GraphqlConfiguration { | ||||||
|  | 
 | ||||||
|  | } | ||||||
| @ -1,13 +1,12 @@ | |||||||
| package com.baeldung.graphqlvsrest.controller; | package com.baeldung.graphqlvsrest.controller; | ||||||
| 
 | 
 | ||||||
| import com.baeldung.graphqlvsrest.entity.Order; | 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.OrderRepository; | ||||||
| import com.baeldung.graphqlvsrest.repository.ProductRepository; |  | ||||||
| import org.springframework.beans.factory.annotation.Autowired; | import org.springframework.beans.factory.annotation.Autowired; | ||||||
| import org.springframework.data.domain.Pageable; | import org.springframework.web.bind.annotation.GetMapping; | ||||||
| import org.springframework.web.bind.annotation.*; | 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; | import java.util.List; | ||||||
| 
 | 
 | ||||||
| @ -5,7 +5,13 @@ import com.baeldung.graphqlvsrest.model.ProductModel; | |||||||
| import com.baeldung.graphqlvsrest.repository.ProductRepository; | import com.baeldung.graphqlvsrest.repository.ProductRepository; | ||||||
| import org.springframework.beans.factory.annotation.Autowired; | import org.springframework.beans.factory.annotation.Autowired; | ||||||
| import org.springframework.data.domain.Pageable; | 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; | import java.util.List; | ||||||
| 
 | 
 | ||||||
| @ -17,22 +23,22 @@ public class ProductController { | |||||||
|     ProductRepository productRepository; |     ProductRepository productRepository; | ||||||
| 
 | 
 | ||||||
|     @GetMapping |     @GetMapping | ||||||
|     public List<Product> getProducts(Pageable pageable){ |     public List<Product> getProducts(Pageable pageable) { | ||||||
|         return productRepository.getProducts(pageable.getPageSize(), pageable.getPageNumber()); |         return productRepository.getProducts(pageable.getPageSize(), pageable.getPageNumber()); | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     @GetMapping("/{product-id}") |     @GetMapping("/{product-id}") | ||||||
|     public Product getProducts(@PathVariable("product-id") Integer productId){ |     public Product getProduct(@PathVariable("product-id") Integer productId) { | ||||||
|         return productRepository.getProduct(productId); |         return productRepository.getProduct(productId); | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     @PostMapping |     @PostMapping | ||||||
|     public Product save(@RequestBody ProductModel productModel){ |     public Product save(@RequestBody ProductModel productModel) { | ||||||
|         return productRepository.save(productModel); |         return productRepository.save(productModel); | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     @PutMapping("/{product-id}") |     @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); |         return productRepository.update(productId, productModel); | ||||||
|     } |     } | ||||||
| } | } | ||||||
| @ -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<Product> 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<Order> getOrders(Product product) { | ||||||
|  |         return orderRepository.getOrdersByProduct(product.getId()); | ||||||
|  |     } | ||||||
|  | } | ||||||
| @ -2,11 +2,11 @@ package com.baeldung.graphqlvsrest.entity; | |||||||
| 
 | 
 | ||||||
| public class Order { | public class Order { | ||||||
|     private Integer id; |     private Integer id; | ||||||
|     private Integer product_id; |     private Integer productId; | ||||||
|     private String customer_uuid; |     private String customerId; | ||||||
|     private String status; |     private String status; | ||||||
|     private String address; |     private String address; | ||||||
|     private String creation_date; |     private String creationDate; | ||||||
| 
 | 
 | ||||||
|     public Integer getId() { |     public Integer getId() { | ||||||
|         return id; |         return id; | ||||||
| @ -16,12 +16,12 @@ public class Order { | |||||||
|         this.id = id; |         this.id = id; | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     public Integer getProduct_id() { |     public Integer getProductId() { | ||||||
|         return product_id; |         return productId; | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     public void setProduct_id(Integer product_id) { |     public void setProductId(Integer productId) { | ||||||
|         this.product_id = product_id; |         this.productId = productId; | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     public String getStatus() { |     public String getStatus() { | ||||||
| @ -32,12 +32,12 @@ public class Order { | |||||||
|         this.status = status; |         this.status = status; | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     public String getCustomer_uuid() { |     public String getCustomerId() { | ||||||
|         return customer_uuid; |         return customerId; | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     public void setCustomer_uuid(String customer_uuid) { |     public void setCustomerId(String customerId) { | ||||||
|         this.customer_uuid = customer_uuid; |         this.customerId = customerId; | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     public String getAddress() { |     public String getAddress() { | ||||||
| @ -48,11 +48,11 @@ public class Order { | |||||||
|         this.address = address; |         this.address = address; | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     public String getCreation_date() { |     public String getCreationDate() { | ||||||
|         return creation_date; |         return creationDate; | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     public void setCreation_date(String creation_date) { |     public void setCreationDate(String creationDate) { | ||||||
|         this.creation_date = creation_date; |         this.creationDate = creationDate; | ||||||
|     } |     } | ||||||
| } | } | ||||||
| @ -11,10 +11,10 @@ public class Product { | |||||||
|     private String status; |     private String status; | ||||||
|     private String currency; |     private String currency; | ||||||
|     private Double price; |     private Double price; | ||||||
|     private List<String> image_url; |     private List<String> imageUrls; | ||||||
|     private List<String> video_url; |     private List<String> videoUrls; | ||||||
|     private Integer stock; |     private Integer stock; | ||||||
|     private Float average_rating; |     private Float averageRating; | ||||||
| 
 | 
 | ||||||
|     public Product(Integer id, ProductModel productModel) { |     public Product(Integer id, ProductModel productModel) { | ||||||
|         this.id = id; |         this.id = id; | ||||||
| @ -23,9 +23,9 @@ public class Product { | |||||||
|         this.currency = productModel.getCurrency(); |         this.currency = productModel.getCurrency(); | ||||||
|         this.price = productModel.getPrice(); |         this.price = productModel.getPrice(); | ||||||
|         this.stock = productModel.getStock(); |         this.stock = productModel.getStock(); | ||||||
|         this.image_url = productModel.getImage_url(); |         this.imageUrls = productModel.getImageUrls(); | ||||||
|         this.video_url = productModel.getVideo_url(); |         this.videoUrls = productModel.getVideoUrls(); | ||||||
|         this.average_rating = 0F; |         this.averageRating = 0F; | ||||||
|         this.status = productModel.getStatus(); |         this.status = productModel.getStatus(); | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
| @ -81,20 +81,20 @@ public class Product { | |||||||
|         this.price = price; |         this.price = price; | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     public List<String> getImage_url() { |     public List<String> getImageUrls() { | ||||||
|         return image_url; |         return imageUrls; | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     public void setImage_url(List<String> image_url) { |     public void setImageUrls(List<String> imageUrls) { | ||||||
|         this.image_url = image_url; |         this.imageUrls = imageUrls; | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     public List<String> getVideo_url() { |     public List<String> getVideoUrls() { | ||||||
|         return video_url; |         return videoUrls; | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     public void setVideo_url(List<String> video_url) { |     public void setVideoUrls(List<String> videoUrls) { | ||||||
|         this.video_url = video_url; |         this.videoUrls = videoUrls; | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     public Integer getStock() { |     public Integer getStock() { | ||||||
| @ -105,11 +105,11 @@ public class Product { | |||||||
|         this.stock = stock; |         this.stock = stock; | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     public Float getAverage_rating() { |     public Float getAverageRating() { | ||||||
|         return average_rating; |         return averageRating; | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     public void setAverage_rating(Float average_rating) { |     public void setAverageRating(Float averageRating) { | ||||||
|         this.average_rating = average_rating; |         this.averageRating = averageRating; | ||||||
|     } |     } | ||||||
| } | } | ||||||
| @ -8,8 +8,8 @@ public class ProductModel { | |||||||
|     private String status; |     private String status; | ||||||
|     private String currency; |     private String currency; | ||||||
|     private Double price; |     private Double price; | ||||||
|     private List<String> image_url; |     private List<String> imageUrls; | ||||||
|     private List<String> video_url; |     private List<String> videoUrls; | ||||||
|     private Integer stock; |     private Integer stock; | ||||||
| 
 | 
 | ||||||
|     public String getName() { |     public String getName() { | ||||||
| @ -52,20 +52,20 @@ public class ProductModel { | |||||||
|         this.price = price; |         this.price = price; | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     public List<String> getImage_url() { |     public List<String> getImageUrls() { | ||||||
|         return image_url; |         return imageUrls; | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     public void setImage_url(List<String> image_url) { |     public void setImageUrls(List<String> imageUrls) { | ||||||
|         this.image_url = image_url; |         this.imageUrls = imageUrls; | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     public List<String> getVideo_url() { |     public List<String> getVideoUrls() { | ||||||
|         return video_url; |         return videoUrls; | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     public void setVideo_url(List<String> video_url) { |     public void setVideoUrls(List<String> videoUrls) { | ||||||
|         this.video_url = video_url; |         this.videoUrls = videoUrls; | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     public Integer getStock() { |     public Integer getStock() { | ||||||
| @ -84,8 +84,8 @@ public class ProductModel { | |||||||
|                 ", status='" + status + '\'' + |                 ", status='" + status + '\'' + | ||||||
|                 ", currency='" + currency + '\'' + |                 ", currency='" + currency + '\'' + | ||||||
|                 ", price=" + price + |                 ", price=" + price + | ||||||
|                 ", image_url=" + image_url + |                 ", imageUrls=" + imageUrls + | ||||||
|                 ", video_url=" + video_url + |                 ", videoUrls=" + videoUrls + | ||||||
|                 ", stock=" + stock + |                 ", stock=" + stock + | ||||||
|                 '}'; |                 '}'; | ||||||
|     } |     } | ||||||
| @ -1,8 +1,6 @@ | |||||||
| package com.baeldung.graphqlvsrest.repository; | package com.baeldung.graphqlvsrest.repository; | ||||||
| 
 | 
 | ||||||
| import com.baeldung.graphqlvsrest.entity.Order; | import com.baeldung.graphqlvsrest.entity.Order; | ||||||
| import com.baeldung.graphqlvsrest.entity.Product; |  | ||||||
| import com.baeldung.graphqlvsrest.model.ProductModel; |  | ||||||
| 
 | 
 | ||||||
| import java.util.List; | import java.util.List; | ||||||
| 
 | 
 | ||||||
| @ -13,24 +13,25 @@ import java.util.stream.Collectors; | |||||||
| @Repository | @Repository | ||||||
| public class OrderRepositoryImpl implements OrderRepository { | public class OrderRepositoryImpl implements OrderRepository { | ||||||
| 
 | 
 | ||||||
|     private static List<Order> orderList = new ArrayList<>(); |     private static final List<Order> ORDER_LIST = new ArrayList<>(); | ||||||
| 
 | 
 | ||||||
|     public OrderRepositoryImpl() { |     public OrderRepositoryImpl() { | ||||||
|         for (int i = 1; i <= 100; i++){ |         for (int i = 1; i <= 100; i++) { | ||||||
|             Order order = new Order(); |             Order order = new Order(); | ||||||
|             order.setId(i); |             order.setId(i); | ||||||
|             order.setProduct_id(i%10); |             order.setProductId(i % 10); | ||||||
|             order.setAddress(UUID.randomUUID().toString()); |             order.setAddress(i + " A Street"); | ||||||
|             order.setCustomer_uuid(UUID.randomUUID().toString()); |             order.setCustomerId(UUID.randomUUID().toString()); | ||||||
|             order.setCreation_date(new Date(System.currentTimeMillis()).toString()); |             order.setCreationDate(new Date(System.currentTimeMillis()).toString()); | ||||||
|             order.setStatus("Delivered"); |             order.setStatus("Delivered"); | ||||||
|             orderList.add(order); |             ORDER_LIST.add(order); | ||||||
|         } |         } | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
| 
 |  | ||||||
|     @Override |     @Override | ||||||
|     public List<Order> getOrdersByProduct(Integer productId) { |     public List<Order> 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()); | ||||||
|     } |     } | ||||||
| } | } | ||||||
| @ -6,68 +6,75 @@ import com.baeldung.graphqlvsrest.repository.ProductRepository; | |||||||
| import org.springframework.stereotype.Repository; | import org.springframework.stereotype.Repository; | ||||||
| 
 | 
 | ||||||
| import java.util.ArrayList; | import java.util.ArrayList; | ||||||
| import java.util.Arrays; |  | ||||||
| import java.util.List; | import java.util.List; | ||||||
| import java.util.Optional; | import java.util.Optional; | ||||||
| import java.util.stream.Collectors; | import java.util.stream.Collectors; | ||||||
| 
 | 
 | ||||||
|  | import static java.util.Collections.singletonList; | ||||||
|  | 
 | ||||||
| @Repository | @Repository | ||||||
| public class ProductRepositoryImpl implements ProductRepository { | public class ProductRepositoryImpl implements ProductRepository { | ||||||
| 
 | 
 | ||||||
|     private static List<Product> productList = new ArrayList<>(); |     private static final List<Product> PRODUCT_LIST = new ArrayList<>(); | ||||||
| 
 | 
 | ||||||
|     public ProductRepositoryImpl() { |     public ProductRepositoryImpl() { | ||||||
|         for (int i = 1; i <= 10; i++){ |         for (int i = 1; i <= 10; i++) { | ||||||
|             Product product = new Product(); |             Product product = new Product(); | ||||||
|             product.setId(i); |             product.setId(i); | ||||||
|             product.setName(String.format("Product %d", i)); |             product.setName(String.format("Product %d", i)); | ||||||
|             product.setDescription(String.format("Product %d description", i)); |             product.setDescription(String.format("Product %d description", i)); | ||||||
|             product.setCurrency(String.format("Product %d currency", i)); |             product.setCurrency(String.format("Product %d currency", i)); | ||||||
|             product.setPrice(Double.valueOf(i^2)); |             product.setPrice((double) (i ^ 2)); | ||||||
|             product.setStock(10); |             product.setStock(10); | ||||||
|             product.setAverage_rating(0F); |             product.setAverageRating(0F); | ||||||
|             product.setImage_url(Arrays.asList(String.format("www.baeldung.com/imageurl/%d", i))); |             product.setImageUrls(singletonList(String.format("www.baeldung.com/imageurl/%d", i))); | ||||||
|             product.setVideo_url(Arrays.asList(String.format("www.baeldung.com/videourl/%d", i))); |             product.setVideoUrls(singletonList(String.format("www.baeldung.com/videourl/%d", i))); | ||||||
|             productList.add(product); | 
 | ||||||
|  |             PRODUCT_LIST.add(product); | ||||||
|         } |         } | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     @Override |     @Override | ||||||
|     public List<Product> getProducts(Integer pageSize, Integer pageNumber) { |     public List<Product> 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 |     @Override | ||||||
|     public Product getProduct(Integer id) { |     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 |     @Override | ||||||
|     public Product save(ProductModel productModel) { |     public Product save(ProductModel productModel) { | ||||||
|         Product product = new Product(productList.size()+1, productModel); |         Product product = new Product(PRODUCT_LIST.size() + 1, productModel); | ||||||
|         productList.add(product); |         PRODUCT_LIST.add(product); | ||||||
|         return product; |         return product; | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     @Override |     @Override | ||||||
|     public Product update(Integer productId, ProductModel productModel) { |     public Product update(Integer productId, ProductModel productModel) { | ||||||
|         Product product =  getProduct(productId); |         Product product = getProduct(productId); | ||||||
|         if (product != null){ |         if (product != null) { | ||||||
|             update(product, productModel); |             update(product, productModel); | ||||||
|         } |         } | ||||||
|         return product; |         return product; | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     private void update(Product product, ProductModel productModel){ |     private void update(Product product, ProductModel productModel) { | ||||||
|         if (productModel != null) { |         if (productModel != null) { | ||||||
|             System.out.println(productModel.toString()); |             System.out.println(productModel); | ||||||
|             Optional.ofNullable(productModel.getName()).ifPresent(product::setName); |             Optional.ofNullable(productModel.getName()).ifPresent(product::setName); | ||||||
|             Optional.ofNullable(productModel.getDescription()).ifPresent(product::setDescription); |             Optional.ofNullable(productModel.getDescription()).ifPresent(product::setDescription); | ||||||
|             Optional.ofNullable(productModel.getCurrency()).ifPresent(product::setCurrency); |             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.getStock()).ifPresent(product::setStock); | ||||||
|             Optional.ofNullable(productModel.getStatus()).ifPresent(product::setStatus); |             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); |             Optional.ofNullable(productModel.getPrice()).ifPresent(product::setPrice); | ||||||
|         } |         } | ||||||
|     } |     } | ||||||
| @ -0,0 +1,11 @@ | |||||||
|  | server: | ||||||
|  |   port: 8081 | ||||||
|  | 
 | ||||||
|  | spring: | ||||||
|  |   graphql: | ||||||
|  |     schema: | ||||||
|  |       locations: classpath:graphql-vs-rest/ | ||||||
|  | 
 | ||||||
|  |   jackson: | ||||||
|  |     parser: | ||||||
|  |       allow-unquoted-control-chars: true | ||||||
| @ -5,20 +5,20 @@ type Product { | |||||||
|     status: String |     status: String | ||||||
|     currency: String! |     currency: String! | ||||||
|     price: Float |     price: Float | ||||||
|     image_url: [String] |     imageUrls: [String] | ||||||
|     video_url: [String] |     videoUrls: [String] | ||||||
|     stock: Int |     stock: Int | ||||||
|     average_rating: Float |     averageRating: Float | ||||||
|     orders:[Order] |     orders:[Order] | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| type Order{ | type Order{ | ||||||
|     id:ID |     id:ID | ||||||
|     product_id:Int |     productId:Int | ||||||
|     customer_uuid:String |     customerId:String | ||||||
|     address:String |     address:String | ||||||
|     status:String |     status:String | ||||||
|     creation_date:String |     creationDate:String | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| input ProductModel { | input ProductModel { | ||||||
| @ -27,8 +27,8 @@ input ProductModel { | |||||||
|     status: String |     status: String | ||||||
|     currency: String! |     currency: String! | ||||||
|     price: Float |     price: Float | ||||||
|     image_url: [String] |     imageUrls: [String] | ||||||
|     video_url: [String] |     videoUrls: [String] | ||||||
|     stock: Int |     stock: Int | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| @ -38,8 +38,8 @@ input ProductUpdateModel { | |||||||
|     status: String |     status: String | ||||||
|     currency: String |     currency: String | ||||||
|     price: Float |     price: Float | ||||||
|     image_url: [String] |     imageUrls: [String] | ||||||
|     video_url: [String] |     videoUrls: [String] | ||||||
|     stock: Int |     stock: Int | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| @ -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) |  | ||||||
| @ -1,46 +0,0 @@ | |||||||
| <?xml version="1.0" encoding="UTF-8"?> |  | ||||||
| <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"> |  | ||||||
|     <modelVersion>4.0.0</modelVersion> |  | ||||||
|     <artifactId>spring-boot-libraries-comparison</artifactId> |  | ||||||
| 
 |  | ||||||
|     <parent> |  | ||||||
|         <groupId>com.baeldung.spring-boot-modules</groupId> |  | ||||||
|         <artifactId>spring-boot-modules</artifactId> |  | ||||||
|         <version>1.0.0-SNAPSHOT</version> |  | ||||||
|     </parent> |  | ||||||
| 
 |  | ||||||
|     <dependencies> |  | ||||||
|         <dependency> |  | ||||||
|             <groupId>org.springframework.boot</groupId> |  | ||||||
|             <artifactId>spring-boot-starter-web</artifactId> |  | ||||||
|         </dependency> |  | ||||||
|         <dependency> |  | ||||||
|             <groupId>org.springframework.data</groupId> |  | ||||||
|             <artifactId>spring-data-jpa</artifactId> |  | ||||||
|         </dependency> |  | ||||||
|         <dependency> |  | ||||||
|             <groupId>com.graphql-java</groupId> |  | ||||||
|             <artifactId>graphql-spring-boot-starter</artifactId> |  | ||||||
|             <version>${graphql-spring-boot-starter.version}</version> |  | ||||||
|         </dependency> |  | ||||||
|         <dependency> |  | ||||||
|             <groupId>com.graphql-java</groupId> |  | ||||||
|             <artifactId>graphql-java-tools</artifactId> |  | ||||||
|             <version>${graphql-java-tools.version}</version> |  | ||||||
|         </dependency> |  | ||||||
|         <dependency> |  | ||||||
|             <groupId>com.graphql-java</groupId> |  | ||||||
|             <artifactId>graphiql-spring-boot-starter</artifactId> |  | ||||||
|             <version>${graphql-spring-boot-starter.version}</version> |  | ||||||
|         </dependency> |  | ||||||
|     </dependencies> |  | ||||||
| 
 |  | ||||||
| 
 |  | ||||||
|     <properties> |  | ||||||
|         <graphql-spring-boot-starter.version>5.0.2</graphql-spring-boot-starter.version> |  | ||||||
|         <graphql-java-tools.version>5.2.4</graphql-java-tools.version> |  | ||||||
|     </properties> |  | ||||||
| 
 |  | ||||||
| </project> |  | ||||||
| @ -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); |  | ||||||
|     } |  | ||||||
| } |  | ||||||
| @ -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); |  | ||||||
|     } |  | ||||||
| } |  | ||||||
| @ -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<Product> { |  | ||||||
|     private OrderRepository orderRepository; |  | ||||||
|     public ProductResolver(OrderRepository orderRepository){ |  | ||||||
|         this.orderRepository = orderRepository; |  | ||||||
|     } |  | ||||||
|     public List<Order> getOrders(Product product){ |  | ||||||
|         return orderRepository.getOrdersByProduct(product.getId()); |  | ||||||
|     } |  | ||||||
| } |  | ||||||
| @ -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<Product> getProducts(int pageSize, int pageNumber) { |  | ||||||
|         return productRepository.getProducts(pageSize, pageNumber); |  | ||||||
|     } |  | ||||||
| 
 |  | ||||||
|     public Product getProduct(int id) { |  | ||||||
|         return productRepository.getProduct(id); |  | ||||||
|     } |  | ||||||
| 
 |  | ||||||
| 
 |  | ||||||
| } |  | ||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user