sysgears / apollo-universal-starter-kit

Apollo Universal Starter Kit is a SEO-friendly, fully-configured, modular starter application that helps developers to streamline web, server, and mobile development with cutting-edge technologies and ultimate code reuse.
https://apollokit.org
MIT License
1.68k stars 323 forks source link

Add support for Date #809

Open igo opened 6 years ago

igo commented 6 years ago

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' ] }
larixer commented 6 years ago

@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.

mitjade commented 6 years ago

@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

igo commented 6 years ago

@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.