# Cannot call @QueryMethod from child workflow

**URL:** <https://community.temporal.io/t/cannot-call-querymethod-from-child-workflow/1247>\
**Category:** Community Support\
**Created:** [December 30, 2020, 5:03am UTC](https://community.temporal.io/t/cannot-call-querymethod-from-child-workflow/1247 "2020-12-30T05:03:00Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Gordon\_Sun](https://sea2.discourse-cdn.com/flex016/user_avatar/community.temporal.io/gordon_sun/32/82_2.png) [@Gordon\_Sun](https://community.temporal.io/u/Gordon_Sun)\
**Post date:** [December 30, 2020, 5:03am UTC](https://community.temporal.io/t/cannot-call-querymethod-from-child-workflow/1247/1 "2020-12-30T05:03:00Z")

</div>

The parent workflow has a @QueryMethod and when start the child workflow, pass it the id

```auto
    public static class ParentWorkflowImpl implements ParentWorkflow {
        @Override
        public void start() {
            ChildWorkflow child1 = Workflow.newChildWorkflowStub(ChildWorkflow.class);
            Async.procedure(child1::start, Workflow.getInfo().getWorkflowId());
            Workflow.sleep(Duration.ofHours(1));
        }

        @Override
        public int query() {
            return 30;
        }
    }

```

When try to use the @QueryMethod from the child workflow like this

```auto
    public static class ChildWorkflowImpl implements ChildWorkflow {
        @Override
        public void start(String parentWorkflowId) {
            ParentWorkflow parentWorkflow = Workflow.newExternalWorkflowStub(ParentWorkflow.class, parentWorkflowId);
            System.out.println(parentWorkflow.query());
        }
    }

```

I got error: `Query is not supported from workflow to workflow. Use activity that perform the query instead.`  
So I tried this

```auto
    public static class QueryActivityImpl implements QueryActivity {

        @Override
        public int query(String parentWorkflowId) {
            ParentWorkflow parentWorkflow = Workflow.newExternalWorkflowStub(ParentWorkflow.class, parentWorkflowId);
            return parentWorkflow.query();
        }
    }

    public static class ChildWorkflowImpl implements ChildWorkflow {
        private final ActivityOptions options = ActivityOptions.newBuilder().setStartToCloseTimeout(Duration.ofSeconds(120)).build();
        private final QueryActivity act = Workflow.newActivityStub(QueryActivity.class, options);

        @Override
        public void start(String parentWorkflowId) {
            System.out.println(act.query(parentWorkflowId));
        }
    }

```

Then I got the error: `Called from non workflow or workflow callback thread`

So is it not allowed to query parent workflow from child? If it is allowed, how do I do that?  
Thank you very much!

---

<div class="post-metadata">

**Author:** ![maxim](https://sea2.discourse-cdn.com/flex016/user_avatar/community.temporal.io/maxim/32/8_2.png) [@maxim](https://community.temporal.io/u/maxim)\
**Post date:** [December 30, 2020, 5:09am UTC](https://community.temporal.io/t/cannot-call-querymethod-from-child-workflow/1247/2 "2020-12-30T05:09:09Z")

</div>

The query usually implies periodic polling and in many situations parent sending a signal to the child upon reaching a certain state is more efficient.

If you still want to run a query from the parent use an activity. Note that only workflow code is allowed to call static methods on the `Workflow` class. An activity code has to use a `WorkflowClient` to create the workflow stub.

**Nit** : There is no need to pass the `parentId` to the child. The child can access it through

```java
Workflow.getInfo().getParentWorkflowId()

```

---

<div class="post-metadata">

**Author:** ![Gordon\_Sun](https://sea2.discourse-cdn.com/flex016/user_avatar/community.temporal.io/gordon_sun/32/82_2.png) [@Gordon\_Sun](https://community.temporal.io/u/Gordon_Sun)\
**Post date:** [December 30, 2020, 5:11am UTC](https://community.temporal.io/t/cannot-call-querymethod-from-child-workflow/1247/3 "2020-12-30T05:11:39Z")

</div>

Thank you very much!  
I will change to parent signal child and avoid passing the id.
