Skip to content

Commit

Permalink
make composite queries recursive
Browse files Browse the repository at this point in the history
  • Loading branch information
bdemann committed Sep 30, 2023
1 parent 2ae79e1 commit 17f9ac8
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 21 deletions.
14 changes: 7 additions & 7 deletions examples/composite_queries/src/canister1/index.did
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
service: () -> {
simpleCompositeQuery: () -> (text) query;
manualQuery: () -> (text) query;
totallyManualQuery: () -> (text) query;
deepQuery: () -> (text) query;
updateQuery: () -> (text) query;
simpleQuery: () -> (text) query;
simpleUpdate: () -> (text);
incCounter: () -> (nat) query;
incCanister1: () -> (nat) query;
incCanister2: () -> (nat) query;
incCounter: () -> (nat) query;
manualQuery: () -> (text) query;
simpleCompositeQuery: () -> (text) query;
simpleQuery: () -> (text) query;
simpleUpdate: () -> (text);
totallyManualQuery: () -> (text) query;
updateQuery: () -> (text) query;
}
22 changes: 10 additions & 12 deletions examples/composite_queries/src/canister1/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,7 @@ import Canister2 from '../canister2';
let canister2: typeof Canister2;
let counter: nat = 0n;

const incCounter = query([], nat, async () => {
counter += 1n;

return counter;
});

export default Canister({
const CompQueryCanister = Canister({
init: init([], () => {
canister2 = Canister2(
Principal.fromText(
Expand Down Expand Up @@ -63,17 +57,19 @@ export default Canister({
return await ic.call(canister2.deepQuery);
}),
// Composite query that modifies the state. Should revert after the call is done
incCounter,
incCounter: query([], nat, async () => {
counter += 1n;

return counter;
}),
// Composite query calling queries on the same canister
incCanister1: query([], nat, async () => {
// TODO This is not an ideal solution but will work for now
const self = Canister({
incCounter
})(ic.id());
const self: any = CompQueryCanister(ic.id());

counter += 1n;

const canister1AResult = await ic.call(self.incCounter);
const canister1AResult: any = await ic.call(self.incCounter);
const canister1BResult = await ic.call(self.incCounter);

return counter + canister1AResult + canister1BResult;
Expand All @@ -88,3 +84,5 @@ export default Canister({
return counter + canister2AResult + canister2BResult;
})
});

export default CompQueryCanister;
4 changes: 2 additions & 2 deletions examples/composite_queries/src/canister2/index.did
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
service: () -> {
deepQuery: () -> (text) query;
incCounter: () -> (nat) query;
manualQuery: () -> (text) query;
simpleQuery: () -> (text) query;
updateQuery: () -> (text);
manualQuery: () -> (text) query;
deepQuery: () -> (text) query;
}

0 comments on commit 17f9ac8

Please sign in to comment.