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
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(project): automatically set created and modified properties
Fixes #112
- Loading branch information
1 parent
021ea58
commit 3029fb1
Showing
3 changed files
with
86 additions
and
1 deletion.
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') { | ||
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