This repository has been archived by the owner on Jun 22, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
feat(schema): Use Neo4j DateTime type for created and modfied fields #154
Merged
+178
−93
Merged
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
021ea58
feat(schema): Use Neo4j DateTime type for created and modfied fields
alastair 3029fb1
feat(project): automatically set created and modified properties
ChristiaanScheermeijer c7fb21f
chore(project): update modified for merge action
ChristiaanScheermeijer File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import { TransformRootFields } from 'graphql-tools' | ||
import { buildPropertyValue, parseFieldName } from '../utils/schema' | ||
import { buildArgument, buildName } from 'neo4j-graphql-js/dist/augment/ast' | ||
import { Kind } from 'graphql/language/kinds' | ||
|
||
export const createdUpdatedFieldTransformer = new TransformRootFields((operation, fieldName, field) => { | ||
// Only needed for mutations | ||
if (operation !== 'Mutation') { | ||
return undefined | ||
} | ||
|
||
const { action } = parseFieldName(fieldName) | ||
|
||
if (action !== 'Create' && action !== 'Update' && action !== 'Merge') { | ||
return undefined | ||
} | ||
|
||
const next = field.resolve | ||
|
||
field.resolve = (object, params, context, info) => { | ||
info.fieldNodes = info.fieldNodes.map(fieldNode => { | ||
const createdIndex = fieldNode.arguments.findIndex(argument => argument.name.value === 'created') | ||
const modifiedIndex = fieldNode.arguments.findIndex(argument => argument.name.value === 'modified') | ||
|
||
// remove given created and modified arguments | ||
if (createdIndex !== -1) { | ||
fieldNode.arguments.splice(createdIndex, 1) | ||
} | ||
|
||
if (modifiedIndex !== -1) { | ||
fieldNode.arguments.splice(modifiedIndex, 1) | ||
} | ||
|
||
const date = new Date() | ||
|
||
const value = { | ||
kind: Kind.OBJECT, | ||
fields: [ | ||
buildPropertyValue('year', Kind.INT, date.getUTCFullYear()), | ||
buildPropertyValue('month', Kind.INT, date.getUTCMonth()), | ||
buildPropertyValue('day', Kind.INT, date.getUTCDate()), | ||
buildPropertyValue('hour', Kind.INT, date.getUTCHours()), | ||
buildPropertyValue('minute', Kind.INT, date.getUTCMinutes()), | ||
buildPropertyValue('second', Kind.INT, date.getUTCSeconds()), | ||
buildPropertyValue('millisecond', Kind.INT, date.getUTCMilliseconds()), | ||
buildPropertyValue('timezone', Kind.STRING, 'z') | ||
] | ||
} | ||
|
||
if (action === 'Create') { | ||
fieldNode.arguments.push(buildArgument({ name: buildName({ name: 'created' }), value })) | ||
} | ||
|
||
if (action === 'Create' || action === 'Update' || action === 'Update') { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why action == Update twice? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. sorry, just saw the other commit that changed it to Merge There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, that was wrong. Fixed it right after. |
||
fieldNode.arguments.push(buildArgument({ name: buildName({ name: 'modified' }), value })) | ||
} | ||
|
||
return fieldNode | ||
}) | ||
|
||
return next(object, params, context, info) | ||
} | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I just had a thought here: What if the type has no 'created' or 'modified' fields? Here you will try and add them anyway.
All of the existing models implement ThingInterface and so have these fields, but some of the types that I defined for supporting the Annotations schema don't include them. I can add them in, but I think it makes sense that this transformer also supports the case where they're not present
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, that's a very good point. I'd assumed every type would contain these properties...