pej*_*ohn 5 javascript graphql aws-amplify
我有一个Channel带有dateOn类型列的GraphQL 表AWSDateTime
在我的 React 应用程序中,我调用了一个 mutation 来创建一个新的Channel. 这是相关的代码:
const createChannel = `
mutation createChannel {
createChannel(input:{
title:"${title}"
dateOn:${Date.now()}
slug: "${slug}"
authorId: "${authorId}"
}) {
id title slug authorId
}
}
`;
Run Code Online (Sandbox Code Playgroud)
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 中以这种格式传递当前时间?
小智 11
你可以用直接的 javascript 做到这一点new Date().toISOString()。然而,这应该真正由后端的解析器处理,而不是从前端传递值。
此外,moment.js 是一个很大的依赖项,除非您打算广泛使用它,否则我会使用类似date-fns.
最终变得简单moment.js
首先我将它安装到我的项目中
npm install moment --save
然后我将其包含在相关的.js文件中
import moment from "moment";
这就是我最终的突变的样子
const createChannel = `
mutation createChannel {
createChannel(input:{
title:"${title}"
dateOn: "${moment().format()}"
slug: "${slug}"
authorId: "${authorId}"
}) {
id title slug authorId
}
}
`;
Run Code Online (Sandbox Code Playgroud)