Skip to content

Commit 809517d

Browse files
authored
feat: allow rewriters to modify parent result (#22)
* 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
1 parent 62e8211 commit 809517d

File tree

5 files changed

+27
-13
lines changed

5 files changed

+27
-13
lines changed

src/ast.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -275,11 +275,11 @@ export const rewriteResultsAtPath = (
275275
const newValue = callback(curResults, index);
276276
return newValue;
277277
});
278-
} else {
279-
newResults[curPathElm] = callback(results, curPathElm);
278+
279+
return newResults;
280280
}
281281

282-
return newResults;
282+
return callback(results, curPathElm);
283283
}
284284

285285
const remainingPath = path.slice(1);

src/rewriters/NestFieldOutputsRewriter.ts

+8-3
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ class NestFieldOutputsRewriter extends Rewriter {
6666
}
6767

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

7171
if (typeof pathResponse === 'object') {
7272
// undo the nesting in the response so it matches the original query
@@ -76,10 +76,15 @@ class NestFieldOutputsRewriter extends Rewriter {
7676
) {
7777
const rewrittenResponse = { ...pathResponse, ...pathResponse[this.newOutputName] };
7878
delete rewrittenResponse[this.newOutputName];
79-
return rewrittenResponse;
79+
80+
return {
81+
...response,
82+
[key]: rewrittenResponse
83+
};
8084
}
8185
}
82-
return pathResponse;
86+
87+
return response;
8388
}
8489
}
8590

src/rewriters/Rewriter.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ abstract class Rewriter {
5757
}
5858

5959
public rewriteResponse(response: any, key: string | number): any {
60-
return response[key];
60+
return response;
6161
}
6262
}
6363

src/rewriters/ScalarFieldToObjectFieldRewriter.ts

+7-4
Original file line numberDiff line numberDiff line change
@@ -49,14 +49,17 @@ class ScalarFieldToObjectFieldRewriter extends Rewriter {
4949
}
5050

5151
public rewriteResponse(response: any, key: string | number) {
52-
const pathResponse = super.rewriteResponse(response, key);
53-
5452
if (typeof response === 'object') {
53+
const pathResponse = response[key];
54+
5555
// undo the nesting in the response so it matches the original query
56-
return pathResponse[this.objectFieldName];
56+
return {
57+
...response,
58+
[key]: pathResponse[this.objectFieldName]
59+
};
5760
}
5861

59-
return pathResponse;
62+
return response;
6063
}
6164
}
6265

test/ast.test.ts

+8-2
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,10 @@ describe('ast utils', () => {
1717
}
1818
};
1919
expect(
20-
rewriteResultsAtPath(obj, ['thing1', 'moreThings', 'type'], (elm, path) => elm[path] + '!')
20+
rewriteResultsAtPath(obj, ['thing1', 'moreThings', 'type'], (elm, path) => ({
21+
...elm,
22+
[path]: elm[path] + '!'
23+
}))
2124
).toEqual({
2225
thing1: {
2326
moreThings: [{ type: 'dog!' }, { type: 'cat!' }, { type: 'lion!' }]
@@ -50,7 +53,10 @@ describe('ast utils', () => {
5053
]
5154
};
5255
expect(
53-
rewriteResultsAtPath(obj, ['things', 'moreThings', 'type'], (elm, path) => elm[path] + '!')
56+
rewriteResultsAtPath(obj, ['things', 'moreThings', 'type'], (elm, path) => ({
57+
...elm,
58+
[path]: elm[path] + '!'
59+
}))
5460
).toEqual({
5561
things: [
5662
{

0 commit comments

Comments
 (0)