apollo-universal-starter-kit icon indicating copy to clipboard operation
apollo-universal-starter-kit copied to clipboard

Add support for Date

Open igo opened this issue 6 years ago • 3 comments

Date fields cannot be used out of box. sqlite (and eventualy other databases) stores date as integer and no conversion to Date is done out of box when performing SELECT. Could you please add support or update examples how to use date fields?

knex.schema.createTable('event', table => {
        table.increments();
        table.string('title');
        table.date('startDate');
        ....
      })
backend error GraphQL execution error: { TypeError: Date cannot represent a non string, or non Date type 1532908800000
    at serialize (/Users/igo/app/node_modules/graphql-iso-date/dist/date/index.js:47:13)
    at GraphQLScalarType.serialize (/Users/igo/app/node_modules/graphql/type/definition.js:348:12)
    at completeLeafValue (/Users/igo/app/node_modules/graphql/execution/execute.js:708:37)
    at completeValue (/Users/igo/app/node_modules/graphql/execution/execute.js:656:12)
    at completeValue (/Users/igo/app/node_modules/graphql/execution/execute.js:636:21)
    at completeValueWithLocatedError (/Users/igo/app/node_modules/graphql/execution/execute.js:585:21)
    at completeValueCatchingError (/Users/igo/app/node_modules/graphql/execution/execute.js:554:12)
    at resolveField (/Users/igo/app/node_modules/graphql/execution/execute.js:500:10)
    at /Users/igo/app/node_modules/graphql/execution/execute.js:367:18
    at Array.reduce (<anonymous>)
    at executeFields (/Users/igo/app/node_modules/graphql/execution/execute.js:364:42)
    at collectAndExecuteSubfields (/Users/igo/app/node_modules/graphql/execution/execute.js:781:10)
    at completeObjectValue (/Users/igo/app/node_modules/graphql/execution/execute.js:771:10)
    at completeValue (/Users/igo/app/node_modules/graphql/execution/execute.js:667:12)
    at completeValueWithLocatedError (/Users/igo/app/node_modules/graphql/execution/execute.js:585:21)
    at completeValueCatchingError (/Users/igo/app/node_modules/graphql/execution/execute.js:560:21)
  message: 'Date cannot represent a non string, or non Date type 1532908800000',
  locations: [ { line: 7, column: 9 } ],
  path: [ 'events', 'edges', 0, 'node', 'startDate' ] }

igo avatar Aug 11 '18 13:08 igo

@igo Date's should be supported out of the box. You might face this problem however: https://stackoverflow.com/questions/41916012/storing-a-node-js-date-in-a-knex-datetime-mysql-datetime-field/41941071 Check the answer on this question and if it doesn't help, please provide exact reproduction steps for your issue.

larixer avatar Aug 16 '18 12:08 larixer

@igo What I do in this case is use typeCast to covert date to what I need.

Here is an example: https://stackoverflow.com/questions/45103788/knexjs-returning-mysql-timestamp-datetime-columns-as-javascript-date-object

mitjade avatar Aug 21 '18 11:08 mitjade

@mitjade yep, that's my problem. I found that solution as well but this unfortunately works only for MySQL. since this is MySQL driver feature. We need solution that works for other DBs as well.

igo avatar Aug 22 '18 18:08 igo