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
Run
It will generate the model files as defined in schema.graphql, create the database and run all the necessary migrations in one shot.
NB! Use with caution in production, as it will delete all the existing records.
Under the fold, yarn booststrap creates a folder generated/graphql-server with an Apollo-based GraphQL server for the query node.
3. Typegen for events and extrinsics
List the events and extrinsics to be used by the mappings and generated type-safe classes using the typegen tool. One can define in a separate yml file or modify the typegen section in manifest.yml
Typegen fetches the metadata from the chain from the block with a given hash (or from the top block if no hash is provided)
4. Mappings and the manifest file
Modify the default mappings in the mappings folder and make sure all the mapping functions are exported. Define the mappings in the mappings section
5. Dockerize
Among other things, the scaffolder generates a docker folder with Dockerfiles.
First, build the builder image:
Now the images for the GraphQL query node and the processor can be built (they use the builder image under the hood)
In order to run the docker-compose stack, we need to create the schema and run the database migrations.
The last command runs yarn db:bootstrap in the builder image. A similar setup strategy may be used for Kubernetes (with builderas a starter container).
Now everything is ready:
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?