简介
我一直试图解决这个问题很长一段时间了,我已经搜索了awnsers,并尝试了许多解决方案,但都失败了,我使用的是Spring Boot 3.0.0版,以及spring-boot-starter-graphql 3.0.0版,我将提供(我相信是)所有相关信息来解决这个问题。
文件
pom.xml文件
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.0.0</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>gorgeousSandwich</groupId>
<artifactId>promotion</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>Promotion Microservice</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>17</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.hibernate.validator</groupId>
<artifactId>hibernate-validator</artifactId>
<version>8.0.0.Final</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-graphql</artifactId>
<version>3.0.0</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webflux</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.graphql</groupId>
<artifactId>spring-graphql-test</artifactId>
<scope>test</scope>
</dependency>
<!-- OPEN API 3 (Swagger) -->
<dependency>
<groupId>org.springdoc</groupId>
<artifactId>springdoc-openapi-ui</artifactId>
<version>1.6.12</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</project>
PromotionController.java
@AllArgsConstructor
@Controller
public class PromotionController {
private static final Logger LOGGER = LoggerFactory.getLogger(PromotionController.class);
private final IPromotionService service;
private final IPromotionMapper mapper;
@MutationMapping
public GraphQLPromotionDTO createPromotion(@Argument PromotionInput promotion) {
try {
PromotionDTO promotionDTO = promotion.toDTO();
LOGGER.trace(String.format("Requesting the creation of a new promotion (%s)", promotionDTO));
promotionDTO = service.createPromotion(promotionDTO);
LOGGER.info("Order successfully created");
return mapper.toGraphQLDTO(promotionDTO);
} catch (Exception e) {
LOGGER.error("Could not create Shop!", e);
return null;
}
}
@QueryMapping
public List<GraphQLPromotionDTO> listAllPromotions() {
System.out.println("Hehehe");
Iterable<PromotionDTO> itr = service.getAll();
List<GraphQLPromotionDTO> l = new ArrayList<>();
itr.forEach(dto -> l.add(mapper.toGraphQLDTO(dto)));
LOGGER.info("Retrieving all promotions");
return l;
}
@QueryMapping
public String health() {
return "All systems Online!";
}
record PromotionInput(Float percentage, String from, String to, String shopId, PromotionType promotionType) {
PromotionDTO toDTO() throws ParseException {
SimpleDateFormat format = new SimpleDateFormat("dd/MM/yyyy");
return new PromotionDTO(null, percentage, format.parse(from), format.parse(to), Long.parseLong(shopId), promotionType);
}
}
}
模式.图形
schema {
query: Query
mutation: Mutator
}
type Mutator{
createPromotion (
promotion: PromotionInput
) : GraphQLPromotionDTO
}
type Query{
listAllPromotions : [GraphQLPromotionDTO],
health: String
}
input PromotionInput{
percentage: Float
from: String
to: String
shopId: ID
promotionType: PromotionType
}
enum PromotionType{
GLOBAL,
LOCAL
}
type GraphQLPromotionDTO {
id: ID
percentage : Float
from : String
to : String
shopId : ID
promotionType: PromotionType
}
Promotion.java
package gorgeousSandwich.promotion.Domain;
import gorgeousSandwich.promotion.Shared.domain.patterns.IAggregateRoot;
import gorgeousSandwich.promotion.Shared.domain.patterns.IEntity;
import gorgeousSandwich.promotion.Shared.domain.patterns.IEntityId;
import gorgeousSandwich.promotion.Shared.domain.valueobjects.Percentage;
import gorgeousSandwich.promotion.Shared.domain.valueobjects.TimeOfEffect;
import jakarta.persistence.*;
import org.springframework.data.annotation.Id;
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
@Entity
public abstract class Promotion implements IAggregateRoot<PromotionId> {
@EmbeddedId
private PromotionId id;
@Embedded
private TimeOfEffect timeOfEffect;
@Embedded
private Percentage percentage;
@Enumerated
private PromotionType type;
Promotion(TimeOfEffect timeOfEffect, Percentage percentage, PromotionType type) {
this.id = new PromotionId();
this.timeOfEffect = timeOfEffect;
this.percentage = percentage;
this.type=type;
}
Promotion(PromotionId id, TimeOfEffect timeOfEffect, Percentage percentage, PromotionType type) {
this.id = id;
this.timeOfEffect = timeOfEffect;
this.percentage = percentage;
this.type=type;
}
protected Promotion() {
}
@Override
public boolean sameAs(IEntity<? extends IEntityId> otherEntity) {
if (otherEntity instanceof Promotion) {
Promotion otherPromotion = ((Promotion) otherEntity);
return obtainId().id().equals(otherPromotion.obtainId().id());
}
return false;
}
@Override
public PromotionId obtainId() {
return id;
}
public TimeOfEffect getTimeOfEffect() {
return timeOfEffect;
}
public Percentage getPercentage() {
return percentage;
}
public PromotionType getType() {
return type;
}
}
GraphQLPromotionDTO.java
package gorgeousSandwich.promotion.Domain;
import lombok.AccessLevel;
import lombok.AllArgsConstructor;
@AllArgsConstructor(access = AccessLevel.PUBLIC)
public class GraphQLPromotionDTO {
public String id;
public float percentage;
public String from;
public String to;
public String shopId;
public PromotionType promotionType;
}
PromotionType.java
package gorgeousSandwich.promotion.Domain;
public enum PromotionType {
LOCAL,
GLOBAL;
}
感谢所有的帮助
∮我已经尝试过的∮
我曾尝试:
- 将架构中DTO的类型与代码匹配
- @模式Map而不是 @query Map和/或@突变Map
- 调试任何端点(但都不起作用)
编辑
好的,我已经取得了一些进展。我可以执行查询,但是变异仍然不起作用。
1条答案
按热度按时间3z6pesqy1#
对于那些和我有同样问题的人,我通过学习拼写找到了解决办法。
突变体
必须更改为
突变
结果:
PS:为每种类型添加了无效验证