-
Notifications
You must be signed in to change notification settings - Fork 62
/
hello_world.rs
106 lines (97 loc) · 3.51 KB
/
hello_world.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
use apollo_router::plugin::Plugin;
use apollo_router::plugin::PluginInit;
use apollo_router::register_plugin;
use apollo_router::services::execution;
use apollo_router::services::subgraph;
use apollo_router::services::supergraph;
use schemars::JsonSchema;
use serde::Deserialize;
use tower::BoxError;
use tower::ServiceBuilder;
use tower::ServiceExt;
#[derive(Debug)]
struct HelloWorld {
#[allow(dead_code)]
configuration: Conf,
}
#[derive(Debug, Default, Deserialize, JsonSchema)]
struct Conf {
// Put your plugin configuration here. It will automatically be deserialized from JSON.
name: String, // The name of the entity you'd like to say hello to
}
// This is a bare bones plugin that can be duplicated when creating your own.
#[async_trait::async_trait]
impl Plugin for HelloWorld {
type Config = Conf;
async fn new(init: PluginInit<Self::Config>) -> Result<Self, BoxError> {
Ok(HelloWorld {
configuration: init.config,
})
}
fn supergraph_service(&self, service: supergraph::BoxService) -> supergraph::BoxService {
// Say hello when our service is added to the router_service
// stage of the router plugin pipeline.
#[cfg(test)]
println!("Hello {}", self.configuration.name);
#[cfg(not(test))]
tracing::info!("Hello {}", self.configuration.name);
// Always use service builder to compose your plugins.
// It provides off the shelf building blocks for your plugin.
ServiceBuilder::new()
// .map_request()
// .map_response()
// .rate_limit()
// .checkpoint()
// .timeout()
.service(service)
.boxed()
}
fn execution_service(&self, service: execution::BoxService) -> execution::BoxService {
//This is the default implementation and does not modify the default service.
// The trait also has this implementation, and we just provide it here for illustration.
service
}
// Called for each subgraph
fn subgraph_service(&self, _name: &str, service: subgraph::BoxService) -> subgraph::BoxService {
// Always use service builder to compose your plugins.
// It provides off the shelf building blocks for your plugin.
ServiceBuilder::new()
// .map_request()
// .map_response()
// .rate_limit()
// .checkpoint()
// .timeout()
.service(service)
.boxed()
}
}
// This macro allows us to use it in our plugin registry!
// register_plugin takes a group name, and a plugin name.
//
// In order to keep the plugin names consistent,
// we use using the `Reverse domain name notation`
register_plugin!("example", "hello_world", HelloWorld);
#[cfg(test)]
mod tests {
// If we run this test as follows: cargo test -- --nocapture
// we will see the message "Hello Bob" printed to standard out
#[tokio::test]
async fn display_message() {
let config = serde_json::json!({
"plugins": {
"example.hello_world": {
"name": "Bob"
}
}
});
// Build a test harness. Usually we'd use this and send requests to
// it, but in this case it's enough to build the harness to see our
// output when our service registers.
let _test_harness = apollo_router::TestHarness::builder()
.configuration_json(config)
.unwrap()
.build()
.await
.unwrap();
}
}