Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(rewriter): expose parent object and element path to rewriter #20

Merged

Conversation

gregbty
Copy link
Contributor

@gregbty gregbty commented Sep 4, 2020

This changes the implementation of rewriteResultsAtPath so that now the parent and path of the element is exposed to Rewriter.rewriteResponse instead of the element itself. This should allow #14 to modify the response.

This changes the implementation of `rewriteResultsAtPath` so that now the parent and path of the element is exposed to `Rewriter`.`rewriteResponse` instead of the element itself. This should support implement field name response rewrites.

BREAKING CHANGE: This changes the definition of `Rewriter`.`rewriteResponse`. The `path` references the location of the element while the `response` points to the parent of the element. Combining these will result in the original behavior.
@gregbty gregbty mentioned this pull request Sep 4, 2020
Copy link
Collaborator

@chanind chanind left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

minor nits with naming, but looks good otherwise! Thanks for submitting this!

@@ -72,8 +72,8 @@ export default class RewriteHandler {
let rewrittenResponse = response;
this.matches.reverse().forEach(({ rewriter, paths }) => {
paths.forEach(path => {
rewrittenResponse = rewriteResultsAtPath(rewrittenResponse, path, responseAtPath =>
rewriter.rewriteResponse(responseAtPath)
rewrittenResponse = rewriteResultsAtPath(rewrittenResponse, path, (parentResponse, path) =>
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: calling this path here is confusing since there's already a variable called path in this function. This should probably be added to the linter

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I renamed this to key, hopefully that suffices, based on your other feedback.

src/ast.ts Outdated
@@ -261,24 +261,38 @@ interface ResultObj {
export const rewriteResultsAtPath = (
results: ResultObj,
path: ReadonlyArray<string>,
callback: (resultsAtPath: any) => any
callback: (parentResult: any, path: string | number) => any
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: could this be called something other than path, since path is used to mean an array of strings in other places in this function? Maybe key or something similar?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated! (Naming is hard.)

return newResults;
newResults[curPathElm] = curResults.map(result =>
rewriteResultsAtPath(result, remainingPath, callback)
);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 good refactor, don't know why reduce was used here instead of just a simple map like you wrote.

@chanind chanind merged commit 62e8211 into graphql-query-rewriter:master Sep 8, 2020
@chanind
Copy link
Collaborator

chanind commented Sep 8, 2020

🎉 This PR is included in version 2.0.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

@gregbty gregbty deleted the advanced-rewrite-response branch September 8, 2020 17:07
@gregbty
Copy link
Contributor Author

gregbty commented Sep 8, 2020

Looks like I missed one thing. This doesn't change the behavior of the output of the rewrites. While we can now see the parent object and modify it, those changes are ignored because the caller only maps the result to the specified path/key. Fixing that is a much bigger change because it would fundamentally change how rewrites are done. For now though, I can at least use this to rename paths by looking via a parent rewrite.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants