Used to check for browser translation.
用于检测浏览器翻译。
ブラウザの翻訳を検出する
Advanced Topics

GraphQL Client


The Blocklet SDK provides direct access to the underlying GraphQL client, which is a specialized instance of ABTNodeClient. This allows you to perform custom GraphQL queries, mutations, and subscriptions directly against the Blocklet Server's GraphQL API, offering flexibility for advanced use cases not covered by the higher-level SDK methods. For more details on how the AuthClient handles authentication and is initialized, refer to the Auth Service documentation.

To begin, obtain an instance of the AuthClient:

import AuthClient from '@blocklet/sdk/lib/service/auth';

// When running inside a Blocklet environment, the AuthClient automatically configures
// the GraphQL endpoint and authentication using the Blocklet's wallet.
const client = new AuthClient();

doRawQuery#

Use the doRawQuery method to send a single raw GraphQL query to the Blocklet Server. This is useful for fetching specific data when the existing SDK methods do not meet your exact requirements.

Parameters

Name

Type

Description

query

any

The GraphQL query string or a parsed query object.

requestOptions

any

Optional. Custom request configurations (e.g., HTTP headers).

Returns

Name

Type

Description

Promise

Promise<any>

A promise that resolves to the result of the GraphQL operation.

Example

This example demonstrates how to use doRawQuery to retrieve the current node's information.

import AuthClient from '@blocklet/sdk/lib/service/auth';

async function getNodeInfoRaw() {
try {
const client = new AuthClient();
const query = `
query {
getNodeInfo {
info {
did
name
version
initialized
}
}
}
`;
const result = await client.doRawQuery(query);
console.log('Node Info:', result.getNodeInfo.info);
} catch (error) {
console.error('Error fetching node info:', error);
}
}

getNodeInfoRaw();

Example Response

{
"getNodeInfo": {
"info": {
"did": "zNKj2gWkK8wLpL7oP7qR7sT7uV7wX7yZ712345",
"name": "My Blocklet Server",
"version": "1.17.0",
"initialized": true
}
}
}

doRawSubscription#

Use the doRawSubscription method to establish a GraphQL subscription, allowing you to receive real-time updates from the Blocklet Server. This method returns a promise that resolves to a subscription object, which can then be used to listen for data or error events.

Parameters

Name

Type

Description

query

any

The GraphQL subscription string or a parsed query object.

Returns

Name

Type

Description

Promise

Promise<ABTNodeClient.Subscription<any>>

A promise that resolves to a subscription object.

Subscription Object Properties

Method

Parameters

Description

on('data', fn)

fn: (data: T) => any

Registers a callback to be invoked when new data is received.

on('error', fn)

fn: (err: Error) => void

Registers a callback to be invoked when an error occurs.

Example

This example demonstrates how to set up a hypothetical subscription to nodeStatusUpdates. Note that the exact subscription queries depend on the Blocklet Server's GraphQL schema.

import AuthClient from '@blocklet/sdk/lib/service/auth';

async function subscribeToNodeStatus() {
try {
const client = new AuthClient();
const subscriptionQuery = `
subscription {
nodeStatusUpdates {
did
status
uptime
}
}
`;

const subscription = await client.doRawSubscription(subscriptionQuery);

subscription.on('data', (data) => {
console.log('Received Node Status Update:', data.nodeStatusUpdates);
});

subscription.on('error', (error) => {
console.error('Subscription Error:', error);
});

console.log('Subscribed to node status updates. Press Ctrl+C to stop.');

} catch (error) {
console.error('Error setting up subscription:', error);
}
}

subscribeToNodeStatus();

Example Data Received from Subscription

{
"nodeStatusUpdates": {
"did": "zNKj2gWkK8wLpL7oP7qR7sT7uV7wX7yZ712345",
"status": "running",
"uptime": "2 days, 3 hours, 15 minutes"
}
}

doBatchQuery#

Use the doBatchQuery method to send multiple GraphQL queries or mutations in a single HTTP request. This can improve efficiency by reducing network overhead, especially when fetching related data from different parts of the schema.

Parameters

Name

Type

Description

queries

object

An object where keys are arbitrary operation names and values are GraphQL query strings or objects.

requestOptions

any

Optional. Custom request configurations (e.g., HTTP headers).

Returns

Name

Type

Description

Promise

Promise<object>

A promise that resolves to an object containing the results of each individual operation, keyed by their operation names.

Example

This example demonstrates how to use doBatchQuery to fetch both node information and delegation state in a single request.

import AuthClient from '@blocklet/sdk/lib/service/auth';

async function getBatchData() {
try {
const client = new AuthClient();
const queries = {
nodeInfo: `
query {
getNodeInfo {
info {
name
version
}
}
}
`,
delegationState: `
query {
getDelegationState {
state {
delegated
}
}
}
`,
};
const result = await client.doBatchQuery(queries);
console.log('Batch Query Result:', result);
console.log('Node Name:', result.nodeInfo.getNodeInfo.info.name);
console.log('Delegated:', result.delegationState.getDelegationState.state.delegated);
} catch (error) {
console.error('Error performing batch query:', error);
}
}

getBatchData();

Example Response

{
"nodeInfo": {
"getNodeInfo": {
"info": {
"name": "My Blocklet Server",
"version": "1.17.0"
}
}
},
"delegationState": {
"getDelegationState": {
"state": {
"delegated": false
}
}
}
}

Directly interacting with the GraphQL client provides granular control over your data operations on Blocklet Server. This is particularly useful for complex queries or integrating with existing GraphQL toolchains. Continue to Webhook Management to learn about managing real-time event notifications.