Skip to content

Commit

Permalink
feat: allow rewriters to modify parent result (#22)
Browse files Browse the repository at this point in the history
* feat: allow rewriters to modify parent result

BREAKING CHANGE: Changes the behavior of `Rewriter`.`rewriteResponse` so that implementations modify the parent result instead of only returning the result of the key path.

* refactor: remove direct mutation of rewritten responses

* fix: use previous logic for rewriting array children
  • Loading branch information
gregbty authored Oct 29, 2020
1 parent 62e8211 commit 809517d
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 13 deletions.
6 changes: 3 additions & 3 deletions src/ast.ts
Original file line number Diff line number Diff line change
Expand Up @@ -275,11 +275,11 @@ export const rewriteResultsAtPath = (
const newValue = callback(curResults, index);
return newValue;
});
} else {
newResults[curPathElm] = callback(results, curPathElm);

return newResults;
}

return newResults;
return callback(results, curPathElm);
}

const remainingPath = path.slice(1);
Expand Down
11 changes: 8 additions & 3 deletions src/rewriters/NestFieldOutputsRewriter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ class NestFieldOutputsRewriter extends Rewriter {
}

public rewriteResponse(response: any, key: string | number) {
const pathResponse = super.rewriteResponse(response, key);
const pathResponse = response[key];

if (typeof pathResponse === 'object') {
// undo the nesting in the response so it matches the original query
Expand All @@ -76,10 +76,15 @@ class NestFieldOutputsRewriter extends Rewriter {
) {
const rewrittenResponse = { ...pathResponse, ...pathResponse[this.newOutputName] };
delete rewrittenResponse[this.newOutputName];
return rewrittenResponse;

return {
...response,
[key]: rewrittenResponse
};
}
}
return pathResponse;

return response;
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/rewriters/Rewriter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ abstract class Rewriter {
}

public rewriteResponse(response: any, key: string | number): any {
return response[key];
return response;
}
}

Expand Down
11 changes: 7 additions & 4 deletions src/rewriters/ScalarFieldToObjectFieldRewriter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,17 @@ class ScalarFieldToObjectFieldRewriter extends Rewriter {
}

public rewriteResponse(response: any, key: string | number) {
const pathResponse = super.rewriteResponse(response, key);

if (typeof response === 'object') {
const pathResponse = response[key];

// undo the nesting in the response so it matches the original query
return pathResponse[this.objectFieldName];
return {
...response,
[key]: pathResponse[this.objectFieldName]
};
}

return pathResponse;
return response;
}
}

Expand Down
10 changes: 8 additions & 2 deletions test/ast.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@ describe('ast utils', () => {
}
};
expect(
rewriteResultsAtPath(obj, ['thing1', 'moreThings', 'type'], (elm, path) => elm[path] + '!')
rewriteResultsAtPath(obj, ['thing1', 'moreThings', 'type'], (elm, path) => ({
...elm,
[path]: elm[path] + '!'
}))
).toEqual({
thing1: {
moreThings: [{ type: 'dog!' }, { type: 'cat!' }, { type: 'lion!' }]
Expand Down Expand Up @@ -50,7 +53,10 @@ describe('ast utils', () => {
]
};
expect(
rewriteResultsAtPath(obj, ['things', 'moreThings', 'type'], (elm, path) => elm[path] + '!')
rewriteResultsAtPath(obj, ['things', 'moreThings', 'type'], (elm, path) => ({
...elm,
[path]: elm[path] + '!'
}))
).toEqual({
things: [
{
Expand Down

0 comments on commit 809517d

Please sign in to comment.