修改GraphQL的源码Scalars
程序员文章站
2022-05-16 10:30:08
...
最近在学习graphql的时候,有个表有Date类型的数据,但是在设置字段类型的时候,发现graphql只支持其基本的类型,
如图:
不包含Date类型的数据。
强行使用GarphQLString,输出的数据如下
于是想到自己写一个方法来支持Date类型,仔细的看了一下Scalars源码,自己动手写了一个,
先看下测试结果:
最终写出的代码如下:
MyScalars.java
package com.woodie.modifyoriginal;
import graphql.Scalars;
import graphql.schema.Coercing;
import graphql.schema.CoercingParseLiteralException;
import graphql.schema.GraphQLScalarType;
import java.text.SimpleDateFormat;
import java.util.Date;
public class MyScalars extends Scalars {
/**
* 定义一个新的方法
*/
public static final GraphQLScalarType GraphQLDate = new GraphQLScalarType("Date", "Built-in Date", new Coercing<String, String>() {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
public String serialize(Object input) {
return sdf.format(input);
}
public String parseValue(Object input) {
return this.serialize(sdf.format(input));
}
public String parseLiteral(Object input) {
if (!(input instanceof Date)) {
throw new CoercingParseLiteralException("Expected AST type 'Date' but was ");
} else {
return sdf.format(input);
}
}
});
}
实体类代码:
Employee.java
package com.woodie.entity;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.*;
import java.util.Date;
@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
@TableName("employees")
public class Employee {
// 指定表的id
// 通过type = IdType.AUTO设置id的自动增长, 由于表employees没有设置id为自动增长,因此这里不能设置这个参数,否则报错
//@TableId(value = "emp_no", type = IdType.AUTO)
@TableId(value = "emp_no")
Integer empNo;
Date birthDate;
String firstName;
String lastName;
Date hireDate;
String gender;
}
测试代码
GraphqlDemo.java
这里Date类型的字段传入MyScalars.GraphQLDate自定义的类型
package com.woodie;
import com.woodie.entity.Employee;
import com.woodie.modifyoriginal.MyScalars;
import graphql.ExecutionResult;
import graphql.GraphQL;
import graphql.Scalars;
import graphql.schema.GraphQLArgument;
import graphql.schema.GraphQLFieldDefinition;
import graphql.schema.GraphQLObjectType;
import graphql.schema.GraphQLSchema;
import java.text.SimpleDateFormat;
import java.util.Date;
public class GraphqlDemo {
public static void main(String[] args) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
/**
* type User { #定义对象
*/
GraphQLObjectType employeeObjectType = GraphQLObjectType.newObject()
.name("Employee")
.field(GraphQLFieldDefinition.newFieldDefinition().name("empNo").type(Scalars.GraphQLInt))
.field(GraphQLFieldDefinition.newFieldDefinition().name("birthDate").type(MyScalars.GraphQLDate))
.field(GraphQLFieldDefinition.newFieldDefinition().name("hireDate").type(MyScalars.GraphQLDate))
// .field(GraphQLFieldDefinition.newFieldDefinition().name("birthDate").type(Scalars.GraphQLString))
// .field(GraphQLFieldDefinition.newFieldDefinition().name("hireDate").type(Scalars.GraphQLString))
.field(GraphQLFieldDefinition.newFieldDefinition().name("gender").type(Scalars.GraphQLString))
.field(GraphQLFieldDefinition.newFieldDefinition().name("firstName").type(Scalars.GraphQLString))
.field(GraphQLFieldDefinition.newFieldDefinition().name("lastName").type(Scalars.GraphQLString))
.build();
/**
* user : User #指定对象以及参数类型
*/
GraphQLFieldDefinition userFieldDefinition = GraphQLFieldDefinition.newFieldDefinition()
.name("employee")
.type(employeeObjectType)
// .dataFetcher(new StaticDataFetcher(new User(1L, "张三", 26)))
.argument(GraphQLArgument.newArgument().name("empNo").type(Scalars.GraphQLLong).build())
.dataFetcher(environment -> {
Long id = environment.getArgument("empNo");
// 查询数据库
// 这里需要设置modules中的sources中的language level: 为8-Lambdas.....
return Employee.builder().birthDate(new Date()).empNo(10001).gender("M").firstName("Woodie").lastName("Wang").hireDate(sdf.parse("2222- 03-14")).build();
})
.build();
/**
* type UserQuery { #定义查询的类型
*/
GraphQLObjectType userQueryObjectType = GraphQLObjectType.newObject().name("UserQuery").field(userFieldDefinition).build();
GraphQLSchema graphQLSchema= GraphQLSchema.newSchema().query(userQueryObjectType).build();
GraphQL graphQL = GraphQL.newGraphQL(graphQLSchema).build();
// user(id:1) 这里是设置参数 的意思
String query = "{employee(empNo:10001){empNo,hireDate}}";
ExecutionResult result = graphQL.execute(query);
// System.out.println(result.getErrors());
// System.out.println(result.getData());
// graphql标准化输出数据
System.out.println(result.toSpecification());
}
}