From 74b2b2e910eaf521494d377dd55f3b3699ffc76c Mon Sep 17 00:00:00 2001 From: Muhammad Abdullah Azam Khan Date: Tue, 22 Feb 2022 13:28:27 +0400 Subject: [PATCH] Bael 5303 graphqlvs rest (#11715) * GraphQL and REST Comparison * Updating README * Reformatting code --- spring-boot-modules/pom.xml | 1 + .../README.md | 7 ++ .../spring-boot-libraries-comparison/pom.xml | 46 +++++++ .../GraphqlVsRestApplication.java | 19 +++ .../configuration/GraphqlConfiguration.java | 35 ++++++ .../controller/OrderController.java | 25 ++++ .../controller/ProductController.java | 38 ++++++ .../baeldung/graphqlvsrest/entity/Order.java | 58 +++++++++ .../graphqlvsrest/entity/Product.java | 115 ++++++++++++++++++ .../graphqlvsrest/model/ProductModel.java | 92 ++++++++++++++ .../repository/OrderRepository.java | 11 ++ .../repository/ProductRepository.java | 14 +++ .../repository/impl/OrderRepositoryImpl.java | 36 ++++++ .../impl/ProductRepositoryImpl.java | 74 +++++++++++ .../graphqlvsrest/resolver/Mutation.java | 22 ++++ .../resolver/ProductResolver.java | 18 +++ .../graphqlvsrest/resolver/Query.java | 27 ++++ .../src/main/resources/application.properties | 0 .../main/resources/graphql/schema.graphqls | 57 +++++++++ 19 files changed, 695 insertions(+) create mode 100644 spring-boot-modules/spring-boot-libraries-comparison/README.md create mode 100644 spring-boot-modules/spring-boot-libraries-comparison/pom.xml create mode 100644 spring-boot-modules/spring-boot-libraries-comparison/src/main/java/com/baeldung/graphqlvsrest/GraphqlVsRestApplication.java create mode 100644 spring-boot-modules/spring-boot-libraries-comparison/src/main/java/com/baeldung/graphqlvsrest/configuration/GraphqlConfiguration.java create mode 100644 spring-boot-modules/spring-boot-libraries-comparison/src/main/java/com/baeldung/graphqlvsrest/controller/OrderController.java create mode 100644 spring-boot-modules/spring-boot-libraries-comparison/src/main/java/com/baeldung/graphqlvsrest/controller/ProductController.java create mode 100644 spring-boot-modules/spring-boot-libraries-comparison/src/main/java/com/baeldung/graphqlvsrest/entity/Order.java create mode 100644 spring-boot-modules/spring-boot-libraries-comparison/src/main/java/com/baeldung/graphqlvsrest/entity/Product.java create mode 100644 spring-boot-modules/spring-boot-libraries-comparison/src/main/java/com/baeldung/graphqlvsrest/model/ProductModel.java create mode 100644 spring-boot-modules/spring-boot-libraries-comparison/src/main/java/com/baeldung/graphqlvsrest/repository/OrderRepository.java create mode 100644 spring-boot-modules/spring-boot-libraries-comparison/src/main/java/com/baeldung/graphqlvsrest/repository/ProductRepository.java create mode 100644 spring-boot-modules/spring-boot-libraries-comparison/src/main/java/com/baeldung/graphqlvsrest/repository/impl/OrderRepositoryImpl.java create mode 100644 spring-boot-modules/spring-boot-libraries-comparison/src/main/java/com/baeldung/graphqlvsrest/repository/impl/ProductRepositoryImpl.java create mode 100644 spring-boot-modules/spring-boot-libraries-comparison/src/main/java/com/baeldung/graphqlvsrest/resolver/Mutation.java create mode 100644 spring-boot-modules/spring-boot-libraries-comparison/src/main/java/com/baeldung/graphqlvsrest/resolver/ProductResolver.java create mode 100644 spring-boot-modules/spring-boot-libraries-comparison/src/main/java/com/baeldung/graphqlvsrest/resolver/Query.java create mode 100644 spring-boot-modules/spring-boot-libraries-comparison/src/main/resources/application.properties create mode 100644 spring-boot-modules/spring-boot-libraries-comparison/src/main/resources/graphql/schema.graphqls diff --git a/spring-boot-modules/pom.xml b/spring-boot-modules/pom.xml index 4925530a35..c1a96a0604 100644 --- a/spring-boot-modules/pom.xml +++ b/spring-boot-modules/pom.xml @@ -51,6 +51,7 @@ spring-boot-libraries spring-boot-libraries-2 + spring-boot-libraries-comparison spring-boot-logging-log4j2 spring-boot-multiple-datasources spring-boot-mvc diff --git a/spring-boot-modules/spring-boot-libraries-comparison/README.md b/spring-boot-modules/spring-boot-libraries-comparison/README.md new file mode 100644 index 0000000000..3efdac2a4c --- /dev/null +++ b/spring-boot-modules/spring-boot-libraries-comparison/README.md @@ -0,0 +1,7 @@ +## 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/) \ No newline at end of file diff --git a/spring-boot-modules/spring-boot-libraries-comparison/pom.xml b/spring-boot-modules/spring-boot-libraries-comparison/pom.xml new file mode 100644 index 0000000000..59d0e75be3 --- /dev/null +++ b/spring-boot-modules/spring-boot-libraries-comparison/pom.xml @@ -0,0 +1,46 @@ + + + 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/GraphqlVsRestApplication.java b/spring-boot-modules/spring-boot-libraries-comparison/src/main/java/com/baeldung/graphqlvsrest/GraphqlVsRestApplication.java new file mode 100644 index 0000000000..29a3ef1e0f --- /dev/null +++ b/spring-boot-modules/spring-boot-libraries-comparison/src/main/java/com/baeldung/graphqlvsrest/GraphqlVsRestApplication.java @@ -0,0 +1,19 @@ +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.security.servlet.SecurityAutoConfiguration; +import org.springframework.context.annotation.Import; + +@SpringBootApplication +@Import(GraphqlConfiguration.class) +@EnableAutoConfiguration(exclude = {SecurityAutoConfiguration.class}) +public class GraphqlVsRestApplication { + + public static void main(String[] args) { + SpringApplication.run(GraphqlVsRestApplication.class, args); + } + +} \ 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 new file mode 100644 index 0000000000..c100a03143 --- /dev/null +++ b/spring-boot-modules/spring-boot-libraries-comparison/src/main/java/com/baeldung/graphqlvsrest/configuration/GraphqlConfiguration.java @@ -0,0 +1,35 @@ +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/controller/OrderController.java b/spring-boot-modules/spring-boot-libraries-comparison/src/main/java/com/baeldung/graphqlvsrest/controller/OrderController.java new file mode 100644 index 0000000000..14f0468bbd --- /dev/null +++ b/spring-boot-modules/spring-boot-libraries-comparison/src/main/java/com/baeldung/graphqlvsrest/controller/OrderController.java @@ -0,0 +1,25 @@ +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 java.util.List; + +@RestController +@RequestMapping("order") +public class OrderController { + + @Autowired + OrderRepository orderRepository; + + @GetMapping() + public List getOrders(@RequestParam("product-id") Integer productId){ + return orderRepository.getOrdersByProduct(productId); + } +} 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-libraries-comparison/src/main/java/com/baeldung/graphqlvsrest/controller/ProductController.java new file mode 100644 index 0000000000..2fdee8765a --- /dev/null +++ b/spring-boot-modules/spring-boot-libraries-comparison/src/main/java/com/baeldung/graphqlvsrest/controller/ProductController.java @@ -0,0 +1,38 @@ +package com.baeldung.graphqlvsrest.controller; + +import com.baeldung.graphqlvsrest.entity.Product; +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 java.util.List; + +@RestController +@RequestMapping("product") +public class ProductController { + + @Autowired + ProductRepository productRepository; + + @GetMapping + public List getProducts(Pageable pageable){ + return productRepository.getProducts(pageable.getPageSize(), pageable.getPageNumber()); + } + + @GetMapping("/{product-id}") + public Product getProducts(@PathVariable("product-id") Integer productId){ + return productRepository.getProduct(productId); + } + + @PostMapping + public Product save(@RequestBody ProductModel productModel){ + return productRepository.save(productModel); + } + + @PutMapping("/{product-id}") + public Product update(@PathVariable("product-id") Integer productId, @RequestBody ProductModel productModel){ + return productRepository.update(productId, productModel); + } +} 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-libraries-comparison/src/main/java/com/baeldung/graphqlvsrest/entity/Order.java new file mode 100644 index 0000000000..89606e9897 --- /dev/null +++ b/spring-boot-modules/spring-boot-libraries-comparison/src/main/java/com/baeldung/graphqlvsrest/entity/Order.java @@ -0,0 +1,58 @@ +package com.baeldung.graphqlvsrest.entity; + +public class Order { + private Integer id; + private Integer product_id; + private String customer_uuid; + private String status; + private String address; + private String creation_date; + + public Integer getId() { + return id; + } + + public void setId(Integer id) { + this.id = id; + } + + public Integer getProduct_id() { + return product_id; + } + + public void setProduct_id(Integer product_id) { + this.product_id = product_id; + } + + public String getStatus() { + return status; + } + + public void setStatus(String status) { + this.status = status; + } + + public String getCustomer_uuid() { + return customer_uuid; + } + + public void setCustomer_uuid(String customer_uuid) { + this.customer_uuid = customer_uuid; + } + + public String getAddress() { + return address; + } + + public void setAddress(String address) { + this.address = address; + } + + public String getCreation_date() { + return creation_date; + } + + public void setCreation_date(String creation_date) { + this.creation_date = creation_date; + } +} 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-libraries-comparison/src/main/java/com/baeldung/graphqlvsrest/entity/Product.java new file mode 100644 index 0000000000..2da9244c92 --- /dev/null +++ b/spring-boot-modules/spring-boot-libraries-comparison/src/main/java/com/baeldung/graphqlvsrest/entity/Product.java @@ -0,0 +1,115 @@ +package com.baeldung.graphqlvsrest.entity; + +import com.baeldung.graphqlvsrest.model.ProductModel; + +import java.util.List; + +public class Product { + private Integer id; + private String name; + private String description; + private String status; + private String currency; + private Double price; + private List image_url; + private List video_url; + private Integer stock; + private Float average_rating; + + public Product(Integer id, ProductModel productModel) { + this.id = id; + this.name = productModel.getName(); + this.description = productModel.getDescription(); + 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.status = productModel.getStatus(); + } + + public Product(){ + + } + + public Integer getId() { + return id; + } + + public void setId(Integer id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getDescription() { + return description; + } + + public void setDescription(String description) { + this.description = description; + } + + public String getStatus() { + return status; + } + + public void setStatus(String status) { + this.status = status; + } + + public String getCurrency() { + return currency; + } + + public void setCurrency(String currency) { + this.currency = currency; + } + + public Double getPrice() { + return price; + } + + public void setPrice(Double price) { + this.price = price; + } + + public List getImage_url() { + return image_url; + } + + public void setImage_url(List image_url) { + this.image_url = image_url; + } + + public List getVideo_url() { + return video_url; + } + + public void setVideo_url(List video_url) { + this.video_url = video_url; + } + + public Integer getStock() { + return stock; + } + + public void setStock(Integer stock) { + this.stock = stock; + } + + public Float getAverage_rating() { + return average_rating; + } + + public void setAverage_rating(Float average_rating) { + this.average_rating = average_rating; + } +} 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-libraries-comparison/src/main/java/com/baeldung/graphqlvsrest/model/ProductModel.java new file mode 100644 index 0000000000..db7a3ba54e --- /dev/null +++ b/spring-boot-modules/spring-boot-libraries-comparison/src/main/java/com/baeldung/graphqlvsrest/model/ProductModel.java @@ -0,0 +1,92 @@ +package com.baeldung.graphqlvsrest.model; + +import java.util.List; + +public class ProductModel { + private String name; + private String description; + private String status; + private String currency; + private Double price; + private List image_url; + private List video_url; + private Integer stock; + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getDescription() { + return description; + } + + public void setDescription(String description) { + this.description = description; + } + + public String getStatus() { + return status; + } + + public void setStatus(String status) { + this.status = status; + } + + public String getCurrency() { + return currency; + } + + public void setCurrency(String currency) { + this.currency = currency; + } + + public Double getPrice() { + return price; + } + + public void setPrice(Double price) { + this.price = price; + } + + public List getImage_url() { + return image_url; + } + + public void setImage_url(List image_url) { + this.image_url = image_url; + } + + public List getVideo_url() { + return video_url; + } + + public void setVideo_url(List video_url) { + this.video_url = video_url; + } + + public Integer getStock() { + return stock; + } + + public void setStock(Integer stock) { + this.stock = stock; + } + + @Override + public String toString() { + return "ProductModel{" + + "name='" + name + '\'' + + ", description='" + description + '\'' + + ", status='" + status + '\'' + + ", currency='" + currency + '\'' + + ", price=" + price + + ", image_url=" + image_url + + ", video_url=" + video_url + + ", 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-libraries-comparison/src/main/java/com/baeldung/graphqlvsrest/repository/OrderRepository.java new file mode 100644 index 0000000000..92cc288426 --- /dev/null +++ b/spring-boot-modules/spring-boot-libraries-comparison/src/main/java/com/baeldung/graphqlvsrest/repository/OrderRepository.java @@ -0,0 +1,11 @@ +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; + +public interface OrderRepository { + List getOrdersByProduct(Integer productId); +} 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-libraries-comparison/src/main/java/com/baeldung/graphqlvsrest/repository/ProductRepository.java new file mode 100644 index 0000000000..c0fb12327f --- /dev/null +++ b/spring-boot-modules/spring-boot-libraries-comparison/src/main/java/com/baeldung/graphqlvsrest/repository/ProductRepository.java @@ -0,0 +1,14 @@ +package com.baeldung.graphqlvsrest.repository; + +import com.baeldung.graphqlvsrest.entity.Product; +import com.baeldung.graphqlvsrest.model.ProductModel; + +import java.util.List; + +public interface ProductRepository { + List getProducts(Integer pageSize, Integer pageNumber); + Product getProduct(Integer id); + Product save(ProductModel productModel); + Product update(Integer productId, ProductModel productModel); + +} 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-libraries-comparison/src/main/java/com/baeldung/graphqlvsrest/repository/impl/OrderRepositoryImpl.java new file mode 100644 index 0000000000..e4f316c865 --- /dev/null +++ b/spring-boot-modules/spring-boot-libraries-comparison/src/main/java/com/baeldung/graphqlvsrest/repository/impl/OrderRepositoryImpl.java @@ -0,0 +1,36 @@ +package com.baeldung.graphqlvsrest.repository.impl; + +import com.baeldung.graphqlvsrest.entity.Order; +import com.baeldung.graphqlvsrest.repository.OrderRepository; +import org.springframework.stereotype.Repository; + +import java.util.ArrayList; +import java.util.Date; +import java.util.List; +import java.util.UUID; +import java.util.stream.Collectors; + +@Repository +public class OrderRepositoryImpl implements OrderRepository { + + private static List orderList = new ArrayList<>(); + + public OrderRepositoryImpl() { + 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.setStatus("Delivered"); + orderList.add(order); + } + } + + + @Override + public List getOrdersByProduct(Integer productId) { + return orderList.stream().filter(order -> order.getProduct_id().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-libraries-comparison/src/main/java/com/baeldung/graphqlvsrest/repository/impl/ProductRepositoryImpl.java new file mode 100644 index 0000000000..845472faea --- /dev/null +++ b/spring-boot-modules/spring-boot-libraries-comparison/src/main/java/com/baeldung/graphqlvsrest/repository/impl/ProductRepositoryImpl.java @@ -0,0 +1,74 @@ +package com.baeldung.graphqlvsrest.repository.impl; + +import com.baeldung.graphqlvsrest.entity.Product; +import com.baeldung.graphqlvsrest.model.ProductModel; +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; + +@Repository +public class ProductRepositoryImpl implements ProductRepository { + + private static List productList = new ArrayList<>(); + + public ProductRepositoryImpl() { + 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.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); + } + } + + @Override + public List getProducts(Integer pageSize, Integer pageNumber) { + return productList.stream().skip(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); + } + + @Override + public Product save(ProductModel productModel) { + Product product = new Product(productList.size()+1, productModel); + productList.add(product); + return product; + } + + @Override + public Product update(Integer productId, ProductModel productModel) { + Product product = getProduct(productId); + if (product != null){ + update(product, productModel); + } + return product; + } + + private void update(Product product, ProductModel productModel){ + if (productModel != null) { + System.out.println(productModel.toString()); + 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.getStock()).ifPresent(product::setStock); + Optional.ofNullable(productModel.getStatus()).ifPresent(product::setStatus); + Optional.ofNullable(productModel.getVideo_url()).ifPresent(product::setVideo_url); + Optional.ofNullable(productModel.getPrice()).ifPresent(product::setPrice); + } + } +} 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 new file mode 100644 index 0000000000..3d643f97e6 --- /dev/null +++ b/spring-boot-modules/spring-boot-libraries-comparison/src/main/java/com/baeldung/graphqlvsrest/resolver/Mutation.java @@ -0,0 +1,22 @@ +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 new file mode 100644 index 0000000000..f20b8d5920 --- /dev/null +++ b/spring-boot-modules/spring-boot-libraries-comparison/src/main/java/com/baeldung/graphqlvsrest/resolver/ProductResolver.java @@ -0,0 +1,18 @@ +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 new file mode 100644 index 0000000000..0d218261b2 --- /dev/null +++ b/spring-boot-modules/spring-boot-libraries-comparison/src/main/java/com/baeldung/graphqlvsrest/resolver/Query.java @@ -0,0 +1,27 @@ +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 new file mode 100644 index 0000000000..e69de29bb2 diff --git a/spring-boot-modules/spring-boot-libraries-comparison/src/main/resources/graphql/schema.graphqls b/spring-boot-modules/spring-boot-libraries-comparison/src/main/resources/graphql/schema.graphqls new file mode 100644 index 0000000000..2709510d72 --- /dev/null +++ b/spring-boot-modules/spring-boot-libraries-comparison/src/main/resources/graphql/schema.graphqls @@ -0,0 +1,57 @@ +type Product { + id: ID + name: String! + description: String + status: String + currency: String! + price: Float + image_url: [String] + video_url: [String] + stock: Int + average_rating: Float + orders:[Order] +} + +type Order{ + id:ID + product_id:Int + customer_uuid:String + address:String + status:String + creation_date:String +} + +input ProductModel { + name: String! + description: String + status: String + currency: String! + price: Float + image_url: [String] + video_url: [String] + stock: Int +} + +input ProductUpdateModel { + name: String + description: String + status: String + currency: String + price: Float + image_url: [String] + video_url: [String] + stock: Int +} + + +# The Root Query for the application +type Query { + products(size: Int, page: Int): [Product]! + product(id: Int): Product! +} + +# The Root Mutation for the application +type Mutation { + saveProduct(product: ProductModel) : Product! + updateProduct(id: Int, product: ProductUpdateModel) : Product! +}