-
Notifications
You must be signed in to change notification settings - Fork 123
docs(router): native plugin system API #7387
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
base: main
Are you sure you want to change the base?
Conversation
Summary of ChangesHello @ardatan, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request focuses on enhancing the documentation for the Hive Router, specifically by adding a comprehensive guide on its native plugin system. The new guide empowers developers to extend the router's functionality using custom Rust plugins, covering everything from initial setup to advanced concepts like lifecycle hooks, context sharing, and configuration. This addition significantly improves the extensibility and customizability of the router for users. Highlights
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
📚 Storybook DeploymentThe latest changes are available as preview in: https://pr-7387.hive-storybook.pages.dev |
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.
Code Review
This pull request introduces a new comprehensive documentation guide titled "Extending the Router," detailing how to create and configure custom Rust plugins for the Hive Router. The guide covers development setup, the router's plugin lifecycle hooks (e.g., on_http_request, on_graphql_params, on_execute, on_subgraph_execute), methods for short-circuiting responses, overriding default behavior, sharing context data between hooks, and configuring/registering plugins. Additionally, the PR includes minor version updates for hive-apollo-router-plugin and hive-console-sdk in Cargo.lock. Review comments primarily focus on enhancing the new documentation's quality by addressing inconsistent heading levels, correcting typos (e.g., an incorrect Rust version, "seperated"), fixing grammatical errors (e.g., "different the end," "different than"), rectifying comment syntax, and critically, recommending the replacement of unwrap() calls with graceful error handling in code examples to promote safer coding practices.
| let allowed_clients: Vec<String> = serde_json::from_str( | ||
| std::fs::read_to_string(self.allowed_ids_path.clone()) | ||
| .unwrap() | ||
| .as_str(), | ||
| ) | ||
| .unwrap(); |
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.
This code uses .unwrap() twice, which can cause the router to panic if the file doesn't exist, is not readable, or contains invalid JSON. In a production-grade plugin, this should be handled gracefully. Consider using match or if let to handle potential errors and log them instead of panicking.
let allowed_clients: Vec<String> =
if let Ok(content) = std::fs::read_to_string(&self.allowed_ids_path) {
serde_json::from_str(&content).unwrap_or_else(|err| {
tracing::error!("Failed to parse allowed clients file: {}", err);
vec![]
})
} else {
tracing::error!("Failed to read allowed clients file");
vec![]
};
|
|
||
| ### Development Setup | ||
|
|
||
| ## Create a new Rust project |
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.
The heading level is inconsistent with the document structure. This heading is h2 (##), but it's a subsection of ### Development Setup (h3). It should be h3 (###) to maintain the correct hierarchy. This also applies to the other sub-headings in this section on lines 56, 80, and 88.
### Create a new Rust project
| ## Create a new Rust project | ||
|
|
||
| First, ensure you have the necessary development environment set up for | ||
| [Rust 1.91.1 or later](https://rust-lang.org/tools/install/). Then, you need to create a new Rust |
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.
| /// This is where you can register your custom plugins | ||
| let plugin_registry = PluginRegistry::new(); | ||
| /// Start the Hive Router with the plugin registry |
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.
In Rust, /// is used for documentation comments that are part of the public API documentation. Inside a function body, regular comments // are more appropriate.
// This is where you can register your custom plugins
let plugin_registry = PluginRegistry::new();
// Start the Hive Router with the plugin registry
| parameters body expected by GraphQL-over-HTTP spec. But we still need to parse the operation into | ||
| AST. | ||
|
|
||
| On the start of this hooks, you can do the following things for example; |
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.
|
|
||
| On the end of this hook, you can do the following things for example; | ||
|
|
||
| - Prevent certain operations from being executed by checked the HTTP headers or other request |
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.
| ready along with the coerced variables. So you can block the operation, manipulate the result, | ||
| variables, etc. | ||
|
|
||
| This is different the end of `on_query_plan` hook because we don't have all the parameters ready |
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.
| variables, etc. | ||
|
|
||
| This is different the end of `on_query_plan` hook because we don't have all the parameters ready | ||
| like coerced variables, filling the introspection fields that are seperated from the actual planning |
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.
| But we still don't have the actual "HTTP" request that would be sent to the subgraph. So this is | ||
| different than `on_subgraph_http_request` hook. So this is before the serialization to HTTP request. |
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.
| let new_header_value = format!("Hello {}", context_data_entry.incoming_data); | ||
| payload.execution_request.headers.insert( | ||
| "x-hello", | ||
| http::HeaderValue::from_str(&new_header_value).unwrap(), |
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.
💻 Website PreviewThe latest changes are available as preview in: https://pr-7387.hive-landing-page.pages.dev |
|
🐋 This PR was built and pushed to the following Docker images: Targets: Platforms: Image Tag: |
|
🐋 This PR was built and pushed to the following Docker images: Targets: Platforms: Image Tag: |
Documentation of graphql-hive/router#482