[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-libraries</module>
|
||||
<module>spring-boot-libraries-2</module>
|
||||
<module>spring-boot-libraries-comparison</module>
|
||||
<module>spring-boot-logging-log4j2</module>
|
||||
<module>spring-boot-mvc</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)
|
||||
- [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
|
||||
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
|
@ -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;
|
||||
|
||||
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;
|
||||
|
|
@ -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<Product> getProducts(Pageable pageable){
|
||||
public List<Product> 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);
|
||||
}
|
||||
}
|
|
@ -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 {
|
||||
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;
|
||||
}
|
||||
}
|
|
@ -11,10 +11,10 @@ public class Product {
|
|||
private String status;
|
||||
private String currency;
|
||||
private Double price;
|
||||
private List<String> image_url;
|
||||
private List<String> video_url;
|
||||
private List<String> imageUrls;
|
||||
private List<String> 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<String> getImage_url() {
|
||||
return image_url;
|
||||
public List<String> getImageUrls() {
|
||||
return imageUrls;
|
||||
}
|
||||
|
||||
public void setImage_url(List<String> image_url) {
|
||||
this.image_url = image_url;
|
||||
public void setImageUrls(List<String> imageUrls) {
|
||||
this.imageUrls = imageUrls;
|
||||
}
|
||||
|
||||
public List<String> getVideo_url() {
|
||||
return video_url;
|
||||
public List<String> getVideoUrls() {
|
||||
return videoUrls;
|
||||
}
|
||||
|
||||
public void setVideo_url(List<String> video_url) {
|
||||
this.video_url = video_url;
|
||||
public void setVideoUrls(List<String> 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;
|
||||
}
|
||||
}
|
|
@ -8,8 +8,8 @@ public class ProductModel {
|
|||
private String status;
|
||||
private String currency;
|
||||
private Double price;
|
||||
private List<String> image_url;
|
||||
private List<String> video_url;
|
||||
private List<String> imageUrls;
|
||||
private List<String> videoUrls;
|
||||
private Integer stock;
|
||||
|
||||
public String getName() {
|
||||
|
@ -52,20 +52,20 @@ public class ProductModel {
|
|||
this.price = price;
|
||||
}
|
||||
|
||||
public List<String> getImage_url() {
|
||||
return image_url;
|
||||
public List<String> getImageUrls() {
|
||||
return imageUrls;
|
||||
}
|
||||
|
||||
public void setImage_url(List<String> image_url) {
|
||||
this.image_url = image_url;
|
||||
public void setImageUrls(List<String> imageUrls) {
|
||||
this.imageUrls = imageUrls;
|
||||
}
|
||||
|
||||
public List<String> getVideo_url() {
|
||||
return video_url;
|
||||
public List<String> getVideoUrls() {
|
||||
return videoUrls;
|
||||
}
|
||||
|
||||
public void setVideo_url(List<String> video_url) {
|
||||
this.video_url = video_url;
|
||||
public void setVideoUrls(List<String> 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 +
|
||||
'}';
|
||||
}
|
|
@ -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;
|
||||
|
|
@ -13,24 +13,25 @@ import java.util.stream.Collectors;
|
|||
@Repository
|
||||
public class OrderRepositoryImpl implements OrderRepository {
|
||||
|
||||
private static List<Order> orderList = new ArrayList<>();
|
||||
private static final List<Order> 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<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 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<Product> productList = new ArrayList<>();
|
||||
private static final List<Product> 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<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
|
||||
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){
|
||||
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);
|
||||
}
|
||||
}
|
|
@ -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
|
||||
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
|
||||
}
|
||||
|
|
@ -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…
Reference in New Issue