最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Passing current time in Javascript to GraphQL AWSDateTime column - Stack Overflow

programmeradmin4浏览0评论

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?

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?

Share Improve this question asked Aug 15, 2019 at 23:16 pejmanjohnpejmanjohn 1,0573 gold badges12 silver badges26 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 19

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
  }
}
`;
发布评论

评论列表(0)

  1. 暂无评论