neo4j-graphql / neo4j-graphql-js

NOTE: This project is no longer actively maintained. Please consider using the official Neo4j GraphQL Library (linked in README).
Other
608 stars 148 forks source link

Auto-populate/update a "created" and "updated" fields on objects #230

Open memolipd opened 5 years ago

memolipd commented 5 years ago

adding a entity level directive eg @tracked to add and correctly update a created and updated timestamp fields.

rphansen91 commented 5 years ago

@memolipd In the meantime until directives are supported, I found a workaround using a custom middleware

const appendCreatedAt = (r,  p,  a,  c,  i,) => {
  const formatted = new Date().toISOString();
  a.createdAt = { formatted };
  return r(p, a, c, i);
};

const appendUpdatedAt = (r,  p,  a,  c,  i,) => {
  const formatted = new Date().toISOString();
  a.updatedAt = { formatted };
  return r(p, a, c, i);
};

export const autoCreatedAt = {
  Query: {},
  Mutation: {
   // Mapping of resolvers to append argument to
    CreatePerson: appendCreatedAt,
  },
};

export const autoUpdatedAt = {
  Query: {},
  Mutation: {
   // Mapping of resolvers to append argument to
    UpdatePerson: appendUpdatedAt,
  },
};

And applying middlewares to schema

import { applyMiddleware } from 'graphql-middleware';
import { autoCreatedAt, autoUpdatedAt } from './middlewares/date';
const schema = makeAugmentedSchema({
  typeDefs,
  resolvers
});
applyMiddleware(
   schema,
   autoCreatedAt,
   autoUpdatedAt
)

Unfortunately this is far removed from type definitions and all of the auto generated mutations have to be mapped to manually so hopefully @defaultValue support comes soon. #120

hsavit1 commented 3 years ago

@johnymontana any updates on this?

michaeldgraham commented 3 years ago

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