有一个NPM包,包含一组RFC 3339兼容的日期/时间图ql标量类型;
graphql-iso-date
.
GraphQLScalarType
要以编程方式在GraphQL中构建自己的标量类型,请执行以下操作:
/** Kind is an enum that describes the different kinds of AST nodes. */
import { Kind } from 'graphql/language';
import { GraphQLScalarType } from 'graphql';
const TimestampType = new GraphQLScalarType({
name: 'Timestamp',
serialize(date) {
return (date instanceof Date) ? date.getTime() : null
},
parseValue(date) {
try { return new Date(value); }
catch (error) { return null; }
},
parseLiteral(ast) {
if (ast.kind === Kind.INT) {
return new Date(parseInt(ast.value, 10));
}
else if (ast.kind === Kind.STRING) {
return this.parseValue(ast.value);
}
else {
return null;
}
},
});
但这次不是重新发明轮子(
#550
GraphQLTimestamp.js
解决方案(我的
TimestampType