Skip to Content
DocumentationAnalog WatchDevelopers' GuideQuerying Data

Querying Data

Learn how to query data with the Watch SDK.

Currently, the Watch Portal doesn’t permit direct querying of data from the GraphQL endpoint. You can only use it to explore published smart contracts and Views. To query data directly from a GraphQL endpoint, you can use the Watch SDK or the GraphQL IDE.

How to Query Data with the Watch SDK

Prerequisites

Before diving into the Watch SDK, ensure you have completed the following checklist components:

  • Install Watch Client. Check out Watch SDK for more details.
  • Generate a Session key. Check out Authentification to learn more.
  • Ensure you have the URL of the deployed View. Visit the appropriate environment URL to explore a list of published Views and their URLs. We'll use the price feed aggregator View we have explained here and funded for illustration.

Displaying a List of Published Artifacts

Depending on which Watch artifact list you would like to display, here's a quick way to do it:

import { TimegraphClient } from "@analog-labs/timegraph-js"; const timeGraphClient = new TimegraphClient({ url: "<URL>", // A url to Watch GraphQL instance. sessionKey: "<SSK>", // Session key created by user wallet using WASM SDK }); const functions = await timeGraphClient.fn.subgraph({ after:"<after the hashID>", before:"<before the hashiD>", hash:"<Current HashId>", limit:"<number of records>" });

Querying Data from Published Views

Here's an example code showcasing how to query data from a published View:

import { TimegraphClient } from "@analog-labs/timegraph-js"; const timeGraphClient = new TimegraphClient({ url: "<URL>", // A url to Watch GraphQL instance. sessionKey: "<SSK>", // Session key created by user wallet using WASM SDK }); // i.e If you're querying other publishers' View, then an alias request will be called in first. // Optional Alias request to query other publisher view. const aliasResponse = await timeGraphClient.alias.add({ name:"<name of the view>", identifier:"<identifier for subgraph>" }) // Request to query the view..... const response = await timeGraphClient.view.data( { hashId:"<hash id of the View>", fields:["<fields to return>"], limit:"<no of records required>" });

Response

{ response: { "view": { "USDC_WETH" : [ { "_cycle": "6", "_block": "17087521", "Price": "0.00051192" } } }

Querying User Info

In addition to publishing and querying Views, the Watch SDK lets you query user information, including your registered data, API keys, and subgraph details.

1. Registered data

Run this code to fetch your registered data from the Watch SDK:

import { TimegraphClient } from "@analog-labs/timegraph-js"; const timeGraphClient = new TimegraphClient({ url: "<URL>", // A url to Watch GraphQL instance. sessionKey: "<SSK>", // Session key created by user wallet using WASM SDK }); const response = await timeGraphClient.user.get(); );

Response

{ response: { "userInfo" : { "userId" : 100, "userRole" : "developer", "subgraph": 100 } }

2. API Keys

You can display the status of your API keys by running this code:

import { TimegraphClient } from "@analog-labs/timegraph-js"; const timeGraphClient = new TimegraphClient({ url: "<URL>", // A url to the Watch GraphQL instance. sessionKey: "<SSK>", // Session key created by user wallet using WASM SDK }); const response = timeGraphClient.apiKey.list(); );

Response

{ response: { "keys": [ { "userId":100, "role":"developer", "key":"Dovq2w5gfhxXFFmLTY9L6eynvPkSoyRAGEQUiXqrZhbQ", "status":"enabled" } }

3. Subgraph

Subgraph details let you extract functions and views associated with a given session key. Depending on the Watch artifact, running any of the specified codes displays the subgraph details:

import { TimegraphClient } from "@analog-labs/timegraph-js"; const timeGraphClient = new TimegraphClient({ url: "<URL>", // A url to Watch GraphQL instance. sessionKey: "<SSK>", // Session key created by user wallet using WASM SDK }); const response = await timeGraphClient.fn.subgraph({ after:"<after this hash fetch all>", before:"<before this hash fetch all>", hash:"<fetch one record with hash>" limit:"<limit of records >" });

Response

{ response: { functions:[{ "hashId" : "QmPhee19czBg1fRgNQZdYGQd1BvABL4v5bJGWY2GAMYE2Y", "name": "UniSwap:USDC_getAmountsOut", "network" : "ethereum", "inputs" : [ { "input" : "number_as_uint256", "array": null, "constant" : null }, { "input" : null, "constant" : null, "array" : [ { "input" : null , "constant" : { "value":"A0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48", "input":"hex_address" } }, { "input" : "hex_address" , "constant" : null } ] } ], "outputs" : [ { "output" : "skip", "array" : null }, { "output" : "uint256_as_number", "array" : null } ], "contract" : { "address" : "7a250d5630B4cF539739dF2C5dAcb4c659F2488D" } }] }

4. Global Artifacts

You can fetch functions and views that are created by other publishers as follows:

import { TimegraphClient } from "@analog-labs/timegraph-js"; const timeGraphClient = new TimegraphClient({ url: "<URL>", // A url to Watch GraphQL instance. sessionKey: "<SSK>", // Session key created by user wallet using WASM SDK }); const response = await timeGraphClient.fn.global({ after:"<after this hash fetch all>", before:"<before this hash fetch all>", hash:"<fetch one record with hash>" limit:"<limit of recoreds >" });

Response

{ response: { functions:[{ "hashId" : "QmPhee19czBg1fRgNQZdYGQd1BvABL4v5bJGWY2GAMYE2Y", .... all functions fields ] .... and all others }

How to Query Data with the GraphQL IDE

Prerequisites

Ensure you have the following components ready:

  1. Your Session Key. Check out Session Key Generator for more details.
  2. Your chosen GraphQL IDE. We'll use the GraphiQL IDE in this example. However, you can use other GraphQL IDEs listed here .
  3. Set up the environment for the GraphQL IDE. See Setup to learn more.

Querying Process

Querying data with the GraphQL IDE is a simple two-step process:

  1. Launch your browser and point the URL to the appropriate environment.
  2. To query any View, locate the middle panel of the IDE's Editor and type or copy this:
query{ simple(limit:5){ price _cycle _block } complex(limit:10){ uni, weth _block _cycle } }

The first statement (i.e., simple(limit:5){}) queries a list of five records from a View named "simple." Each fetched record has 3 fields: price, cycle, and block. On the other hand, the second statement (i.e., complex(limit:10){}) fetches 10 records from a View named "complex," where each fetched record has 4 fields: uni, weth, block, and _cycle.

Besides typical queries, you can also query other details, such as registered data, API keys, and global artifacts with the GraphQL IDE:

Run this code to fetch your registered data:

query{ userInfo { wallet userId userRole subgraph } }