#4437: Ensure method hooks always run with correct timing (before and after method) #4537
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Resolves #4437
Impact: minor
Type: bugfix
Issue
Due to current limitations with extending the Catalog schema, I created a
MethodHooks.after
callback to move a couple fields from Products to Catalog when a product is published. I registered the hook to thecatalog/publish/products
method. When it ran, I was seeing that my callback was firing before the completion of the method.Solution
I identified that this was happening in Meteor methods that call GraphQL mutation functions, without
await
ing for them to complete before returning a result. There were 2 methods that did this -catalog/publish/products
andaccounts/addressBookAdd
.Meteor methods allow you to return a promise and will await it, so we should make method-hooks work in that scenario.
Breaking changes
None
Testing
METEOR_DISABLE_OPTIMISTIC_CACHING=1 REACTION_LOG_LEVEL="DEBUG" reaction
As first line in MethodHooks.before callback, add logging:
console.log("before copyCartToOrder");
As first line in MethodHooks.after callback, add logging:
console.log("after copyCartToOrder");
After line 105 (methodResult = ...) add logging:
console.log("method is done");
before copyCartToOrder
cart/copyCartToOrder ...
Transitioned cart ... to order ...
sendOrderEmail status: new
method is done
after copyCartToOrder
Add the following code to the end of the file:
Before catalog/publish/products hook
method is done
After catalog/publish/products hook