How to query subgraph for historical data

I want to query a subgraph for historical data from a certain block height until the current block. I can pass a block argument to the query but this only leaves me with the data until this block. Is there an “easy” way to achieve this or should I use filters to do so? What would be the most efficient way?

The use case would be to get data from the last week and display it in a graph

First things first: Welcome to The Graph forum. Thanks for your time getting involved with The Graph and writing this question.

It depends heavily if you are the author of the subgraph or if you query a subgraph from someone else: If you are the author: You can add a “blockNumber” field to your entity and then filter for that field through the auto-generated where argument. Furthermore, you could add startBlock to your subgraph.yml to only care about events from a certain block.

If you are not the author of the subgraph, it is more tricky: You can run every query for a specific block height or hash as you said. But other than that, there is not much you can do. Maybe there are timestamps or other fields that you can narrow down your query with. If the subgraph code is open source, you can try to fork, add the block fields and deploy to your own account. But do not forget to open a PR.

If you want to learn more about the different possibilities to query subgraphs, try this tool: https://graphiql-online.com. Our internal playground is currently less advanced.