@mysten/sui v2.0 and a new dApp Kit are here! Check out the migration guide
Mysten Labs SDKs
Clients

SuiGraphQLClient

The SuiGraphQLClient enables type-safe GraphQL queries against the Sui GraphQL API.

For more details on the Sui GraphQL API, see the GraphQL reference.

The SuiGraphQLClient implements all the the Core API methods:

import { SuiGraphQLClient } from '@mysten/sui/graphql';

const client = new SuiGraphQLClient({
	url: 'https://sui-mainnet.mystenlabs.com/graphql',
	network: 'mainnet',
});

const { object } = await client.getObject({ objectId: '0x...' });

Custom GraphQL queries

To query anything no in the Core API, you can use the query method to execute custom GraphQL queries.

We'll start by creating our client, and executing a very basic query:

import { SuiGraphQLClient } from '@mysten/sui/graphql';
import { graphql } from '@mysten/sui/graphql/schema';

const gqlClient = new SuiGraphQLClient({
	url: 'https://graphql.testnet.sui.io/graphql',
	network: 'testnet',
});

const chainIdentifierQuery = graphql(`
	query {
		chainIdentifier
	}
`);

async function getChainIdentifier() {
	const result = await gqlClient.query({
		query: chainIdentifierQuery,
	});

	return result.data?.chainIdentifier;
}

Type-safety for GraphQL queries

You may have noticed the example above does not include any type definitions for the query. The graphql function used in the example is powered by gql.tada and will automatically provide the required type information to ensure that your queries are properly typed when executed through SuiGraphQLClient.

The graphql function detects variables used by your query, and will ensure that the variables passed to your query are properly typed.

const getSuinsName = graphql(`
	query getSuiName($address: SuiAddress!) {
		address(address: $address) {
			defaultSuinsName
		}
	}
`);

async function getDefaultSuinsName(address: string) {
	const result = await gqlClient.query({
		query: getSuinsName,
		variables: {
			address,
		},
	});

	return result.data?.address?.defaultSuinsName;
}

Using typed GraphQL queries with other GraphQL clients

The graphql function returns document nodes that implement the TypedDocumentNode standard, and will work with the majority of popular GraphQL clients to provide queries that are automatically typed.

import { useQuery } from '@apollo/client';

const chainIdentifierQuery = graphql(`
	query {
		chainIdentifier
	}
`);

function ChainIdentifier() {
	const { loading, error, data } = useQuery(getPokemonsQuery);

	return <div>{data?.chainIdentifier}</div>;
}

On this page