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

Composite queries #1851

Merged
merged 3 commits into from
Jul 8, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion examples/composite_queries/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions examples/composite_queries/package.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"name": "composite_queries_end_to_end_test_functional_syntax",
"scripts": {
"pre_tests": "ts-node --transpile-only --ignore=false test/pretest.ts",
"tests": "npm run pre_tests && jest",
Expand Down
2 changes: 1 addition & 1 deletion examples/composite_queries/src/canister1/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
serialize,
text,
update
} from 'azle';
} from 'azle/experimental';

import Canister2 from '../canister2';

Expand Down
2 changes: 1 addition & 1 deletion examples/composite_queries/src/canister2/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
serialize,
text,
update
} from 'azle';
} from 'azle/experimental';

import Canister3 from '../canister3';

Expand Down
2 changes: 1 addition & 1 deletion examples/composite_queries/src/canister3/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Canister, query, text } from 'azle';
import { Canister, query, text } from 'azle/experimental';

export default Canister({
deepQuery: query([], text, () => {
Expand Down
15 changes: 15 additions & 0 deletions src/lib/stable/ic_apis/id.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { Principal } from '..';

/**
* Gets the id of this canister
* @returns the canister id
*/
export function id(): Principal {
if (globalThis._azleIc === undefined) {
return undefined as any;
}

// TODO consider bytes instead of string, just like with caller
const idString = globalThis._azleIc.id();
return Principal.fromText(idString);
}
2 changes: 2 additions & 0 deletions src/lib/stable/ic_apis/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
export { call } from './call';
export { id } from './id';
export { notify } from './notify';
export { reply } from './reply';
export { trap } from './trap';
18 changes: 18 additions & 0 deletions src/lib/stable/ic_apis/reply.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { IDL } from '..';

/**
* Used to manually reply to an ingress message. Intended to be used in
* canister methods with a {@link Manual} return type.
* @param reply the value with which to reply. Must be of type `T` where `T`
* is the generic type supplied to `Manual<T>`. Otherwise will result in an
* uncaught `TypeError`.
*/
export function reply<T>(data: T, type: IDL.Type<T>): void {
if (globalThis._azleIc === undefined) {
return undefined as any;
}

return globalThis._azleIc.replyRaw(
new Uint8Array(IDL.encode([type], [data])).buffer
);
}
11 changes: 7 additions & 4 deletions src/lib/stable/query.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import { IDL } from '@dfinity/candid';

import { isAsync } from '../canister_methods/is_async';
import { executeWithCandidSerde } from './execute_with_candid_serde';

export function query(
paramIdls: IDL.Type[],
returnIdl?: IDL.Type
returnIdl?: IDL.Type,
options?: {
composite?: boolean;
manual?: boolean;
}
): MethodDecorator {
return <T>(
target: object,
Expand All @@ -21,15 +24,15 @@ export function query(
originalMethod,
paramIdls,
returnIdl,
false // TODO implement manual check
options?.manual ?? false
);
};

descriptor.value = methodCallback as any;

globalThis._azleCanisterMethods.queries.push({
name: propertyKey as string,
composite: isAsync(originalMethod)
composite: options?.composite ?? false
// TODO implement guard
});

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.azle
.dfx
dfx_generated
node_modules
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
{
"canisters": {
"canister1": {
"type": "azle",
"main": "src/canister1/index.ts",
"candid": "src/canister1/index.did",
"candid_gen": "custom",
"env": [
"CANISTER1_PRINCIPAL",
"CANISTER2_PRINCIPAL",
"CANISTER3_PRINCIPAL",
"AZLE_TEST_FETCH"
],
"assets": [
["src/canister2/index.did", "candid/canister2.did"],
["src/canister3/index.did", "candid/canister3.did"]
],
"declarations": {
"output": "test/dfx_generated/canister1",
"node_compatibility": true
}
},
"canister2": {
"type": "azle",
"main": "src/canister2/index.ts",
"candid": "src/canister2/index.did",
"candid_gen": "custom",
"env": ["CANISTER3_PRINCIPAL", "AZLE_TEST_FETCH"],
"assets": [["src/canister3/index.did", "candid/canister3.did"]],
"declarations": {
"output": "test/dfx_generated/canister2",
"node_compatibility": true
}
},
"canister3": {
"type": "azle",
"main": "src/canister3/index.ts",
"candid": "src/canister3/index.did",
"candid_gen": "custom",
"declarations": {
"output": "test/dfx_generated/canister3",
"node_compatibility": true
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/** @type {import('ts-jest').JestConfigWithTsJest} */
module.exports = {
preset: 'ts-jest',
testEnvironment: 'node',
transform: {
'^.+\\.ts$': 'ts-jest',
'^.+\\.js$': 'ts-jest'
},
transformIgnorePatterns: ['/node_modules/(?!(azle)/)'] // Make sure azle is transformed
};
Loading
Loading