graphql-spqr 中的自定义标量类型
Custom Scalar Type in graphql-spqr
我将graphql-spqr,java.util.Date定义为Scalar。是否可以覆盖 java.util.Date 的 serialization/deserialization 以获得日期的不同字符串表示形式?
此 中提到的 ScalarStrategy 已在最新版本中删除。
public class Order {
private String id;
private Date orderDate; //GraphQLScalarType "Date"
public Order() {
}
public Order(String id, String bookId, Date orderDate) {
this.id = id;
this.orderDate = orderDate;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public Date getOrderDate() {
return orderDate;
}
public void setOrderDate(Date orderDate) {
this.orderDate = orderDate;
}
}
GraphQL 响应:
{
"data": {
"createOrder": {
"id": "74e4816c-f850-4d63-9855-e4601fa125f4",
"orderDate": "2019-05-26T08:25:01.349Z", // --> 2019-05-26
}
}
}
ScalarStrategy
不是实现您想要的目标的正确方法。当您想更改 Java 类型映射到 GraphQL 的方式时,您通常会提供一个新的(或自定义现有的)TypeMapper
.
查看现有的 Date
标量实现并以类似的方式实现您自己的。然后实现一个自定义 TypeMapper
,它总是 returns 来自 toGraphQLType
和 toGraphQLInputType
方法的该标量的静态实例。
public class CustomTypeMapper implements TypeMapper {
private static final GraphQLScalarType GraphQLCustomDate = ...;
@Override
public GraphQLOutputType toGraphQLType(...) {
return GraphQLCustomDate;
}
@Override
public GraphQLInputType toGraphQLInputType(...) {
return GraphQLCustomDate;
}
@Override
public boolean supports(AnnotatedType type) {
return type.getType() == Date.class; // This mapper only deals with Date
}
}
要注册它,调用generator.withTypeMappers(new CustomTypeMapper()
。
也就是说,由于您只是想切断时间部分,因此最好在此处使用 LocalDate
。您可以通过注册 TypeAdapter
(它只不过是一个映射器 + 转换器)让 SPQR 透明地做到这一点,但是如上所述的简单映射器是您的情况下更有效的解决方案。如果您仍然决定采用适配器方式,则可以继承 AbstractTypeAdapter<Date, LocalDate>
并实现转换逻辑(应该很简单)。通过 generator.withTypeAdapters
注册或将其分别注册为映射器和转换器。
我将graphql-spqr,java.util.Date定义为Scalar。是否可以覆盖 java.util.Date 的 serialization/deserialization 以获得日期的不同字符串表示形式?
此
public class Order {
private String id;
private Date orderDate; //GraphQLScalarType "Date"
public Order() {
}
public Order(String id, String bookId, Date orderDate) {
this.id = id;
this.orderDate = orderDate;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public Date getOrderDate() {
return orderDate;
}
public void setOrderDate(Date orderDate) {
this.orderDate = orderDate;
}
}
GraphQL 响应:
{
"data": {
"createOrder": {
"id": "74e4816c-f850-4d63-9855-e4601fa125f4",
"orderDate": "2019-05-26T08:25:01.349Z", // --> 2019-05-26
}
}
}
ScalarStrategy
不是实现您想要的目标的正确方法。当您想更改 Java 类型映射到 GraphQL 的方式时,您通常会提供一个新的(或自定义现有的)TypeMapper
.
查看现有的 Date
标量实现并以类似的方式实现您自己的。然后实现一个自定义 TypeMapper
,它总是 returns 来自 toGraphQLType
和 toGraphQLInputType
方法的该标量的静态实例。
public class CustomTypeMapper implements TypeMapper {
private static final GraphQLScalarType GraphQLCustomDate = ...;
@Override
public GraphQLOutputType toGraphQLType(...) {
return GraphQLCustomDate;
}
@Override
public GraphQLInputType toGraphQLInputType(...) {
return GraphQLCustomDate;
}
@Override
public boolean supports(AnnotatedType type) {
return type.getType() == Date.class; // This mapper only deals with Date
}
}
要注册它,调用generator.withTypeMappers(new CustomTypeMapper()
。
也就是说,由于您只是想切断时间部分,因此最好在此处使用 LocalDate
。您可以通过注册 TypeAdapter
(它只不过是一个映射器 + 转换器)让 SPQR 透明地做到这一点,但是如上所述的简单映射器是您的情况下更有效的解决方案。如果您仍然决定采用适配器方式,则可以继承 AbstractTypeAdapter<Date, LocalDate>
并实现转换逻辑(应该很简单)。通过 generator.withTypeAdapters
注册或将其分别注册为映射器和转换器。