Skip to content

Commit 296f2fa

Browse files
authored
fix(docs): updated usage example of apollo-router plugin as a rs lib (#6749)
1 parent 8d92025 commit 296f2fa

File tree

3 files changed

+58
-8
lines changed

3 files changed

+58
-8
lines changed

packages/libraries/router/README.md

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ integration to Apollo-Router.
3030

3131
[Please follow this guide and documentation for integrating Hive with Apollo Router](https://the-guild.dev/graphql/hive/docs/other-integrations/apollo-router)
3232

33-
### Library
33+
### As a Library
3434

3535
If you are
3636
[building a custom Apollo-Router with your own native plugins](https://www.apollographql.com/docs/graphos/routing/customization/native-plugins),
@@ -44,14 +44,21 @@ hive-apollo-router-plugin = "..."
4444
And then in your codebase, make sure to import and register the Hive plugin:
4545

4646
```rs
47+
use apollo_router::register_plugin;
4748
// import the registry instance and the plugin registration function
4849
use hive_apollo_router_plugin::registry::HiveRegistry;
49-
use hive_apollo_router_plugin::usage::register;
50+
// Import the usage plugin
51+
use hive_apollo_router_plugin::usage::UsagePlugin;
52+
// Import persisted documents plugin, if needed
53+
use persisted_documents::PersistedDocumentsPlugin;
54+
5055

5156
// In your main function, make sure to register the plugin before you create or initialize Apollo-Router
5257
fn main() {
5358
// Register the Hive usage_reporting plugin
54-
register();
59+
register_plugin!("hive", "usage", UsagePlugin);
60+
// Register the persisted documents plugin, if needed
61+
register_plugin!("hive", "persisted_documents", PersistedDocumentsPlugin);
5562

5663
// Initialize the Hive Registry instance and start the Apollo Router
5764
match HiveRegistry::new(None).and(apollo_router::main()) {

packages/web/docs/src/content/other-integrations/apollo-router.mdx

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -246,12 +246,16 @@ You can send usage reporting to Hive registry by enabling `hive.usage` plugin in
246246
mod plugins;
247247

248248
use anyhow::Result;
249+
+ use apollo_router::register_plugin;
249250
+ use hive_apollo_router_plugin::registry;
250-
+ use hive_apollo_router_plugin::usage;
251+
+ use hive_apollo_router_plugin::usage::UsagePlugin;
251252

252253
fn main() -> Result<()> {
253254
- apollo_router::main()
254-
+ usage::register();
255+
256+
+ // Register the Hive usage_reporting plugin
257+
+ register_plugin!("hive", "usage", UsagePlugin);
258+
255259
+ match registry::HiveRegistry::new(None).and(apollo_router::main()) {
256260
+ Ok(_) => {}
257261
+ Err(e) => {
@@ -367,6 +371,45 @@ plugins:
367371
cache_size: 1000
368372
```
369373

374+
### Use as library
375+
376+
You may also add the persisted-documents plugin:
377+
378+
```toml filename="Cargo.toml"
379+
[dependencies]
380+
# You can use a "branch", or commit id with "rev" key
381+
hive_apollo_router_plugin = { git = "https://github.com/graphql-hive/console", branch = "main" }
382+
```
383+
384+
Next, update `src/main.rs` file with the following content to add GraphQL Hive:
385+
386+
```diff filename="src/main.rs"
387+
mod plugins;
388+
389+
use anyhow::Result;
390+
+ use apollo_router::register_plugin;
391+
+ use hive_apollo_router_plugin::registry;
392+
+ use hive_apollo_router_plugin::persisted_documents::PersistedDocumentsPlugin;
393+
394+
fn main() -> Result<()> {
395+
- apollo_router::main()
396+
397+
+ // Register the persisted documents plugin, if needed
398+
+ register_plugin!("hive", "persisted_documents", PersistedDocumentsPlugin);
399+
400+
+ match registry::HiveRegistry::new(None).and(apollo_router::main()) {
401+
+ Ok(_) => {}
402+
+ Err(e) => {
403+
+ eprintln!("{}", e);
404+
+ std::process::exit(1);
405+
+ }
406+
+ }
407+
}
408+
```
409+
410+
If you are using both plugins (usage and persisted operations), you'll need to have a
411+
`register_plugin!` call to both plugins.
412+
370413
## Additional Resources
371414

372415
- [Get started with Apollo Federation and Hive guide](/docs/get-started/apollo-federation)

pnpm-lock.yaml

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)