Pep*_*Pep 2 javascript node.js graphql graphql-js
我收到来自外部 API 的 LASTUPDATE: 1579443364 响应,
如何在架构中定义数据类型以便将其转换为日期格式?
像这样的格式: Mon Jan 19 2020 00:44:04
GraphQL 模式语言支持:
Int
Float
字符串
Id
布尔值
小智 7
您可以使用自定义标量将 Int 解析为日期如果您使用的是 apollo 服务器,您可以尝试这样的事情
import { GraphQLScalarType } from 'graphql';
import { Kind } from 'graphql/language';
const resolverMap = {
Date: new GraphQLScalarType({
name: 'Date',
description: 'Date custom scalar type',
parseValue(value) {
return new Date(value); // value from the client
},
serialize(value) {
return value.getTime(); // value sent to the client
},
parseLiteral(ast) {
if (ast.kind === Kind.INT) {
return new Date(ast.value) // ast value is always in string format
}
return null;
},
}),
};
Run Code Online (Sandbox Code Playgroud)
进而
scalar Date
type MyType {
created: Date
}
Run Code Online (Sandbox Code Playgroud)
您可以在这里仔细查看https://www.apollographql.com/docs/graphql-tools/scalars/ 这里有一些其他有用的标量https://github.com/Urigo/graphql-scalars
| 归档时间: |
|
| 查看次数: |
25925 次 |
| 最近记录: |