Add resource middleware to non batch resources #57
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.
Oops! We forgot to apply custom middleware to non batch resources.
What's going on here?
dataloader-codegen works by having the user define a config file of "resources" [1] that they want to generate dataloaders for.
[1] A "resource" = a function that returns data - e.g. a Promise from a call to
fetch. In Yelpy terms, this is an API function exported by a node clientlibA config file might look like this:
dataloader-codegen/examples/swapi/swapi.dataloader-config.yaml
Lines 9 to 14 in 5aa5db6
You can see that we declared
getPlanetsto be a batch resource, by passingisBatchResource: true.dataloader-codegen has two seperate codegen methods - one for batch resources, and one for non batch resources:
dataloader-codegen/src/implementation.ts
Line 27 in 5aa5db6
dataloader-codegen/src/implementation.ts
Line 385 in 5aa5db6
What's "middleware"?
Functions that run before and after the call to the underlying resource - allowing the user to globally transform the arguments/response before passing it back to userland.
Our generated code should wrap the call to the underlying resource with middleware, rather than calling the underlying resource directly.
See: https://github.com/Yelp/dataloader-codegen/blob/master/API_DOCS.md#getloaders-arguments
What's the problem?
The codegen for batch resources and non-batch resources are separate methods (see above) and share no common code. When we added support for middleware to this library, we never added it to the
getNonBatchLoadercodegen 😬What does this PR do?
Fixes the above issue, by pulling the middleware wrapped call to the underlying resource out into a seperate codegen method, to be called by both
getNonBatchLoaderandgetBatchLoader.