Skip to content
This repository has been archived by the owner on Sep 3, 2021. It is now read-only.

Implement @created and @updated directives for automatic server timestamps #364

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

colinskow
Copy link

@colinskow colinskow commented Dec 19, 2019

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.

@codecov-io
Copy link

codecov-io commented Dec 19, 2019

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%) ⬆️
src/augment/directives.js 93.9% <100%> (+0.31%) ⬆️
src/translate.js 98.39% <100%> (+0.07%) ⬆️

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.

@colinskow colinskow changed the title Implement @create and @update directives for automatic server timestamps Implement @created and @updated directives for automatic server timestamps Dec 19, 2019
@johnymontana
Copy link
Contributor

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?

@colinskow
Copy link
Author

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

@nether-cat
Copy link

@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 :)

@colinskow
Copy link
Author

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

@hsavit1
Copy link

hsavit1 commented Dec 6, 2020

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

@michaeldgraham
Copy link
Collaborator

#608

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants