Querying
The Graph is a decentralized protocol for indexing and querying data from blockchains, such as Optimism, Ethereum, and IPFS, making it easier for developers to build decentralized applications. This guide focuses on querying a particular subgraph for HNS.ID.
How to Query
Using the GraphiQL Interface
- Navigate to The Graph's GraphiQL interface (opens in a new tab).
- In the left-hand query pane, write or paste your GraphQL query.
- Press the "Play" button to execute the query and observe the results in the right-hand pane.
Programmatically in Your Code
To query the subgraph programmatically, you can use GraphQL client libraries like Apollo or urql. Here's a sample JavaScript code snippet using Apollo Client:
import { ApolloClient, InMemoryCache, gql } from '@apollo/client';
const client = new ApolloClient({
uri: 'https://gateway.thegraph.com/api/{api-key}/subgraphs/id/6FAHdNZszs25J25NmjTc2de7nuQu2b4HRhGWsPWiKruh',
cache: new InMemoryCache()
});
client.query({
query: gql`
{
tlds(first: 5) {
id
owner
}
}
`
}).then(data => console.log(data))
.catch(error => console.error(error));
Example Queries
Here are some sample GraphQL queries you might run against the Handshake HNS-ID subgraph:
Fetch first 5 TLDs and their owners
{
tlds(first: 5) {
id
owner {
id
}
}
}
Get details of a specific TLD
{
tlds(where: {label: "hns"}) {
id
label
owner {
id
}
resolver {
id
addresses {
cointype
address
}
textRecords{
key
value
}
}
}
}
Fetch all SLDs belonging to a specific TLD along with their addresses
{
tlds(where: {label: "hns"}) {
id
label
slds {
id
owner {
id
}
fullName
}
}
}