SuiGraphQLClient
Connect to Sui over GraphQL with type-safe queries and subscriptions
SuiGraphQLClient talks to the Sui GraphQL API. It implements the same client API as
SuiGrpcClient, so anything on Querying data and
Signing and execution works here unchanged:
import { SuiGraphQLClient } from '@mysten/sui/graphql';
const client = new SuiGraphQLClient({
url: 'https://graphql.mainnet.sui.io/graphql',
network: 'mainnet',
});
const { object } = await client.getObject({
objectId: '0x...',
include: { content: true },
});Beyond that shared surface, the client can run any query the schema supports, typed from the schema itself. The rest of this page covers writing those queries; see the GraphQL reference for the schema.
The constructor takes:
| Option | Description |
|---|---|
url | GraphQL endpoint: an absolute http(s) URL, or a path such as /graphql in a browser or with a custom fetch |
subscriptionUrl | Subscription endpoint; defaults to url with /subscriptions appended |
network | Network the endpoint serves, used for caching and MVR defaults |
headers | Extra headers sent with every request, such as an API key |
fetch | Custom fetch implementation |
queries | Named documents callable through execute |
mvr | Move Registry overrides |
restrictedMoveCalls | Move calls transactions built with this client can't make; see Restricted Move calls |
Public endpoints:
| Network | URL |
|---|---|
| Mainnet | https://graphql.mainnet.sui.io/graphql |
| Testnet | https://graphql.testnet.sui.io/graphql |
| Devnet | https://graphql.devnet.sui.io/graphql |
Writing queries
Use query to run any GraphQL document against the endpoint:
import { graphql } from '@mysten/sui/graphql/schema';
const chainIdentifierQuery = graphql(`
query {
chainIdentifier
}
`);
const result = await client.query({ query: chainIdentifierQuery });
console.log(result.data?.chainIdentifier);Type safety
The graphql function is powered by gql.tada, which types results and
variables from the schema itself, with no code generation step and no hand-written result types.
Fields you did not select are not on the result type, and variables are checked against the query:
const getSuinsName = graphql(`
query getSuiName($address: SuiAddress!) {
address(address: $address) {
defaultNameRecord {
domain
}
}
}
`);
async function getDefaultSuinsName(address: string) {
const result = await client.query({
query: getSuinsName,
variables: { address },
});
return result.data?.address?.defaultNameRecord?.domain;
}Using typed documents with other clients
The graphql function returns document nodes implementing the
TypedDocumentNode standard, so the
same typed documents work with most GraphQL clients:
import { useQuery } from '@apollo/client';
import { graphql } from '@mysten/sui/graphql/schema';
const chainIdentifierQuery = graphql(`
query {
chainIdentifier
}
`);
function ChainIdentifier() {
// Result types flow through the other client's own hooks and methods
const { data } = useQuery(chainIdentifierQuery);
return <span>{data?.chainIdentifier}</span>;
}Handling errors
query returns { data, errors } rather than throwing on GraphQL errors, because a response can be
partially successful, where some fields resolve and others fail. Check errors before trusting
data:
const { data, errors } = await client.query({ query: chainIdentifierQuery });
if (errors?.length) {
throw new AggregateError(errors.map((error) => new Error(error.message)));
}A transport-level failure (a network error, a non-2xx response or a malformed body) throws
SuiGraphQLRequestError instead. Its reason is set when repeating the request may succeed:
Unavailable for network failures and HTTP 503, RateLimited for HTTP 429, and ServerError for
HTTP 408 and other 5xx responses except 501.
Named queries
Pass a queries map when constructing the client and call them by name with execute. This keeps
query documents in one place instead of threading them through your call sites:
const client = new SuiGraphQLClient({
url: 'https://graphql.mainnet.sui.io/graphql',
network: 'mainnet',
queries: {
getSuinsName,
chainIdentifier: chainIdentifierQuery,
},
});
const result = await client.execute('getSuinsName', {
variables: { address: '0xabc...' },
});Results and variables stay typed from the document the name resolves to.
Reading your own writes
GraphQL reads an index that trails execution slightly, so a transaction executeTransaction has
already returned might not appear in a query for a moment. Use
waitForTransaction before reading back a
transaction's effects.
Subscriptions
To follow checkpoints, transactions, or events with history replay, resume tokens, and reconnection, use ledger streams.
Use subscribe for any subscription document the endpoint supports. Results and variables are
inferred from typed documents, just as with query:
import { SuiGraphQLClient } from '@mysten/sui/graphql';
import { graphql } from '@mysten/sui/graphql/schema';
const client = new SuiGraphQLClient({
url: 'https://graphql.devnet.sui.io/graphql',
network: 'devnet',
});
const checkpoints = graphql(`
subscription Checkpoints {
checkpoints {
cursor
node {
sequenceNumber
digest
}
}
}
`);
for await (const result of client.subscribe({ query: checkpoints })) {
if (result.errors?.length) {
console.error(result.errors);
continue;
}
console.log(result.data?.checkpoints);
}Subscriptions stream server-sent events over HTTP POST, using the client's headers and fetch.
Set subscriptionUrl if your provider serves subscriptions at a different URL.
Each result keeps its GraphQL errors alongside any partial data. HTTP, network and malformed
response failures throw SuiGraphQLRequestError. The loop ends when the server closes the
subscription, and subscribe does not reconnect.
The request starts when iteration starts. break out of the loop to close the subscription.
Aborting the signal also closes it, but rejects the pending iteration with the abort reason:
const controller = new AbortController();
try {
for await (const result of client.subscribe({ query: checkpoints, signal: controller.signal })) {
console.log(result.data?.checkpoints);
}
} catch (error) {
if (!controller.signal.aborted) throw error;
}