Skip to content

Commit

Permalink
feat: Query Hasura Endpoint through Block Streamer (#745)
Browse files Browse the repository at this point in the history
Block Streamer will be querying block match data from an Indexer's
postgres tables through Hasura. Thus, Block Streamer needs to be able to
query and parse data returned by Hasura's graphQL APIs.

This PR introduces a crate which can generate code necessary to parse
returned data from a graphQL query. I've also created a struct which
encapsulates the code for making graphQL calls for ease of use when
integrating or mocking this feature in the future.
  • Loading branch information
darunrs committed May 23, 2024
1 parent 1eac90b commit ffced35
Show file tree
Hide file tree
Showing 10 changed files with 6,749 additions and 7 deletions.
296 changes: 295 additions & 1 deletion block-streamer/Cargo.lock

Large diffs are not rendered by default.

5 changes: 3 additions & 2 deletions block-streamer/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,14 @@ borsh = "0.10.2"
cached = "0.49.3"
chrono = "0.4.25"
futures = "0.3.5"
graphql_client = { version = "0.14.0", features = ["reqwest"] }
lazy_static = "1.4.0"
mockall = "0.11.4"
near-lake-framework = "0.7.8"
prometheus = "0.13.3"
prost = "0.12.3"
redis = { version = "0.21.5", features = ["tokio-comp", "connection-manager"] }
reqwest = { version = "^0.11.0", features = ["json"] }
serde = { version = "1", features = ["derive"] }
serde_json = "1.0.55"
tracing = "0.1.40"
Expand All @@ -31,8 +34,6 @@ wildmatch = "2.1.1"

registry-types = { path = "../registry/types" }

near-lake-framework = "0.7.8"

[build-dependencies]
tonic-build = "0.10"

Expand Down
23 changes: 23 additions & 0 deletions block-streamer/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// TODO: Improve README further

## GraphQL Code Generation
Querying a GraphQL requires informing Rust of the correct types to deserialize the response data into. In order to do this, the schema of the GraphQL data needs to be introspected. Following that, the query intended to be called needs to be fully defined. With this information, code can be automatically generated using the macro provided in graphql-client. Below are the instructions on how to do so.

### Generating schema.graphql
Follow the instructions in the [Hasura Documentation](https://hasura.io/docs/latest/schema/common-patterns/export-graphql-schema/) to introspect the schema and generate the graphql file. Keep in mind that a header for the role needs to be provided. Otherwise, the schemas remain hidden from the public/default user.

For example: `gq https://my-graphql-engine.com/v1/graphql -H 'X-Hasura-Role: someaccount_near' --introspect > schema.graphql`

### Generating Rust types from query
After acquiring the graphql file for the schema, write the queries that need to be called in individual graphql files. Once written, add the following code template to a Rust file and the code will be auto generated using the macro. Assuming there are no problems generating the code, the code will be immediately usable.

```
#[derive(GraphQLQuery)]
#[graphql(
schema_path = "PATH/TO/schema.graphql",
query_path = "PATH/TO/query.graphql",
response_derives = "Debug",
normalization = "rust"
)]
struct QueryNameInPascalCase;
```
6 changes: 6 additions & 0 deletions block-streamer/graphql/darunrs_near/get_bitmaps_exact.graphql
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
query GetBitmapsExact($block_date: date, $receiver_ids: [String!], $limit: Int, $offset: Int) {
darunrs_near_bitmap_v5_actions_index(limit: $limit, offset: $offset, where: {block_date: {_eq: $block_date}, receiver: {receiver: {_in: $receiver_ids}}}) {
bitmap
first_block_height
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
query GetBitmapsWildcard($block_date: date, $receiver_ids: String, $limit: Int, $offset: Int) {
darunrs_near_bitmap_v5_actions_index(limit: $limit, offset: $offset, where: {block_date: {_eq: $block_date}, receiver: {receiver: {_regex: $receiver_ids}}}) {
bitmap
first_block_height
}
}
Loading

0 comments on commit ffced35

Please sign in to comment.