5

I'm storing a value to a postgres field that is of type timestamp with time zone. I was defining the field as an int in my Apollo schema, but I'm getting this error message in the resolver:

column "apptDateTime" is of type timestamp with time zone but expression is of type integer

Looking up GraphQL data types, I don't yet see any type that is cited as corresponding to a field of type timestamp.

What's the correct field type to use in the Apollo schema for a field that is of type timestamp in the database?

Brian Burns
  • 20,575
  • 8
  • 83
  • 77
VikR
  • 4,818
  • 8
  • 51
  • 96
  • Checkout the section on the date scalar: http://graphql.org/learn/schema/#scalar-types Probably best to define a custom scalar. – Tally Barak Dec 11 '16 at 20:27
  • I’m reading up on dates and Apollo, and I note that as Tally posts, it is required to add custom code for this purpose. I found documentation here: http://dev.apollodata.com/tools/graphql-tools/scalars.html#Date-as-a-scalar The docs define the resolver map, but don’t appear to show how to include the resolver map in your schema and/or other resolvers. Can someone provide or link to an example of how to reference a resolver map from your other Apollo resolvers? – VikR Dec 11 '16 at 20:46
  • this is how it was done for json: https://github.com/taion/graphql-type-json/blob/master/src/index.js – Tally Barak Dec 11 '16 at 20:48

2 Answers2

2

I got my mutation working that includes a field of type Timestamp with Time Zone. I did it by changing the schema for my apptDateTime field from Int to String, and passing in an ISO string. Sequelize and Postgres took care of changing it into a field of type Timestamp with Time Zone.

Update 2021:

Here's what I'm using these days.

Sequelize:

  timeOfNonce: {type: Sequelize.DATE} 

Schema:

 scalar DATETIME
 .....
 timeOfNonce: DATETIME

These days I let Sequelize define my SQL tables via:

const deleteAllData_fromThisModel = false;
const alterThisTableToMatchDBConnectorsModel = true;

myDataModel.sync({force: deleteAllData_fromThisModel, 
                  alter: alterThisTableToMatchDBConnectorsModel}).then(err => {
    console.log('myDataModel has been synced')
}).catch(err => {
    throw err
});
VikR
  • 4,818
  • 8
  • 51
  • 96
  • 2
    can you show something of your code? did you not need : require('pg').types.setTypeParser(1114, function(stringValue) { return new Date(stringValue + "+0000"); // e.g., UTC offset. Use any offset that you would like. }); – stackdave Nov 02 '17 at 07:13
2

I find this way to work with input in forms, needed convert from client (input form) to the server, and from the server to client (input form)

Graphql:

    updatedAt: String

Sequelize:

    updatedAt: {  type: Sequelize.DATE },

Postgresql:

    "createdAt" timestamp(6) with time zone DEFAULT now(),

Value transform to the Server:

  value  = dateToISO(value);

Value transform to the Client:

  if ( isNil(value) ) {
    value = '';
  } else {
    value  =  value.toLocaleDateString() +' '+ value.toLocaleTimeString();
  }

the helpers:

let dateToISO = function (dateString) {
  if (!dateString) {
    return null;
  }
  let p = dateString.split(/\D/g);
  /* It's up your date on input in this case come from DD-MM-YYYY
    for MM-DD-YYY use: return [p[1], p[2], p[0]].join('-'); */
  return [p[2], p[1], p[0]].join('-'); 

};
stackdave
  • 6,655
  • 9
  • 37
  • 56