Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing current time in Javascript to GraphQL AWSDateTime column

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?

like image 511
pejmanjohn Avatar asked Aug 15 '19 23:08

pejmanjohn


2 Answers

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.

like image 53
KevinChappell Avatar answered Sep 26 '22 08:09

KevinChappell


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
  }
}
`;
like image 20
pejmanjohn Avatar answered Sep 22 '22 08:09

pejmanjohn