sending Date using moment to graphql
Asked Answered
A

4

11

I am trying to send a mutation to my graphql server to update a date. My mutation schema looks like this:

mutation CreateReminderMutation(
    $birthday: Date
) {
    createReminder(
        birthday: $birthday
    ) {
        id
    }
}

Then in my react component I am sending the date like this using moment.

const Calendar = () => {
   // component and mutation implementation
   birthday: moment(birthday).toDate()
}

I am getting the following error message:

[GraphQL error]: Message: Variable "$birthday" got invalid value "2019-03-15T12:00:00.000Z"; Expected type Date; Value is not a valid Date: 2019-03-15T12:00:00.000Z, unparsable, Location: [object Object], Path: undefined

can anyone advise how to get the correct Date format with moment to send to graphql?

Antipyrine answered 25/3, 2019 at 11:36 Comment(3)
why has this been downvoted without any explanation? If it was an obvious easy solution I would have found itAntipyrine
well, toDate seems to be exactly what you should be doing, that's for sure, as toDate returns a copy of the native Date object - it's odd that the error also states that date string is unparsable - I mean, that is THE guaranteed parsable string format for a dateDegrease
thanks @JaromandaX at least I am not going completely crazy. can't find much in the graphql docs about thisAntipyrine
F
12

Assuming you're using the graphql-iso-date package, a Date scalar needs to be in the YYYY-MM-DD format. So your mutation would be like:

mutation {
  createReminder(birthday: "1990-01-01") {
    id
  }
}

See this codesandbox: https://m5782nj1w9.sse.codesandbox.io/?query=query%20%7B%0A%20%20daysAgo%28when%3A%20%222018-01-01%22%29%0A%7D

Fosdick answered 25/3, 2019 at 12:10 Comment(0)
I
3

No moment.js solution but this works for me using graphql prisma

new Date().toISOString()
Ingrate answered 1/4, 2020 at 15:22 Comment(0)
S
2

Note that the toISOString() method returns the "simplified extended ISO format" which is YYYY-MM-DDTHH:mm:ss.sssZ (MDN Reference)

To get the YYYY-MM-DD ISO 8601 string, you need

 new Date().toISOString().split("T")[0],
Seleucid answered 17/10, 2022 at 22:31 Comment(0)
P
0

I used new Date().toUTCString() for hasura deployed on heroku. This did not work for me new Date().toISOString()

Praemunire answered 24/6, 2021 at 17:20 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.