-
Notifications
You must be signed in to change notification settings - Fork 13
Aggregate queries local support only & recommendation #691
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -138,6 +138,66 @@ When testing, you may need to reset your database: | |
| - **Schema Validation**: Use the Data tab to verify that relationships between entities are correctly established | ||
| - **Performance Monitoring**: Watch for tables that grow unusually large, which might indicate inefficient indexing | ||
|
|
||
| ## Aggregations: local vs hosted (avoid the foot‑gun) | ||
|
|
||
| When developing locally with Hasura, you may notice that GraphQL aggregate helpers (for example, count/sum-style aggregations) are available. On the hosted service, these aggregate endpoints are intentionally not exposed. Aggregations over large datasets can be very slow and unpredictable in production. | ||
|
|
||
| The recommended approach is to compute and store aggregates at indexing time, not at query time. In practice this means maintaining counters, sums, and other rollups in entities as part of your event handlers, and then querying those precomputed values. | ||
|
|
||
| ### Example: indexing-time aggregation | ||
|
|
||
| schema.graphql | ||
|
|
||
| ```graphql | ||
| # singleton; you hardcode the id and load it in and out | ||
| entity GlobalState { | ||
| id: ID! # "global-state" | ||
| count: INT! | ||
| } | ||
|
|
||
| entity Token { | ||
| id: ID! # incremental number | ||
| description: String! | ||
| } | ||
| ``` | ||
|
|
||
| EventHandler.ts | ||
|
|
||
| ```typescript | ||
| const globalStateId = "global-state"; | ||
|
|
||
| NftContract.Mint.loader((event, context) => { | ||
| context.GlobalState.load(globalStateId); | ||
| }); | ||
|
Comment on lines
+168
to
+171
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @DenhamPreen This is invalid API There's also missing |
||
|
|
||
| NftContract.Mint.handler((event, context) => { | ||
| const globalState = context.GlobalState.get(globalStateId); | ||
|
|
||
| if (!globalState) { | ||
| context.log.error("global state doesn't exist"); | ||
| return; | ||
| } | ||
|
|
||
| const incrementedTokenId = globalState.count + 1; | ||
|
|
||
| context.Token.set({ | ||
| id: incrementedTokenId, | ||
| description: event.params.description, | ||
| }); | ||
|
|
||
| context.GlobalState.set({ | ||
| ...globalState, | ||
| count: incrementedTokenId, | ||
| }); | ||
| }); | ||
| ``` | ||
|
|
||
| This pattern scales: you can keep per-entity counters, rolling windows (daily/hourly entities keyed by date), and top-N caches by updating entities as events arrive. Your queries then read these precomputed values directly, avoiding expensive runtime aggregations. | ||
|
|
||
| #### Exceptional cases | ||
|
|
||
| If runtime aggregate queries are a hard requirement for your use case, please reach out and we can evaluate options for your project on the hosted service. Contact us on [Discord](https://discord.gg/envio). | ||
|
|
||
| ## Disable Hasura for Self-Hosted Indexers | ||
|
|
||
| Starting from `envio@2.26.0` it's possible to disable Hasura integration for self-hosted indexers. To do so, set the `ENVIO_HASURA` environment variable to `false`. | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It should be
type, not entity @DenhamPreen