# Read from the Subgraph

## Read Only: The subgraph

Most of the data from the blockchain, and additional higher level fields are readable from the [subgraph](https://thegraph.com/) ([quick video](https://www.youtube.com/watch?v=l2rzT_Dp4T0)).

Kovan: <https://thegraph.com/explorer/subgraph/charged-particles/kovan-universe?query=Protons%20NFTs>

&#x20;We recommend digging into the **Schema** yourselves to figure out what is available to query.

![](https://674717437-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MJ0DOfJceiIU5ANgHDU%2F-MW7BbqGOe3nDM_hDeBW%2F-MW7ESsPZVc5TUxAmJxz%2FScreen%20Shot%202021-03-18%20at%2010.07.50%20PM.png?alt=media\&token=f0650201-4bb8-4918-b207-0e3fea8efcd1)

Please also pay attention to some example queries we provide on the top left

![](https://674717437-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MJ0DOfJceiIU5ANgHDU%2F-MW7BbqGOe3nDM_hDeBW%2F-MW7IYjISqAa5aZQxeU-%2FScreen%20Shot%202021-03-18%20at%2010.26.18%20PM.png?alt=media\&token=472794e3-f610-4d9b-8acd-a4074426fd9d)

{% hint style="info" %}
Almost all the information available from the contracts is available somewhere on the subgraph, besides the **current accrued interest** (ie: *how much my DAI at 12% APY in my ProtonNFT made me*) which is currently available only by [contract querying](https://docs.charged.fi/charged-particles-protocol/smart-contracts-documentation/interfaces/charged-particles-interface)`currentParticleCharge(address,uint256,string,address)(uint256)`
{% endhint %}

### Sample Interaction using Node.js

The sample is in Typescript using `ts-node` but you can use CommonJs require as well.

```yaml
npm install -D ts-node graphql-request graphql
```

`graphql-request` is the most minimal and simplest to use GraphQL client. It's perfect for small scripts or simple apps. But **it doesn't allow you to listen for changes** for this, please use something like [apollo-client](https://www.npmjs.com/package/apollo-client).

You might also need a `tsconfig.json` at the root of your project with&#x20;

```typescript
{ "compilerOptions": { "moduleResolution": "node" } }
```

until <https://github.com/prisma-labs/graphql-request/issues/214> is resolved.

{% code title="getFirst20Protons.ts" %}

```typescript
import { request, gql } from 'graphql-request';

const query = gql`
{
   universes {
     protonToken {
       tokens(first:20) {
         name,
         tokenId,
         id
         creator,
         owner,
         salePrice,
         lastSalePrice,
       }
     }
   }
 }`
 
 const doGraphRequest = (endpoint, query, variables={}) => new Promise((resolve, reject) => {
  request(endpoint, query, variables)
    .then((data) => resolve(data))
    .catch(err => reject(err));
});

async function run() {
  const endpoint = 'https://api.thegraph.com/subgraphs/name/charged-particles/kovan-universe';
  const res: any = await doGraphRequest(endpoint, query);
  const { protonToken } = res.universes[0];
  console.log(protonToken);
}

run();
```

{% endcode %}

```typescript
# Run it
./node_modules/.bin/ts-node getFirst20Protons.ts

/* Outputs:


{
  tokens: [
    {
      creator: '0xb14d1a16f30db670097da86d4008640c6ccc2b76',
      id: '0xd4f7389297d9cea850777ea6ccbd7db5817a12b2-1',
      lastSalePrice: null,
      name: 'ETH on Mars',
      owner: '0xb14d1a16f30db670097da86d4008640c6ccc2b76',
      salePrice: '69000000000000000000',
      tokenId: '1'
    },
    {
      creator: '0x11113c2fba9ae08d7d16c817f03de51f29117db2',
      id: '0xd4f7389297d9cea850777ea6ccbd7db5817a12b2-10',
      lastSalePrice: null,
      name: 'ha',
      owner: '0x11113c2fba9ae08d7d16c817f03de51f29117db2',
      salePrice: '0',
      tokenId: '10'
    },
    ...
*/
```

{% hint style="info" %}
If you want access to [**VSCode debugger**](https://docs.charged.fi/charged-particles-protocol/developing-on-the-protocol/vscode-node.js-typescript-debugging#debugging-ts-node) and be able to set breakpoints check Debugging so you can use `debugger;` statement.
{% endhint %}
