Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

sending Date using moment to graphql

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?

like image 373
peter flanagan Avatar asked Mar 25 '19 11:03

peter flanagan


2 Answers

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

like image 100
WickyNilliams Avatar answered Oct 21 '22 22:10

WickyNilliams


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

new Date().toISOString()
like image 37
Martin Avatar answered Oct 21 '22 22:10

Martin