Call query method of a child workflow from the query method of parent

I have a long-running workflow “Workflow1” from which I start a child workflow “Workflow2” (which is also a long-running workflow) in async.
I store the workflowId of the child workflow in the state of the parent workflow while starting the child workflow in async as shown below

ChildWorkflowStub childWorkflowStub =
        Workflow.newUntypedChildWorkflowStub(value.workflowName, kycWorkflowOptions);

childWorkflowStub.executeAsync(Void.class);
Promise<WorkflowExecution> childWorkflowExecution = childWorkflowStub.getExecution();

childWorkflowId = childWorkflowExecution.get().getWorkflowId()

I only want to expose the Parent workflow to the services.
To achieve this, we have exposed signal and query methods from the parent workflow, from which I need to call the child workflow’s signal/query method respectively.

I am able to call the signal method of child workflow from the signal method in parent workflow.

But not able to figure out a way to trigger the query method of a child workflow from the query method of the parent. Is this possible?

Hello @Aliasgar

But not able to figure out a way to trigger the query method of a child workflow from the query method of the parent. Is this possible?

It is not possible. One thing you can do is to signal the parent workflow from the child workflow when the childWorkflow state change.

I only want to expose the Parent workflow to the services.

You can dynamically add signal and query handlers to your workflows so they are not included in the workflow interface, for example:

Workflow.registerListener(
    (DynamicSignalHandler)
        (signalName, encodedArgs) -> {
      // ...
});

 Workflow.registerListener(
    (DynamicQueryHandler)
        (queryType, encodedArgs) -> {
          // ...
});

Note that if you have a concrete signal method handler define in your workflow interface it would be called for the associated signal, if not the dynamic one would.

But not able to figure out a way to trigger the query method of a child workflow from the query method of the parent. Is this possible?

Signal methods are executed inside workflow thread so in signal handler you could use ExternalWorkflowStub to signal the child if you wanted (which has the dynamic signal handler defined). This is not possible with query handlers however.

One thing you could consider is utilizing workflow id for parent/child so for example child workflow id could based on parent workflow id plus some known postfix so from the client perspective you would be able to signal/query child by just knowing the parent workflow id.

1 Like

Thanks @tihomir for the elaborate response

This is exactly how I have implemented it.

Is there any possibility of this being picked up for development at some point?
It would make the code in consuming services much more clean and elegant if there is a capability to Call Query Methods of external/child workflows from Query method of a Workflow. :slight_smile:

Do agree that having to use activity to query external workflow from another workflow is not the most user friendly approach. Opened issue here, hope it helps.

1 Like