背景
Graph-java 在包中提供了除规范要求的几种类型,还新增了几种java特有的类型,比如BigDecimal,Byte。
当然我们也可以自己定义Scalar,接下来,以LocalDateTime为例,我们来实现自定义的Scalar。
参考其他built-定义的类型,自定义的Type需要实现三个方法,分别来序列化输入和输出。
GraphQLDate
package com.tangbaobao.graphql.service;
import graphql.schema.*;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
/**
* @author tangxuejun
* @version 2020/4/5 5:50 下午
*/
public class GraphQLDate extends GraphQLScalarType {
public static final String YYYY_MM_DD_HH_MM = "yyyy-MM-dd HH:mm";
public GraphQLDate() {
super("GraphQLDate", "GraphQLDate type", new Coercing<LocalDateTime, String>() {
@Override
public String serialize(Object dataFetcherResult) throws CoercingSerializeException {
LocalDateTime localDateTime = (LocalDateTime) dataFetcherResult;
return localDateTime.format(DateTimeFormatter.ofPattern(YYYY_MM_DD_HH_MM))