I have a GraphQL table Channel
with the column dateOn
of the type AWSDateTime
In my React app I am calling a mutation to create a new Channel
. Here's the relevant code:
const createChannel = `
mutation createChannel {
createChannel(input:{
title:"${title}"
dateOn:${Date.now()}
slug: "${slug}"
authorId: "${authorId}"
}) {
id title slug authorId
}
}
`;
AWS accepts a datetime string in this format:
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
How would I go about passing the current time in this format in Javascript?
You can do this with straight javascript new Date().toISOString()
. However this should really be handled by the resolver on the backend instead of passing the value from the frontend.
Also moment.js is a large dependency, unless you plan to use it extensively I would go with something like date-fns
.
Ended up being simple with moment.js
First I installed it in my project
npm install moment --save
Then I included it in the relevant .js file
import moment from "moment";
And here's what my final mutation looked like
const createChannel = `
mutation createChannel {
createChannel(input:{
title:"${title}"
dateOn: "${moment().format()}"
slug: "${slug}"
authorId: "${authorId}"
}) {
id title slug authorId
}
}
`;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With