Tutorial
Build a Hydra Indexer and GraphQL server from scratch under five minutes
0. Hello Hydra!
Start off by setting up a project folder
mkdir hello-hydra && cd hello-hydra1. From zero to one
Next, run the scaffold command, which generates all the required files:
hydra-cli scaffoldAnswer the prompts and the scaffolder will generate a sample backbone for our Hydra project. This includes:
Sample GraphQL data schema in
schema.graphqldescribing proposals in the Kusama networkSample mapping scripts in the
./mappingfolder translating substrate events into theProposalentity CRUD operationsdocker-compose.ymlfor running a Postgres instance locally as a Docker service..envwith all the necessary environment variables.package.jsonwith a few utility yarn scripts to be used later on.
2. Codegen
Now all is set for generating the Graphql server and the indexer for Kusama proposals:
hydra-cli codegenThe codegen command creates two separate projects:
./generated/graphql-server: this is a GraphQL for querying the proposals./generated/indexer: this is a background indexer tool that fetches the blocks from the Substrate chain (in this case the public Kusama network) and updates the database calling the mapping scripts
3. Set up the database
Now it's time to set up the database:
yarn db:startThis command simply spins up a Postgres Docker image.
yarn db:bootstrapThis creates a DB schema for our data model described in schema.graphql.
4. Start Hydra Indexer
Finally, we're ready to run the indexer and the GraphQL server:
yarn indexer:startKeep an eye on the output to keep track of the indexer's progress.
5. Start Hydra GraphQL server
In a separate terminal window:
yarn server:start:devThe last command starts the server in the dev mode and you will see a GraphQL playground opening in your browser (if not, navigate manually to localhost:4000/graphql). It's time to explore all the GraphQL queries supported out-of-the-box! Note, that depending on the starting block it may take a considerable time for the indexer to catch up with the Kusama network, and until then queries may return empty results.
6. Dockerize
Among other things, the scaffolder generates a top-level package.jsonwith a bunch of convenient yarn targets. For example, putting your Hydra Indexer and GraphQL server is easy as running the following targets:
yarn docker:indexer:buildyarn docker:server:buildThis will create Docker images named hydra-indexer and hydra-graphql-server
What to do next?
Explore more examples
Describe your own schema in
schema.graphqlWrite your indexer mappings
Push your Hydra indexer and GraphQL Docker images to Docker Hub and deploy
Last updated
Was this helpful?