首页 > 解决方案 > 将 Javascript 中的当前时间传递给 GraphQL AWSDateTime 列

问题描述

我有一个带有类型Channel列的 GraphQL 表dateOnAWSDateTime

在我的 React 应用程序中,我调用了一个突变来创建一个新的Channel. 以下是相关代码:

const createChannel = `
mutation createChannel {
  createChannel(input:{
    title:"${title}"
    dateOn:${Date.now()}
    slug: "${slug}"
    authorId: "${authorId}"
  }) {
    id title slug authorId
  }
}
`;

AWS 接受以下格式的日期时间字符串:

The AWSDateTime scalar type represents a valid extended ISO 8601 DateTime string. In other words, this scalar type accepts datetime strings of the form YYYY-MM-DDThh:mm:ss.sssZ

我将如何在 Javascript 中以这种格式传递当前时间?

标签: javascriptgraphqlaws-amplify

解决方案


你可以用直接的 javascript 来做到这一点new Date().toISOString()。然而,这应该由后端的解析器处理,而不是从前端传递值。

同样 moment.js 是一个很大的依赖项,除非您打算广泛使用它,否则我会使用类似date-fns.


推荐阅读