gpt4 book ai didi

java - 在 graphql 模式中使用日期类型

转载 作者:行者123 更新时间:2023-12-02 01:35:03 25 4
gpt4 key购买 nike

我想向 graphql 方案引入标量类型并为其提供解析器。

我相信我需要做的是为新类型提供相应的解析器,如here所述

我使用 spring boot starter 来实现服务器。com.graphql-java:graphql-spring-boot-starter:5.0.2

schema.graphqls:

type Query {
pets(last: Int): [Pet]
}
type Pet {
id: ID
name: String
}

QueryResolver:

@Component
public class Query implements GraphQLQueryResolver {

@Autowired
PetStore petStore;

public Pet pet(long id) {
return petStore.pet(id);
}

public List<Pet> pets(Integer last) {
return petStore.pets().stream()
.limit(last != null ? last : Integer.MAX_VALUE)
.collect(Collectors.toList());
}
}

我相信有一种方法可以更新这样的方案:

type Pet {
id: ID
name: String
dateOfBirth: LocalDateTime
}

scalar LocalDateTime

并提供解析器来定义如何处理新字段值,如下所示:

@Component
public class DateResolver extends GraphQLScalarType {

// serialize/deserialize logic

}

不幸的是我得到以下异常:

com.coxautodev.graphql.tools.SchemaClassScannerError: Expected a user-defined GraphQL scalar type with name 'LocalDateTime' but found none!

最佳答案

经过一番研究,我在 graphql-java-9.2.jar!/graphql/Scalars.class 中找到了 java 类型的解析器,并从中得到了启发。

关于java - 在 graphql 模式中使用日期类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57525225/

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