GraphQL增量学习03-GraphQL-scalar-customtypetarget编写自定义类型代码graphQL-example/features.jsStep1.参考graphqlgraphql/languageconst{GraphQLScalarType}=require('graphql')const{Kind}=require('graphql/language')GraphQLScalarType用于声明ScalarKind类型检查2.写typeDefsconsttypeDefs=`###自定义日期类型###scalarDatetypeNotice{content:String!###消息时间###通知时间:日期!}`###...###是注释标量Date定义自定义类型Notice.noticeTime字段使用自定义Date类型3.写resolversconstresolvers={Date:newGraphQLScalarType({name:'Date',description:'日期自定义标量类型',parseValue(value){returnnewDate(value)//来自客户端的值},serialize(value){//returnnewDate(value).getTime()returnnewDate(value)//发送给客户端的值},parseLiteral(ast){if(ast.kind===Kind.INT){returnparseInt(ast.value,10)//ast值总是字符串格式}returnnull}})}解析器需要详细说明详细语句parseValue(value){...clientinputserialize(value){...printtoclientparseLiteral(value){...checktype4.mergeSchemaconstschema=makeExecutableSchema({typeDefs,resolvers})referencegraphql-工具
