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

How to implement an after doc saved callback or chain a promise after a resolver?

Open zxpectre opened this issue 6 years ago • 3 comments

Hi, I need to implement an after-doc-saved callback or to chain a promise after a mutation resolver in order to add later subscriptions. How can I implement that? Using .wrapResolve() maybe but how should I do it properly? Thanks.

zxpectre avatar Feb 01 '19 15:02 zxpectre

As refer you may read the following issues

  • https://github.com/graphql-compose/graphql-compose/issues/112#issuecomment-367616354
  • https://github.com/graphql-compose/graphql-compose/issues/72#issuecomment-317243160
const middleware = next => async rp => {
  // before mutation logic
  // somehow change `rp`

  const payload = await next(rp);

  // after mutation logic change `payload`
  // eg. payload.record will contain mongoose document
 
  return payload;
}

schemaComposer.Mutation.addFields({
    userCreate: UserTC
      .getResolver('createOne')
      .wrapResolve(middleware),
});

nodkz avatar Feb 04 '19 08:02 nodkz

Also you may read this https://graphql-compose.github.io/docs/en/basics-resolvers.html#via-resolverwrapresolve

If you will have questions or additions – feel free to continue the discussion with code examples.

nodkz avatar Feb 04 '19 08:02 nodkz

As refer you may read the following issues

const middleware = next => async rp => {
  // before mutation logic
  // somehow change `rp`

  const payload = await next(rp);

  // after mutation logic change `payload`
  // eg. payload.record will contain mongoose document
 
  return payload;
}

schemaComposer.Mutation.addFields({
    userCreate: UserTC
      .getResolver('createOne')
      .wrapResolve(middleware),
});

This makes a lot of sense. Thought on awaiting but didn't know if it was the proper way. Thanks @nodkz ! I'll try it this way.

zxpectre avatar Feb 04 '19 12:02 zxpectre