VikR
VikR

Reputation: 5142

Apollo/GraphQL: Field Type to Use for Timestamp?

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?

Upvotes: 5

Views: 22930

Answers (2)

VikR
VikR

Reputation: 5142

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
});

Upvotes: 2

stackdave
stackdave

Reputation: 7094

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('-'); 

};

Upvotes: 3

Related Questions