neo4j-graphql-js
neo4j-graphql-js copied to clipboard
Implement @created and @updated directives for automatic server timestamps
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 Report
Merging #364 into master will increase coverage by
0.06%. The diff coverage is100%.
@@ 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 dataPowered by Codecov. Last update bbd705f...3925dbc. Read the comment docs.
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 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.
@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 :)
Rebased from master since I'm using this PR in my own development.
Any updates on this? I'd like to use this myself. What's the holdup?
https://github.com/neo4j-graphql/neo4j-graphql-js/issues/608