Skip to content

Commit

Permalink
flatten val:null in decorate results
Browse files Browse the repository at this point in the history
  • Loading branch information
aravindet committed Apr 16, 2024
1 parent 9bbdfbd commit c7b01db
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
12 changes: 11 additions & 1 deletion src/common/coding/decorate.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ export default function decorate(rootGraph, rootQuery) {
: { ...plumGraph };
graph.$val = true;
} else if (Array.isArray(plumGraph)) {
graph = decodeGraph(plumGraph);
graph = deValNull(decodeGraph(plumGraph));
} else {
throw Error('decorate.unexpected_graph');
}
Expand Down Expand Up @@ -180,6 +180,16 @@ export default function decorate(rootGraph, rootQuery) {
return result;
}

// Replace $val: null produced by
function deValNull(graph) {
if (typeof graph !== 'object' || !graph) return graph;
if ('$val' in graph && graph.$val !== true) return graph.$val;

// Important: update graph in-place to avoid losing non-enumerable props.
for (const prop in graph) graph[prop] = deValNull(graph[prop]);
return graph;
}

function addPageMeta(graph, args) {
if (args.$all) {
Object.assign(graph, { $page: args, $prev: null, $next: null });
Expand Down
2 changes: 1 addition & 1 deletion src/common/coding/test/decorate.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ describe('val_null', () => {
],
{ foo: 1 },
);
expect(result).toEqual({ foo: { bar: { $val: null } } });
expect(result).toEqual({ foo: { bar: null } });
});

test('explicit', () => {
Expand Down

0 comments on commit c7b01db

Please sign in to comment.