Building Queries

Send your first graphQL query and get ready to be blown away

We will take the example of the spl-governance program and build graphQL queries to fetch data, instead of the usual getProgramAccounts() call.

Use our GraphQL demo project to easily get started and try out these queries in code.

#Get all proposalsV2 accounts

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 how we have included all the data fields that are available to us, you can cherry pick fields that are useful to you. For example if you only want names and addresses of the proposals. The the query becomes like

query MyQuery {
  GovER5Lthms3bLBqWub97yVrMmEogzX7xNjdXpPPCVZw_ProposalV2 {
    name
    pubkey
  }
}

By default the API will return 1000 accounts data in response. You can control how many accounts you need by specifying limit. The query becomes like below.

query MyQuery {
  GovER5Lthms3bLBqWub97yVrMmEogzX7xNjdXpPPCVZw_ProposalV2(limit: 100) {
    name
    pubkey
  }
}

Ideally there would be thousands of accounts in a program, so lets see how can we paginate and get more responses.

Last updated