概述
would it be possible to do dates in ISOString format? (011-10-05T14:48:00.000Z)
前端老哥要ISO的,那我就给他ISO的
post.graphql
type Post {
# ...
createdAt: String @date
updatedAt: String @date
}
// ====================================================
//
// This file is part of the Martin.
//
// Create by Martin
// Copyright (c) 2019 Martin
//
// ====================================================
import { SchemaDirectiveVisitor } from 'apollo-server-express';
import { GraphQLField, defaultFieldResolver, GraphQLString } from 'graphql';
import moment = require('moment');
export class DateFormatDirective extends SchemaDirectiveVisitor {
public visitFieldDefinition(field: GraphQLField<any, any>) {
const { resolve = defaultFieldResolver } = field;
field.resolve = async (...args) => {
const date = await resolve.apply(this, args);
return moment(date).toISOString();
};
// The formatted Date becomes a String, so the field type must change:
field.type = GraphQLString;
}
}
schemaDirectives 里面引用一下
看下文档:https://github.com/apollographql/graphql-tools/blob/master/docs/source/schema-directives.md
最后
以上就是等待项链为你收集整理的GraphQL query时返回指定格式的DateTime的全部内容,希望文章能够帮你解决GraphQL query时返回指定格式的DateTime所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复