neo4j-graphql-js icon indicating copy to clipboard operation
neo4j-graphql-js copied to clipboard

Implement @created and @updated directives for automatic server timestamps

Open colinskow opened this issue 5 years ago • 7 comments

I added @created and @updated GraphQL directives which when attached to a DateTime field will automatically populate with a server timestamp every time the object is created or modified. These work on nodes as well as relationship objects.

@created will add a timestamp when a node or relationship is created through CREATE or MERGE. @updated will update the timestamp on CREATE, MERGE, and UPDATE.

Example:

type SuperHero {
    id: ID!
    name: String!
    created: DateTime @created
    updated: DateTime @updated
  }

type Power {
  id: ID!
  title: String!
  endowment: [Endowment]
}

type Endowment @relation(name: "ENDOWED_TO") {
  from: Power!
  to: SuperHero!
  strength: Int!
  since: DateTime @created
  modified: DateTime @updated
}

Includes unit tests for Cypher generation, and I've also tested manually with Neo4J Desktop to ensure the Cypher works as expected.

colinskow avatar Dec 19 '19 01:12 colinskow

Codecov Report

Merging #364 into master will increase coverage by 0.06%. The diff coverage is 100%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #364      +/-   ##
==========================================
+ Coverage   96.31%   96.38%   +0.06%     
==========================================
  Files          24       24              
  Lines        2821     2875      +54     
==========================================
+ Hits         2717     2771      +54     
  Misses        104      104
Impacted Files Coverage Δ
src/utils.js 93.73% <100%> (+0.19%) :arrow_up:
src/augment/directives.js 93.9% <100%> (+0.31%) :arrow_up:
src/translate.js 98.39% <100%> (+0.07%) :arrow_up:

Continue to review full report at Codecov.

Legend - Click here to learn more Δ = absolute <relative> (impact), ø = not affected, ? = missing data Powered by Codecov. Last update bbd705f...3925dbc. Read the comment docs.

codecov-io avatar Dec 19 '19 01:12 codecov-io

Hey @colinskow - thanks for the PR!

This feature was requested in #230

One issue I see is how to handle this for @cypher directive mutation fields. Should it just be up to the user to update the created and updated fields in their @cypher query? Or is there some way we could wrap the resolver so that we can extend this functionality to mutations that use the @cypher directive?

johnymontana avatar Dec 20 '19 16:12 johnymontana

@johnymontana the Cypher code to update the timestamps really depends on the type of mutation, which we don't really know in the case of custom code. For a developer who is already going to the trouble to implement custom mutation logic, adding timestamps is trivial. It would be quite complex for us to try to do it and we'd risk breaking the query.

colinskow avatar Dec 20 '19 19:12 colinskow

@johnymontana the Cypher code to update the timestamps really depends on the type of mutation, which we don't really know in the case of custom code. For a developer who is already going to the trouble to implement custom mutation logic, adding timestamps is trivial. It would be quite complex for us to try to do it and we'd risk breaking the query.

Agreed. Still a nice to have feature :)

nether-cat avatar Feb 10 '20 21:02 nether-cat

Rebased from master since I'm using this PR in my own development.

colinskow avatar Jul 11 '20 03:07 colinskow

Any updates on this? I'd like to use this myself. What's the holdup?

hsavit1 avatar Dec 06 '20 01:12 hsavit1

https://github.com/neo4j-graphql/neo4j-graphql-js/issues/608

michaeldgraham avatar May 02 '21 04:05 michaeldgraham