cardano-foundation / cardano-graphql

GraphQL API for Cardano
Apache License 2.0
263 stars 104 forks source link

GraphQL big results/pagination #796

Open RdeWilde opened 1 year ago

RdeWilde commented 1 year ago

Ask a question

When doing a GraphQL query which has a large result-set, it might be efficient to use pagination. One can do this by using a combination of limit and offset. This works fine for smaller offsets, but as it increase, response time also seems to increase. So my guess would be, that the full query result is generated, and only the offset - limit part is being returned. But as process time increase, really large offsets take forever to finish, while the number of results might be the same as offset 0.

Is there any way to improve this?

Or if not, is there any other way to accomplish the same thing?

The query used:

query delegatorData (
    $whereStake: ActiveStake_bool_exp,
    $whereReward: Reward_bool_exp,
    $limit: Int
    $offset: Int
) {
    activeStake (where: $whereStake, limit: $limit, offset: $offset) {
      address
      amount
      epochNo
      stakePoolId
    },
    rewards (where: $whereReward, limit: $limit, offset: $offset) {
        address
        amount
        stakePool {
          id
        }
    }
}

With variables:

{
    "limit": 2500,
    "offset": 60000,
    "whereStake": {
        "epochNo": {
            "_eq": 370
        }
    },
    "whereReward": {
        "earnedIn": {
            "number": {
                "_eq": 370
            }
        }
    }
}
RdeWilde commented 1 year ago

Hasura graphql engine v2 seems to support 'edges', but as we are depending on v1.3.3 we can't use that yet. https://github.com/input-output-hk/cardano-graphql/blob/b632720ae5402ec8adec3721a59b538cd77a112b/packages/server/package.json#L27 https://www.npmjs.com/package/@cardano-graphql/api-cardano-db-hasura?activeTab=explore Code > hasura > Dockerfile

FROM hasura/graphql-engine:v1.3.3.cli-migrations-v2

Any plans for v2? https://hasura.io/blog/cursor-offset-pagination-with-hasura-graphql/

Hasura now supports the relay spec and you can get started here

rhyslbw commented 1 year ago

We've just updated to Hasura 2, and it's now out in v8.0.0