使用GraphQL从MongoDB数据库中获取DateTime时遇到了麻烦。每次我收到Expected的错误时,我们都可以将其转换为'java.time.OffsetDateTime',但它的内容为'LocalDateTime'。 我这样设置模式
scalar DateTime
scalar Date
type Query {
users: [User]
}
type User {
id: ID!
username: String!
password: String!
email: String!
dateOfBirth: Date
dateCreated: DateTime!
}
并像这样设置GraphQL-Java扩展标量
@Component
class scalars() {
@Bean
fun objectMapper(): ObjectMapper? {
return ObjectMapper().registerModule(JavaTimeModule())
.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS)
}
@Bean
fun schemaParserOptions(): SchemaParserOptions? {
return SchemaParserOptions.newOptions()
.objectMapperProvider(PerFieldObjectMapperProvider { fieldDefinition: FieldDefinition? -> objectMapper() }).build()
}
@Bean
fun date(): GraphQLScalarType? {
return ExtendedScalars.Date
}
@Bean
fun datetime(): GraphQLScalarType? {
return ExtendedScalars.DateTime
}
}
日期工作正常,但DateTime不起作用。我将用户设置为
@Document(collection = "users")
data class UserEntity(
var username: String,
var password: String,
var email: String,
var dateOfBirth: LocalDate,
var dateCreated: LocalDateTime
) {
@Id
var id: String = ""
}
所以我想知道我的用户对象的DateTime应该是哪种数据类型,如何正确设置扩展的DateTime标量才能工作,以便可以从MongoDB中检索它?谢谢您的提前帮助
我在数据库中的DateTime也是这样设置的:"dateCreated" : ISODAte("2020-0703T:30:52.237Z")