Hgraph is a Web3 data infrastructure company focused on tooling for the Hedera network and beyond. You can sign up for an account and access our account dashboard console. This SDK intends to make developing with our API easy, flexible, and fast.
If you’re new to GraphQL, you can learn more GraphQL. Then, build queries using and interactive editor.
This SDK is in active development in conjunction with the hgraph API.
We are working towards following Semantic Versioning versioning.
Before a v1 major release there may be breaking changes on new releases. For
use in production, we recommend pinning an exact release - i.e --save-exact
.
npm i --save-exact @hgraph.io/sdk@latest
.
The default export of the SDK is a class that accepts configuration parameters.
To authenticate using an API key, pass your API key to the x-api-key
header on
each request.
For front-end solutions, we plan to authenticate using json web tokens. See https://jwt.io/. Currently, this functionality is limited, please reach out to use to utilize this functionality.
The following quick start template repos for different environments are available:
The following instructions are for meant for contributing to this repo (or hacking away). To run this code base locally use the following steps to get up and running.
gh repo clone hgraph-io/sdk
npm i
npm run watch
: watch for file changes and build on change- depend on local version of sdk by using local dependency: `npm i ../sdk'
- test ws connection:
wscat -s 'graphql-ws' -H 'x-api-key: <...>' -c wss://api.hgraph.dev/v1/graphql
SDK includes built-in GraphQL subscription management capabilities, simplifying the process of handling and interacting with subscriptions. This feature encapsulates all active subscriptions and their lifecycle control within the Client
.
- The
subscribe
andpatchedSubscribe
methods return an immutableObservableSubscription
object. ObservableSubscription
serves as both a subscription identifier and contains anunsubscribe
method.Client
methods for subscription management:removeSubscription: (subscription: ObservableSubscription) => void removeAllSubscriptions: () => void getSubscribtions: () => ObservableSubscription[]
import Client, { Network, Environment, ObservableSubscription } from '@hgraph.io/sdk';
const hg = new Client({
network: Network.HederaMainnet,
environment: Environment.Production,
});
const transactionSubscription: ObservableSubscription = hg.subscribe({
query: `
subscription LastTransaction {
transaction(limit: 1, order_by: {consensus_timestamp: desc}) {
id
consensus_timestamp
}
}`
}, {
next: (data) => {
console.log(data);
},
error: (errors) => {
console.error('LastTransaction subscription closed with errors:', errors);
},
complete: () => {
console.log("LastTransaction subscription complete");
}
});
Using the subscription object:
transactionSubscription.unsubscribe();
Or using the client method:
hg.removeSubscription(transactionSubscription);
Attempting to unsubscribe from an already completed subscription will raise an exception to prevent unwanted side effects.
hg.removeAllSubscriptions();
When unsubscribing (by any method), the
complete
subscription handler is called.
const subscriptions: ObservableSubscription[] = hg.getSubscriptions();
The patchedSubscribe
method extends the functionality of the existing subscribe
method by providing detailed patches indicating changes in data. These patches follow the RFC6902 standard, with operations such as "add", "remove", and "replace". This allows developers to precisely understand the modifications in the updated data, making it especially useful for scenarios like tracking ownership transfers of NFTs.
interface PatchOperation {
op: 'add' | 'remove' | 'replace';
path: string;
value: any;
}
op
: Indicates the type of operation performed.path
: A JSON Pointer string that specifies the location in the data where the operation is performed. JSON Pointers are defined in RFC6901.value
: The value associated with the operation. Foradd
andreplace
operations, this is the new value being added or replacing the old value. Forremove
operations, this is the value being removed.
When a change occurs, the method generates patches that describe the exact differences between the previous and current states. Here is a basic example demonstrating the usage of patchedSubscribe
:
import Client, { Network, Environment } from '@hgraph.io/sdk';
const hg = new Client({
network: Network.HederaTestnet,
environment: Environment.Production,
});
const observableSpender = "12345678";
hg.patchedSubscribe({
query: `
subscription GetNFTs($spender: bigint!) {
nft(where: { spender: { _eq: $spender } }) {
token_id
serial_number
metadata
}
}`,
variables: {
spender: observableSpender
}
}, {
next: (data, patches) => {
patches.forEach(patch => {
switch (patch.op) {
case 'add':
console.log("New NFT grant rights to spender:", patch.value);
break;
case 'remove':
console.log("NFT owner revoked rights:", patch.value);
break;
case 'replace':
console.log(`NFT data replaced at path: ${patch.path}, replaced value:`, patch.value);
break;
}
});
},
error: (err) => {
console.error(err);
},
complete: () => {
console.log("Subscription complete");
}
});
In this example, the
patchedSubscribe
method is used to subscribe to NFTs based on a spender. Thenext
handler processes the patches to determine what NFTs has been added, removed or what data in NFT replaced.