Bael 5303 graphqlvs rest (#11715)

* GraphQL and REST Comparison

* Updating README

* Reformatting code
This commit is contained in:
Muhammad Abdullah Azam Khan 2022-02-22 13:28:27 +04:00 committed by GitHub
parent c8231b7c60
commit 74b2b2e910
19 changed files with 695 additions and 0 deletions

View File

@ -51,6 +51,7 @@
<!-- <module>spring-boot-keycloak</module> --> <!-- Fixing under JAVA-8271 --> <!-- <module>spring-boot-keycloak</module> --> <!-- Fixing under JAVA-8271 -->
<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-multiple-datasources</module> <module>spring-boot-multiple-datasources</module>
<module>spring-boot-mvc</module> <module>spring-boot-mvc</module>

View File

@ -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/)

View File

@ -0,0 +1,46 @@
<?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>

View File

@ -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);
}
}

View File

@ -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);
}
}

View File

@ -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<Order> getOrders(@RequestParam("product-id") Integer productId){
return orderRepository.getOrdersByProduct(productId);
}
}

View File

@ -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<Product> 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);
}
}

View File

@ -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;
}
}

View File

@ -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<String> image_url;
private List<String> 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<String> getImage_url() {
return image_url;
}
public void setImage_url(List<String> image_url) {
this.image_url = image_url;
}
public List<String> getVideo_url() {
return video_url;
}
public void setVideo_url(List<String> 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;
}
}

View File

@ -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<String> image_url;
private List<String> 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<String> getImage_url() {
return image_url;
}
public void setImage_url(List<String> image_url) {
this.image_url = image_url;
}
public List<String> getVideo_url() {
return video_url;
}
public void setVideo_url(List<String> 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 +
'}';
}
}

View File

@ -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<Order> getOrdersByProduct(Integer productId);
}

View File

@ -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<Product> getProducts(Integer pageSize, Integer pageNumber);
Product getProduct(Integer id);
Product save(ProductModel productModel);
Product update(Integer productId, ProductModel productModel);
}

View File

@ -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<Order> 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<Order> getOrdersByProduct(Integer productId) {
return orderList.stream().filter(order -> order.getProduct_id().equals(productId)).collect(Collectors.toList());
}
}

View File

@ -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<Product> 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<Product> 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);
}
}
}

View File

@ -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);
}
}

View File

@ -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<Product> {
private OrderRepository orderRepository;
public ProductResolver(OrderRepository orderRepository){
this.orderRepository = orderRepository;
}
public List<Order> getOrders(Product product){
return orderRepository.getOrdersByProduct(product.getId());
}
}

View File

@ -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<Product> getProducts(int pageSize, int pageNumber) {
return productRepository.getProducts(pageSize, pageNumber);
}
public Product getProduct(int id) {
return productRepository.getProduct(id);
}
}

View File

@ -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!
}