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

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

Open
memolipd opened this issue Apr 18, 2019 · 3 comments
Open

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

memolipd opened this issue Apr 18, 2019 · 3 comments

Comments

@memolipd
Copy link

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

@rphansen91
Copy link

rphansen91 commented Jul 26, 2019

@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
Copy link

hsavit1 commented Dec 7, 2020

@johnymontana any updates on this?

@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

No branches or pull requests

4 participants