# Query node

This is a query-node project generated by `hydra-cli`. Experiment by modifying `schema.graphql` and the mappings in the `mappings` folder, defined in `manifest.yml`.

## 1. Bootstrap

Run

```bash
yarn && yarn bootstrap
```

and generate the model files as defined in `schema.graphql`, create the database and run all the necessary migrations in one shot.

NB! Don't use in production, as it will delete all the existing records.


## 2. Generate Types for events and extrinsics

A separate tool Hydra Typegen can be used for generating Typescript classes for the event handlers (the _mappings_).  
Run

```bash
yarn typegen
```
to run the [typegen](https://github.com/Joystream/hydra/tree/master/packages/hydra-typegen/README.md) for events and extrinsics defined in `manifest.yml` (it fetches the metadata from an RPC endpoint and blockhash defined there). 


## 3. Build Mappings

Mappings is a separated TypeScript module created in the mappings folder. The handlers exported by the module should match the ones defined in `manifest.yml` in the mappings section. Once the necessary files are generated, build it with

```bash
yarn mappings:build
```

## 4. Run the processor and the GraphQL server

Then run the processor:

```bash
yarn processor:start
```

Afterwards start the GraphQL server in a separate terminal (opens a GraphQL playground at localhost by default):

```bash
yarn query-node:start:dev
```

## 5. Locally hosted indexer

The Hydra Indexer endpoint used by Hydra processor is defined as environment variable `INDEXER_ENDPOINT_URL` sourced from `.env`. There are publicly available Hydra indexers for Polkadot and Subsocial. For other chains, a self-hosted indexer should be used.

The simplest way to run an indexer locally is to run `docker-compose-indexer.yml` with `docker-compose`. The following environment variables must be provided:

- Database connection settings: DB_NAME, DB_HOST, DB_PORT, DB_USER, DB_PASS
- Chain RPC endpoint: WS_PROVIDER_ENDPOINT_URI
- If non-standard types are being used by the Substrate runtime, map type definitions in the json format as an external volume

Follow the links for more information about the [indexer](https://github.com/Joystream/hydra/tree/master/packages/hydra-indexer/README.md) service and [indexer-api-gateway](https://github.com/Joystream/hydra/tree/master/packages/hydra-indexer-gateway/README.md).



# Tmp command order
```
# run from the Joystream monorepo root

# temporarily remove query node dependency from mappings package
sed -i 's/"query-node": "^0.0.0",//' query-node/mappings/package.json

# install dependencies
yarn

# add dummy values to input schema variants that needs it
sed -i 's/type AssetOwnerChannel @variant {/type AssetOwnerChannel @variant {\n  dummy: Int/' query-node/schema.graphql
sed -i 's/type AssetNeverProvided @variant {/type AssetNeverProvided @variant {\n  dummy: Int/' query-node/schema.graphql
sed -i 's/type AssetDeleted @variant {/type AssetDeleted @variant {\n  dummy: Int/' query-node/schema.graphql
sed -i 's/type AssetUploadStatus @variant {/type AssetUploadStatus @variant {\n  dummy: Int/' query-node/schema.graphql

yarn workspace @joystream/content-metadata-protobuf build
yarn workspace @joystream/types build
yarn workspace query-node-root build # this will fail during mappings compilation

# setup TS declarations for query node compilation
sed -i 's/"compilerOptions": {/"compilerOptions": {\n    "declaration": true,/' query-node/generated/graphql-server/tsconfig.json

# add back dependency
sed -i 's/"dependencies": {/"dependencies": {\n    "query-node": "^0.0.0",/' query-node/mappings/package.json

# install query node dependency to mappings
yarn

# yarn workspace query-node compile # probably not needed
sed -i 's/BN/number/' query-node/generated/graphql-server/src/modules/variants/variants.model.ts # replace BigNumber by number

# remove extra variant imports
TMP_VARIANT_IMPORT="import { Asset } from '../variants/variants.model'"
sed -i "s#$TMP_VARIANT_IMPORT##;" query-node/generated/graphql-server/src/modules/channel/channel.model.ts query-node/generated/graphql-server/src/modules/video/video.model.ts
echo $TMP_VARIANT_IMPORT >> query-node/generated/graphql-server/src/modules/channel/channel.model.ts
echo $TMP_VARIANT_IMPORT >> query-node/generated/graphql-server/src/modules/video/video.model.ts

# fix relations cardinality (why is this code generated like this? is input schema wrong or Hydra has a bug?)
sed -i 's/return result.channels/return [result.channels]/' query-node/generated/graphql-server/src/modules/channel-category/channel-category.resolver.ts
sed -i 's/return result.category/return result.category[0]/' query-node/generated/graphql-server/src/modules/channel/channel.resolver.ts
sed -i 's/return result.videos/return [result.videos]/' query-node/generated/graphql-server/src/modules/video-category/video-category.resolver.ts
sed -i 's/return result.category/return result.category[0]/' query-node/generated/graphql-server/src/modules/video/video.resolver.ts

# fix two functions having the same name (why this has to be changed manually?)
sed -i '0,/videoMediaMetadata/s/videoMediaMetadata/videoMediaMetadata2/' query-node/generated/graphql-server/src/modules/video-media-metadata/video-media-metadata.resolver.ts

# add missing dependency
sed -i 's/"dependencies": {/"dependencies": {\n    "pg-listen": "^1.7.0",/' query-node/generated/graphql-server/package.json

# install new dependencies
yarn

echo "set -a; . .env; set +a; yarn workspace query-node build:dev" > tmp.sh
chmod +x tmp.sh
./tmp.sh

# build mappings
yarn workspace query-node-root typegen
yarn workspace query-node-mappings build

```

running the processor:
```
cp types/augment/all/defs.json query-node/mappings/lib/generated/types/typedefs.json
docker-compose up -d db
yarn workspace query-node-root db:create
yarn workspace query-node-root db:migrate

```
