Define your remote source and implement RemoteGraphQLDataSource for it
```rust pub struct CommonSource { pub name: String, pub addr: String, pub tls: bool, }
impl RemoteGraphQLDataSource for CommonSource { fn name(&self) -> &str { &self.name } fn address(&self) -> &str { &self.addr } fn tls(&self) -> bool { self.tls } } ```
Build a gateway-server with your sources
rust
let gateway_server = GatewayServer::builder()
.with_source(CommonSource::new("countries", "countries.trevorblades.com", true))
.build();
Configure reqwest handlers
```rust use asyncgraphql::http::{GraphQLPlaygroundConfig, playgroundsource}; use apollogatewayrs::{actix::{graphqlrequest, graphqlsubscription}};
pub async fn playground() -> HttpResponse { let html = playgroundsource(GraphQLPlaygroundConfig::new("/").subscriptionendpoint("/")); HttpResponse::Ok() .contenttype("text/html; charset=utf-8") .body(html) } fn configureapi(config: &mut actixweb::web::ServiceConfig) { config.service( actixweb::web::resource("/") .route(actixweb::web::post().to(graphqlrequest)) .route( actixweb::web::get() .guard(actixweb::guard::Header("upgrade", "websocket")) .to(graphqlsubscription), ) .route(actixweb::web::get().to(playground)), ); } ``` And spawn actix-web server!
rust
async fn main() -> std::io::Result<()> {
let gateway_server = GatewayServer::builder()
.with_source(CommonSource::new("countries", "countries.trevorblades.com", true))
.build();
let gateway_server = Data::new(gateway_server);
HttpServer::new(move || App::new()
.app_data(gateway_server.clone())
.configure(configure_api)
)
.bind("0.0.0.0:3000")?
.run()
.await
}
You can see full example in examples/actix/common_usage
The gateway can modify the details of an incoming request before executing it across your subgraphs. For example, your subgraphs might all use the same authorization token to associate an incoming request with a particular user. The gateway can add that token to each operation it sends to your subgraphs.
```rust
impl RemoteGraphQLDataSource for AuthSource { fn name(&self) -> &str { &self.name } fn address(&self) -> &str { &self.addr }
async fn did_receive_response(&self, response: &mut Response, ctx: &Context) -> anyhow::Result<()> {
let session = ctx.get_session();
if let Some(jwt) = response.headers.get("user-id")
.and_then(|header| header.parse().ok())
.and_then(|user_id| create_jwt(user_id).ok()) {
session.insert("auth", jwt);
}
Ok(())
}
}
impl RemoteGraphQLDataSource for CommonSource { fn name(&self) -> &str { &self.name } fn address(&self) -> &str { &self.addr }
async fn will_send_request(&self, request: &mut Request, ctx: &Context) -> anyhow::Result<()> {
let session = ctx.get_session();
if let Some(user_id) = session.get("auth")
.ok()
.flatten()
.and_then(|identity| decode_identity(identity).ok())
.map(|claims| claims.claims.id) {
request.headers.insert("user-id".to_string(), user_id.to_string());
}
Ok(())
}
} ```
Apollo-gateway-rs support subscription, use apollogatewayrs::actix::graphql_subscription if you want it.