Sitemap
Khac Vy

Khac Vy

Querying Program Data on Solana, powered by SHYFT’s GraphQL APIs

In this episode, we will explore the process of fetching and querying the entire GovernanceV2 program on Solana, using Shyft GraphQL APIs

6 min readNov 17, 2023

--

Press enter or click to view image in full size
Querying program data on Solana, graph ql apis

The Problem with getProgramAccounts()

As Solana developers, we are all familiar with the essential yet challenging RPC call, getProgramAccounts(). While it is the go-to method for querying a program’s account data, its drawbacks are hard to ignore. It’s sluggish, you can’t paginate and apply filters like you would on a database. In essence, it’s not the ideal tool for production, and if you aspire to deliver a seamless user experience, relying solely on getProgramAccounts() is simply not an option.

Press enter or click to view image in full size
Slow get program accounts rpc call

Shyft GraphQL Program APIs: The Simple Solution

Enter Shyft GraphQL Program APIs, transforming the way you interact with Solana’s program account data. In a nutshell, our solution allows you to query any program’s account data with ease, providing a powerful alternative to the traditional getProgramAccounts().

Getting Started

Shyft GraphQL program API lets you access both mainnet and devnet program data. It can be accessed from the below URLs.

Mainnet:

https://programs.shyft.to/v0/graphql/?api_key={your-api-key}&network=mainnet-beta

Devnet:

https://programs.shyft.to/v0/graphql/?api_key={your-api-key}&network=devnet

If you don’t want to set up the code or just want to see how it works, you can access these UI tools, which allow you to easily interact with our GraphQL program API:

Once you paste your GraphQL endpoint into the above UI, it will automatically trigger an introspection call, fetching the entire schema for all the programs that we currently host. It would look like this

Press enter or click to view image in full size

We can now begin utilizing the Shyft GraphQL Program API to retrieve proposal data from the SPL Governance program.

To get started, we will need a few things.

Register your Shyft account and get your own Shyft API key.

x-api-key is an authentication parameter, which gives you access to Shyft APIs. You can get your own API Key from the Shyft website. Just signup with your email id here and you can get it for free. If you already have an Shyft API Key, please skip this step.

NextJs or any dev environment/stack that supports API calls

We have used Next JS for this project, but this can be done in any application development environment. Please refer to this link for more information on how to set up GraphQL with Next.js 13.

Get Khac Vy’s stories in your inbox

Join Medium for free to get updates from this writer.

You can clone the full source code of this article from this repo.

Building query

If you are already familiar with GraphQL, building a query is quite simple. To fetch proposals from the SPL Governance program, use a query like this:

query MyQuery {
GovER5Lthms3bLBqWub97yVrMmEogzX7xNjdXpPPCVZw_ProposalV2 {
abstainVoteWeight
closedAt
denyVoteWeight
descriptionLink
draftAt
executingAt
executionFlags
governance
governingTokenMint
lamports
maxVoteWeight
maxVotingTime
name
options
reserved1
signatoriesCount
signatoriesSignedOffCount
signingOffAt
startVotingAt
state
tokenOwnerRecord
vetoVoteWeight
voteThreshold
voteType
votingAt
votingAtSlot
votingCompletedAt
pubkey
}
}

Note that we have included all the data fields available to us; however, you can cherry-pick fields that are useful to you. For example, if you only want the name and pubkey of the proposals, the query would look like:

query MyQuery {
GovER5Lthms3bLBqWub97yVrMmEogzX7xNjdXpPPCVZw_ProposalV2 {
name
pubkey
}
}

To retrieve the data, the @apollo/client package provides several React hooks that enable us to fetch data from a GraphQL API. In this tutorial, we will utilize the useLazyQuery hook. This choice is made because we want to manually execute the query when the user clicks the Search button, instead of automatically executing the query when the component is mounted.

const query = gql`
query GovER5Lthms3bLBqWub97yVrMmEogzX7xNjdXpPPCVZw_ProposalV2 {
name
pubkey
}
`;

const [getProposals, { loading, data, fetchMore }] = useLazyQuery(query);

const handleSearch = (event: React.FormEvent<HTMLFormElement>) => {
event.preventDefault();
getProposals();
};

The result data will look like

{
"data": {
"GovER5Lthms3bLBqWub97yVrMmEogzX7xNjdXpPPCVZw_ProposalV2": [
{
"name": "Pay 0.1 SOL to HAzL8…tEaqf",
"pubkey": "H8y3YLzB3jVbx5UCY5DBXz2R5rizCBB9uUVoNMacJBRK"
},
{
"name": "NFT",
"pubkey": "3LGcrfPxtGzM25MGaKzUxefkPrage9EiWK5Dg7RRjtFF"
},
{
"name": "Hackathon_1.0 Prize Split",
"pubkey": "69Nbj3BH3kji5saLnrDJbvWKwNBqEUWtEV6vEs32nUze"
}
]
}
}

Applying filters

We don’t just want to fetch all the proposals; we specifically want to retrieve those proposals whose names contain some user-inputted text. To achieve this, we need to update our query to be able to accept the variables

query GovER5Lthms3bLBqWub97yVrMmEogzX7xNjdXpPPCVZw_GovernanceV2(
$where: GovER5Lthms3bLBqWub97yVrMmEogzX7xNjdXpPPCVZw_ProposalV2_bool_exp
) {
GovER5Lthms3bLBqWub97yVrMmEogzX7xNjdXpPPCVZw_ProposalV2(where: $where) {
name
pubkey
}
}

Now we can begin fetching the data. At this point, the getProposals function needs to accept the variable, which is the user-inputted text.

const query = gql`
query GovER5Lthms3bLBqWub97yVrMmEogzX7xNjdXpPPCVZw_GovernanceV2(
$where: GovER5Lthms3bLBqWub97yVrMmEogzX7xNjdXpPPCVZw_ProposalV2_bool_exp
) {
GovER5Lthms3bLBqWub97yVrMmEogzX7xNjdXpPPCVZw_ProposalV2(where: $where) {
name
pubkey
}
}
`;

const [getProposals, { loading, data, fetchMore }] = useLazyQuery(query);

const handleSearch = (event: React.FormEvent<HTMLFormElement>) => {
event.preventDefault();
getProposals({
variables: {
where: {
name: {
_regex: text,
},
}
}
});
};

Ordering and Sorting Data

Our GraphQL API supports sorting the data based on the data fields of our choice. For instance, if we want to sort our proposals in ascending order by draft date, we need to update our query to look like:

query GovER5Lthms3bLBqWub97yVrMmEogzX7xNjdXpPPCVZw_GovernanceV2(
$where: GovER5Lthms3bLBqWub97yVrMmEogzX7xNjdXpPPCVZw_ProposalV2_bool_exp
$orderBy: [GovER5Lthms3bLBqWub97yVrMmEogzX7xNjdXpPPCVZw_ProposalV2_order_by!]
) {
GovER5Lthms3bLBqWub97yVrMmEogzX7xNjdXpPPCVZw_ProposalV2(
where: $where
order_by: $orderBy
) {
name
pubkey
}
}

Similar to filtering data, at this point, our getProposals function will need to accept an additional variable for sorting.

const query = gql`
query GovER5Lthms3bLBqWub97yVrMmEogzX7xNjdXpPPCVZw_GovernanceV2(
$where: GovER5Lthms3bLBqWub97yVrMmEogzX7xNjdXpPPCVZw_ProposalV2_bool_exp
$orderBy: [GovER5Lthms3bLBqWub97yVrMmEogzX7xNjdXpPPCVZw_ProposalV2_order_by!]
) {
GovER5Lthms3bLBqWub97yVrMmEogzX7xNjdXpPPCVZw_ProposalV2(
where: $where
order_by: $orderBy
) {
name
pubkey
}
}
`;

const [getProposals, { loading, data, fetchMore }] = useLazyQuery(query);

const handleSearch = (event: React.FormEvent<HTMLFormElement>) => {
event.preventDefault();
getProposals({
variables: {
where: {
name: {
_regex: text,
},
},
orderBy: [
{
draftAt: "ASC"
},
],
}
});
};

Paginating data

As we all know getProgramAccounts() does not support pagination currently. It becomes very difficult to handle huge amount of data in a single call, most of the time it leads to connection timeouts. With GraphQL, pagination becomes super easy. Let's take the previous query and try to paginate proposals account data.

To implement pagination, we can specify the limit and offset fields in our query. The limit represents the number of rows returned, default is 1000, and the offset indicates the starting point of the result set.

query GovER5Lthms3bLBqWub97yVrMmEogzX7xNjdXpPPCVZw_GovernanceV2(
$where: GovER5Lthms3bLBqWub97yVrMmEogzX7xNjdXpPPCVZw_ProposalV2_bool_exp
$orderBy: [GovER5Lthms3bLBqWub97yVrMmEogzX7xNjdXpPPCVZw_ProposalV2_order_by!]
$limit: Int
$offset: Int
) {
GovER5Lthms3bLBqWub97yVrMmEogzX7xNjdXpPPCVZw_ProposalV2(
where: $where
order_by: $orderBy
limit: $limit
offset: $offset
) {
name
pubkey
}
}

Now, we update our getProposals function to accept the limit and offset variables."

const query = gql`
query GovER5Lthms3bLBqWub97yVrMmEogzX7xNjdXpPPCVZw_GovernanceV2(
$where: GovER5Lthms3bLBqWub97yVrMmEogzX7xNjdXpPPCVZw_ProposalV2_bool_exp
$orderBy: [GovER5Lthms3bLBqWub97yVrMmEogzX7xNjdXpPPCVZw_ProposalV2_order_by!]
$limit: Int
$offset: Int
) {
GovER5Lthms3bLBqWub97yVrMmEogzX7xNjdXpPPCVZw_ProposalV2(
where: $where
order_by: $orderBy
limit: $limit
offset: $offset
) {
name
pubkey
}
}
`;

const [getProposals, { loading, data, fetchMore }] = useLazyQuery(query);

const handleSearch = (event: React.FormEvent<HTMLFormElement>) => {
event.preventDefault();
getProposals({
variables: {
where: {
name: {
_regex: text,
},
},
orderBy: [
{
draftAt: "ASC"
},
],
limit: 10,
offset: 0,
}
});
};

After setting up everything and running the application, we receive results that look like the video below.

Conclusion

In conclusion, dealing with Solana’s getProgramAccounts() has its challenges — it’s slow, lacks features like filtering, and isn’t ideal for smooth user experiences. But here comes the Shyft GraphQL Program APIs to the rescue! These APIs offer a simpler and better way to get program account data in Solana. They fix the issues we face with getProgramAccounts() and make Solana development more flexible and user-friendly. As Solana evolves, tools like Shyft GraphQL Program APIs become crucial for a smoother and more efficient development journey.

We sincerely hope you enjoyed reading this blog post! If you have any inquiries or thoughts, please don’t hesitate to visit our Discord community.

A big thank you for taking the time to read through it!

Resources

--

--