Shared composite query giving error in Motoko

Hi, I’m testing new shared composite queries…

Trying to deploy following actor locally and I get an error:

compositequerytestA.mo:4.29-4.34: syntax error [M0001], unexpected token ‘query’, expected one of token or sequence:

import Text "mo:base/Text";

actor class CQTA() = this {
    public shared composite query func queryCallTest() : async Text 
    {
       return "return some text";
    };
}

Any ideas? I’ve updated my dfx to 0.14.2.

Thanks for your help, Phasma.

I did manage to deploy without using keyword ‘composite’.

But, now I’m having issue using the composite query. My 2nd actor code is following:

import Text "mo:base/Text";

actor class CQTB() = this {

  let EXTERNAL = actor "bkyz2-fmaaa-aaaaa-qaaaq-cai" : actor { 
     queryCallTest: () ->  async (Text);
  };

    public shared query func queryCallTest() : async Text 
    {
       return async EXTERNAL.queryCallTest();
    };

}

Deploying gives following error:

compositequerytestB.mo:11.15-11.39: type error [M0037], misplaced async expression; try enclosing in an async function

Any ideas?

Hmm, Motoko composite query support has not yet shipped, even with dfx 0.14.3. You would need to side-load Motoko 0.9.4 or higher for that, or, if you are feeling brave, try the current beta of dfx (0.15.0-beta.0) that ships with a newer Motoko compiler.

FTR, the syntax will require the composite keyword (just before query), and ordinary queries cannot call queries or composite queries, only composite queries can. Here’s an example (assuming Bucket.get is also a (composite or vanilla) query:

  public composite query func get(k : Key) : async ?Value {
    switch (buckets[k % n]) {
      case null null;
      case (?bucket) await bucket.get(k);
    };
  };

Were those answers generated with the help of an LLM, I wonder?

3 Likes

Hey claudio, thanks for your answer!! And yes, these answers looked a bit LLM-ish to me too :slight_smile:

Trying this with 0.15.0-beta.3…

import Text "mo:base/Text";

actor class CQTA() = this {
    public composite query func queryCallTest() : async Text 
    {
       return "return some text";
    };
}

Calling composite query gives me following error:

Call was rejected:
Request ID: 006eab2f9fad047af6e534e15ab59c3b5eaf5088f6d227711ab0d5e0801192cc
Reject code: 5
Reject text: Composite query cannot be called in replicated mode

Any ideas? Thanks!!

How did you invoke the query from dfx?

dfx call CQTA queryCallTest will, by default, invoke the (composite) query in replicated mode.

You may need to use something like

dfx call --query CQTA queryCallTest

instead.

(https://github.com/dfinity/docs/blob/main/modules/developers-guide/pages/cli-reference/dfx-canister.adoc#dfx-canister-call)

PS. Query methods can be executed in replicated or on-replicated mode (–query). Composite queries can only be called in non-replicated mode. Perhaps dfx could give a better error here.

1 Like