graphql-compose-mongoose icon indicating copy to clipboard operation
graphql-compose-mongoose copied to clipboard

Publish info about `beforeRecordMutate`

Open oklas opened this issue 7 years ago • 4 comments

Hi! First of all thanks for the library.

What about publishing info about beforeRecordMutate? Or at least to know that it will not be subject to change.

oklas avatar May 11 '18 17:05 oklas

Hi @oklas Sorry for the late response.

I don like beforeRecordMutate implementation but it's widely used in our app. And will not be removed in near future. So feel free to use it.

Even more, if I decide to remove it in future, firstly will be DEPRECATION warnings with solution or migration guide, and only after that with MAJOR release it will be removed.


beforeRecordMutate used for wrapping resolvers createOne, removeById, removeOne, updateById, updateOne. It has following form:

UserTC.wrapResolverResolve('updateById', next => async rp => {

  // extend rp with additional logic 
  rp.beforeRecordMutate = async (doc, resolveParams) => {
      doc.status = 'changed';
      doc.userTouchedAt = new Date();

      const canMakeUpdate = await getSomethingAsync( ...provide data from doc... );
      if (!canMakeUpdate) {
         // interrupt operation
         throw new Error('Not allowed!');
      }

      return doc;
    };

  return next(rp);
});

You may take a look on test case https://github.com/graphql-compose/graphql-compose-mongoose/blob/720f0ba76ebfd588839f4efb0651a6c4ab94f982/src/resolvers/tests/updateById-test.js#L157 and search other use cases https://github.com/graphql-compose/graphql-compose-mongoose/search?l=JavaScript&q=beforeRecordMutate&type=

nodkz avatar May 18 '18 07:05 nodkz

Hi @nodkz. Thank you for answer. I found this when I analyzed the code in order to solve needed tasks. I am care about to show others usefull info. I think thing is appropriate when it solves its tasks. The above example is clean. I support the addition of this into docs.

oklas avatar May 21 '18 12:05 oklas

It is also useful when access wrapper is created:

function adminAccess(resolvers) {
  Object.keys(resolvers).forEach((k) => {
    resolvers[k] = resolvers[k].wrapResolve(next => async rp => {
      rp.beforeRecordMutate = async function(doc, rp) {

        const canAdminAccess = await getAdminAsync( ... )
        if (!canAdminAccess) {
         throw new Error('Forbidden!');
        }

        return doc;
      }

      return next(rp)
    })
  })
  return resolvers
}

schemaComposer.Mutation.addFields({
  createResource: ResourceTC.getResolver('createOne'),
  updateResource: ResourceTC.getResolver('updateById'),
  ...adminAccess({
    removeResource: ResourceTC.getResolver('removeById'),
  }),
});

oklas avatar May 21 '18 12:05 oklas

Yeah, will be cool if you add this to the docs: here https://github.com/graphql-compose/graphql-compose-mongoose/blob/master/README.md and after merging add it here https://github.com/graphql-compose/graphql-compose.github.io/blob/source/docs/plugin-mongoose.md

nodkz avatar May 21 '18 13:05 nodkz