# useQuery in the @daml/react library, forces me to pass in a template

**URL:** <https://forum.canton.network/t/usequery-in-the-daml-react-library-forces-me-to-pass-in-a-template/296>\
**Category:** App Development\
**Tags:** nodejs, react\
**Created:** [April 29, 2020, 4:28pm UTC](https://forum.canton.network/t/usequery-in-the-daml-react-library-forces-me-to-pass-in-a-template/296 "2020-04-29T16:28:04Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![chris.norris](https://yyz2.discourse-cdn.com/flex034/user_avatar/forum.canton.network/chris.norris/32/688_2.png) [@chris.norris](https://forum.canton.network/u/chris.norris)\
**Post date:** [April 29, 2020, 4:28pm UTC](https://forum.canton.network/t/usequery-in-the-daml-react-library-forces-me-to-pass-in-a-template/296/1 "2020-04-29T16:28:04Z")

</div>

I need to retrieve all contracts visible to a party on the ledger using the recommended path (which means getting @daml/react from npm), but the only suitable function available there is `useQuery`, which forces me to pass in a contract template thereby restricting the result to contracts of that type.

Previously this could be accomplished as follows, where the ledger context was part of the source code:

```haskell
import { useLedgerState, getContracts } from '../../context/LedgerContext';

function Default() {

  const ledger = useLedgerState();
  const allContracts = getContracts(ledger);
...

```

How is this achieved now?

---

<div class="post-metadata">

**Author:** ![Leonid\_Shlyapnikov](https://yyz2.discourse-cdn.com/flex034/user_avatar/forum.canton.network/leonid_shlyapnikov/32/241_2.png) [@Leonid\_Shlyapnikov](https://forum.canton.network/u/Leonid_Shlyapnikov)\
**Post date:** [April 29, 2020, 7:04pm UTC](https://forum.canton.network/t/usequery-in-the-daml-react-library-forces-me-to-pass-in-a-template/296/2 "2020-04-29T19:04:43Z")

</div>

I do not know how to achieve this with @daml/react (cc @Martin_Huschenbett, @shaynefletcher).

However it seems like it is by design: `useQuery` takes a `Template` as an argument, allowing to query only contracts of the specified template. The `QueryResult` is strongly typed, it needs to know the exact `Template` type to be able to deserialize the contracts returned by the query.

In any case there is the JSON API endpoint that might be useful:

> URL: `/v1/query`  
> Method: `GET`  
> List all currently active contracts for all known templates.

[https://docs.daml.com/json-api/index.html#contract-search-all-templates](https://docs.daml.com/json-api/index.html#contract-search-all-templates)

---

<div class="post-metadata">

**Author:** ![Martin\_Huschenbett](https://yyz2.discourse-cdn.com/flex034/user_avatar/forum.canton.network/martin_huschenbett/32/372_2.png) [@Martin\_Huschenbett](https://forum.canton.network/u/Martin_Huschenbett)\
**Post date:** [April 30, 2020, 7:59am UTC](https://forum.canton.network/t/usequery-in-the-daml-react-library-forces-me-to-pass-in-a-template/296/3 "2020-04-30T07:59:25Z")

</div>

Hi @chris.norris,

@Leonid_Shlyapnikov is absolutely right. The @daml/react library is designed with strong static-typying in mind and for use cases where the structure of the templates on the ledger is known during development of the UI. Any forms of meta-programming are beyond the scope of this library.

As @Leonid_Shlyapnikov also pointed out, using the JSON API directly is currently your only way to get hold of all active contracts.

I’m not sure what library your code snippet is referring to. This is definitely not an old version of @daml/react.

---

<div class="post-metadata">

**Author:** ![chris.norris](https://yyz2.discourse-cdn.com/flex034/user_avatar/forum.canton.network/chris.norris/32/688_2.png) [@chris.norris](https://forum.canton.network/u/chris.norris)\
**Post date:** [April 30, 2020, 8:32am UTC](https://forum.canton.network/t/usequery-in-the-daml-react-library-forces-me-to-pass-in-a-template/296/4 "2020-04-30T08:32:37Z")

</div>

Thanks for the guidance! The snippet is taken from before the use of daml/react, where the context file was provided as part of the scaffolding.
