gpt4 book ai didi

java - GraphQL Java 未正确检测 kotlin 枚举

转载 作者:行者123 更新时间:2023-11-30 05:27:01 25 4
gpt4 key购买 nike

我正在使用 graphql spring boot kickstarter - https://github.com/graphql-java-kickstart/graphql-spring-boot为了创建一个 spring graphql 服务。我正在使用 kotlin 构建实际的服务。

我在我的 graphql 服务中声明了一个枚举类型,我也在我的 kotlin 代码中声明了该类型。这是我为另一个枚举(TimeUnit)所做的事情,但对于新枚举(WeekDay)我收到错误

我尝试为枚举类型创建显式解析器,但无济于事。

package com.houseshare.server.data.entity

import com.vladmihalcea.hibernate.type.array.IntArrayType
import org.hibernate.annotations.GenericGenerator
import org.hibernate.annotations.TypeDef
import org.hibernate.annotations.TypeDefs
import java.util.*
import javax.persistence.*
import javax.validation.constraints.Size


@Entity
@Table(name = "scheduler")
@TypeDefs(
TypeDef(
typeClass = IntArrayType::class,
defaultForType = Array<Int>::class
)
)
class Scheduler(
@Id @GeneratedValue(generator = "UUID")
@GenericGenerator(
name = "UUID",
strategy = "org.hibernate.id.UUIDGenerator"
) val id: UUID = UUID.randomUUID(),
@Column(length = 50)
@Size(max = 50)
var name: String,
@Column
var duration: Int? = null,
@Column
@Enumerated(EnumType.ORDINAL)
var durationUnit: TimeUnit? = null,
@Column
var frequency: Int? = null,
@Column
@Enumerated(EnumType.ORDINAL)
var frequencyUnit: TimeUnit? = null,
@Column
var scheduleDays: Array<Int>? = null,
@Column
@Temporal(TemporalType.TIME)
var scheduleTime: Date? = null,
@Column
var effort: Int? = null
) {

override fun toString(): String {
return "Scheduler [id= + ${this.id}]";
}

fun getScheduleDaysList() : List<WeekDay>? {
return scheduleDays?.map { WeekDay.fromInt(it) }
}

fun setScheduleDaysList(daysOfWeek : List<WeekDay>?) {
if(daysOfWeek == null) {
this.scheduleDays = null;
} else {
this.scheduleDays = daysOfWeek.map { it.value }.toTypedArray();
}
}

}

enum class TimeUnit {
MINUTE, HOUR, DAY, WEEK, MONTH
}

enum class WeekDay(val value: Int) {
MONDAY(1),
TUESDAY(2),
WEDNESDAY(3),
THURSDAY(4),
FRIDAY(5),
SATURDAY(6),
SUNDAY(7);

companion object {
fun fromInt(value: Int) = WeekDay.values().first { it.value == value }
}
}

Graphql 架构:

type Scheduler {
id: ID!,
name: String!
duration: Int,
durationUnit: TimeUnit,
frequency: Int,
frequencyUnit: TimeUnit,
scheduleDays: [WeekDay],
scheduleTime: Time
}

type Time

enum TimeUnit {
MINUTE,
HOUR,
DAY,
WEEK,
MONTH
}

enum WeekDay {
MONDAY,
TUESDAY,
WEDNESDAY,
THURSDAY,
FRIDAY,
SATURDAY,
SUNDAY
}

生成的错误消息是:类型“WeekDay”在 GraphQL 架构中被声明为枚举,但不是 Java 枚举!

最佳答案

通过为 Scheduler 创建自定义解析器并在那里声明类型和转换来修复此问题

关于java - GraphQL Java 未正确检测 kotlin 枚举,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58354926/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com