Skip to content

Commit

Permalink
delete router_url config for subgraphs in the config file #522 (#553)
Browse files Browse the repository at this point in the history
  • Loading branch information
bnjjj authored Mar 8, 2022
1 parent cac2951 commit 9a3893c
Show file tree
Hide file tree
Showing 41 changed files with 585 additions and 501 deletions.
4 changes: 4 additions & 0 deletions apollo-router-core/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,10 @@ impl From<QueryPlannerError> for Response {
pub enum SchemaError {
/// IO error: {0}
IoError(#[from] std::io::Error),
/// URL parse error for subgraph {0}: {1}
UrlParse(String, url::ParseError),
/// Could not find an URL for subgraph {0}
MissingSubgraphUrl(String),
/// Parsing error(s).
Parse(ParseErrors),
/// Api error(s): {0}
Expand Down
10 changes: 5 additions & 5 deletions apollo-router-core/src/layers/forbid_http_get_mutations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,14 @@ mod forbid_http_get_mutations_tests {
use std::sync::Arc;

use super::*;
use crate::http_compat::RequestBuilder;
use crate::query_planner::fetch::OperationKind;
use crate::{
plugin_utils::{ExecutionRequest, ExecutionResponse, MockExecutionService},
Context, QueryPlan,
};
use http::{Request, StatusCode};
use http::StatusCode;
use reqwest::Url;
use serde_json::json;
use tower::ServiceExt;

Expand Down Expand Up @@ -181,11 +183,9 @@ mod forbid_http_get_mutations_tests {
.query_plan(Arc::new(QueryPlan { root }))
.context(
Context::new().with_request(Arc::new(
Request::builder()
.method(method)
RequestBuilder::new(method, Url::parse("http://test").unwrap())
.body(crate::Request::default())
.unwrap()
.into(),
.unwrap(),
)),
)
.build()
Expand Down
15 changes: 7 additions & 8 deletions apollo-router-core/src/layers/headers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -294,10 +294,13 @@ mod test {
use crate::headers::{
InsertConfig, InsertLayer, PropagateConfig, PropagateLayer, RemoveConfig, RemoveLayer,
};
use crate::http_compat::RequestBuilder;
use crate::layer::ConfigurableLayer;
use crate::plugin_utils::MockSubgraphService;
use crate::{Context, Request, Response, SubgraphRequest, SubgraphResponse};
use http::header::{CONTENT_LENGTH, CONTENT_TYPE, HOST};
use http::Method;
use reqwest::Url;
use std::collections::HashSet;
use std::sync::Arc;
use tower::{BoxError, Layer};
Expand Down Expand Up @@ -475,33 +478,29 @@ mod test {

fn example_originating_request() -> Context {
Context::new().with_request(Arc::new(
http::Request::builder()
.method("GET")
RequestBuilder::new(Method::GET, Url::parse("http://test").unwrap())
.header("da", "vda")
.header("db", "vdb")
.header("dc", "vdc")
.header(HOST, "host")
.header(CONTENT_LENGTH, "2")
.header(CONTENT_TYPE, "graphql")
.body(Request::builder().query("query").build())
.unwrap()
.into(),
.unwrap(),
))
}

fn example_request() -> SubgraphRequest {
SubgraphRequest {
http_request: http::Request::builder()
.method("GET")
http_request: RequestBuilder::new(Method::GET, Url::parse("http://test").unwrap())
.header("aa", "vaa")
.header("ab", "vab")
.header("ac", "vac")
.header(HOST, "rhost")
.header(CONTENT_LENGTH, "22")
.header(CONTENT_TYPE, "graphql")
.body(Request::builder().query("query").build())
.unwrap()
.into(),
.unwrap(),
operation_kind: OperationKind::Query,
context: example_originating_request(),
}
Expand Down
10 changes: 10 additions & 0 deletions apollo-router-core/src/plugin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,16 @@ where
/// Plugins will appear in the configuration as a layer property called: {group}.{name}
#[macro_export]
macro_rules! register_plugin {
($name: literal, $value: ident) => {
startup::on_startup! {
let qualified_name = $name.to_string();

$crate::register_plugin(qualified_name, $crate::PluginFactory::new(|configuration| {
let plugin = $value::new(serde_json::from_value(configuration.clone())?)?;
Ok(Box::new(plugin))
}, |gen| gen.subschema_for::<<$value as $crate::Plugin>::Config>()));
}
};
($group: literal, $name: literal, $value: ident) => {
$crate::reexports::startup::on_startup! {
let qualified_name = if $group == "" {
Expand Down
2 changes: 1 addition & 1 deletion apollo-router-core/src/plugin_utils/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@ pub use service::{
pub use structures::{
execution_request::ExecutionRequest, execution_response::ExecutionResponse,
router_request::RouterRequest, router_response::RouterResponse,
subgraph_response::SubgraphResponse,
subgraph_request::SubgraphRequest, subgraph_response::SubgraphResponse,
};
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use super::CompatRequest;
use crate::{Context, QueryPlan};
use crate::{http_compat::Request, Context, QueryPlan};
use std::sync::Arc;
use typed_builder::TypedBuilder;

Expand All @@ -16,7 +16,7 @@ impl From<ExecutionRequest> for crate::ExecutionRequest {
query_plan: execution_request.query_plan.unwrap_or_default(),
context: execution_request
.context
.unwrap_or_else(|| Context::new().with_request(Arc::new(Default::default()))),
.unwrap_or_else(|| Context::new().with_request(Arc::new(Request::mock()))),
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use super::{from_names_and_values, CompatRequest};
use crate::{Context, Error, Object, Path};
use crate::{http_compat::Request, Context, Error, Object, Path};
use http::{Response, StatusCode};
use serde_json_bytes::Value;
use std::sync::Arc;
Expand Down Expand Up @@ -46,7 +46,7 @@ impl From<ExecutionResponse> for crate::ExecutionResponse {
response,
context: execution_response
.context
.unwrap_or_else(|| Context::new().with_request(Arc::new(Default::default()))),
.unwrap_or_else(|| Context::new().with_request(Arc::new(Request::mock()))),
}
}
}
1 change: 1 addition & 0 deletions apollo-router-core/src/plugin_utils/structures/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ pub mod execution_request;
pub mod execution_response;
pub mod router_request;
pub mod router_response;
pub mod subgraph_request;
pub mod subgraph_response;

type CompatRequest = Arc<crate::http_compat::Request<crate::Request>>;
Expand Down
27 changes: 15 additions & 12 deletions apollo-router-core/src/plugin_utils/structures/router_request.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use super::from_names_and_values;
use crate::{Context, Object};
use http::Request;
use crate::{http_compat::RequestBuilder, Context, Object};
use http::Method;
use reqwest::Url;
use serde_json_bytes::Value;
use std::sync::Arc;
use typed_builder::TypedBuilder;
Expand All @@ -19,20 +20,22 @@ pub struct RouterRequest {

impl From<RouterRequest> for crate::RouterRequest {
fn from(request: RouterRequest) -> Self {
let mut req = Request::builder();
let gql_request = crate::Request {
query: request.query,
operation_name: request.operation_name,
variables: request.variables.unwrap_or_default(),
extensions: request.extensions.unwrap_or_default(),
};

let mut req = RequestBuilder::new(Method::GET, Url::parse("http://default").unwrap());

for (key, value) in request.headers.unwrap_or_default() {
req = req.header(key, value);
}
let req = req
.body(crate::Request {
query: request.query,
operation_name: request.operation_name,
variables: request.variables.unwrap_or_default(),
extensions: request.extensions.unwrap_or_default(),
})
.expect("body is always valid; qed");
let req = req.body(gql_request).expect("body is always valid; qed");

crate::RouterRequest {
context: request.context.unwrap_or_default().with_request(req.into()),
context: request.context.unwrap_or_default().with_request(req),
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use super::{from_names_and_values, CompatRequest};
use crate::{Context, Error, Object, Path};
use crate::{http_compat::Request, Context, Error, Object, Path};
use http::{Response, StatusCode};
use serde_json_bytes::Value;
use std::sync::Arc;
Expand Down Expand Up @@ -46,7 +46,7 @@ impl RouterResponse {
.into(),
context: this
.context
.unwrap_or_else(|| Context::new().with_request(Arc::new(Default::default()))),
.unwrap_or_else(|| Context::new().with_request(Arc::new(Request::mock()))),
}
}
}
42 changes: 42 additions & 0 deletions apollo-router-core/src/plugin_utils/structures/subgraph_request.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
use super::from_names_and_values;
use crate::{fetch::OperationKind, http_compat, Context, Object, Request};
use http::Method;
use reqwest::Url;
use serde_json_bytes::Value;
use std::sync::Arc;
use typed_builder::TypedBuilder;

#[derive(Default, Clone, TypedBuilder)]
#[builder(field_defaults(default, setter(strip_option)))]
pub struct SubgraphRequest {
query: Option<String>,
operation_name: Option<String>,
operation_kind: Option<OperationKind>,
variables: Option<Arc<Object>>,
#[builder(default, setter(!strip_option, transform = |extensions: Vec<(&str, Value)>| Some(from_names_and_values(extensions))))]
extensions: Option<Object>,
context: Option<Context<()>>,
}

impl From<SubgraphRequest> for crate::SubgraphRequest {
fn from(request: SubgraphRequest) -> Self {
let gql_req = crate::Request {
query: request.query,
operation_name: request.operation_name,
variables: request.variables.unwrap_or_default(),
extensions: request.extensions.unwrap_or_default(),
};
let req_compat: http_compat::Request<Request> =
http_compat::RequestBuilder::new(Method::GET, Url::parse("http://default").unwrap())
.body(gql_req)
.expect("won't fail because our url is valid; qed");
crate::SubgraphRequest {
context: request
.context
.unwrap_or_default()
.with_request(Arc::new(req_compat.clone())),
http_request: req_compat,
operation_kind: request.operation_kind.unwrap_or(OperationKind::Query),
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use super::from_names_and_values;
use crate::{Context, Error, Object, Path};
use crate::{http_compat::Request, Context, Error, Object, Path};
use http::{Response, StatusCode};
use serde_json_bytes::Value;
use std::sync::Arc;
Expand Down Expand Up @@ -46,7 +46,7 @@ impl From<SubgraphResponse> for crate::SubgraphResponse {
response,
context: subgraph_response
.context
.unwrap_or_else(|| Context::new().with_request(Arc::new(Default::default()))),
.unwrap_or_else(|| Context::new().with_request(Arc::new(Request::mock()))),
}
}
}
25 changes: 15 additions & 10 deletions apollo-router-core/src/query_planner/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -370,16 +370,21 @@ pub(crate) mod fetch {
.await?;

let subgraph_request = SubgraphRequest {
http_request: http::Request::builder()
.method(http::Method::POST)
.body(
Request::builder()
.query(operation)
.variables(Arc::new(variables.clone()))
.build(),
)
.unwrap()
.into(),
http_request: http_compat::RequestBuilder::new(
http::Method::POST,
schema
.subgraphs()
.find_map(|(name, url)| (name == service_name).then(|| url))
.expect("we can unwrap here because we already checked the subgraph url")
.clone(),
)
.body(
Request::builder()
.query(operation)
.variables(Arc::new(variables.clone()))
.build(),
)
.expect("it won't fail because the url is correct and already checked; qed"),
context: context.clone(),
operation_kind: *operation_kind,
};
Expand Down
12 changes: 6 additions & 6 deletions apollo-router-core/src/query_planner/testdata/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -104,12 +104,12 @@ url: String!
scalar join__FieldSet

enum join__Graph {
ACCOUNTS @join__graph(name: "accounts" url: "")
BOOKS @join__graph(name: "books" url: "")
DOCUMENTS @join__graph(name: "documents" url: "")
INVENTORY @join__graph(name: "inventory" url: "")
PRODUCT @join__graph(name: "product" url: "")
REVIEWS @join__graph(name: "reviews" url: "")
ACCOUNTS @join__graph(name: "accounts" url: "http://accounts")
BOOKS @join__graph(name: "books" url: "http://books")
DOCUMENTS @join__graph(name: "documents" url: "http://documents")
INVENTORY @join__graph(name: "inventory" url: "http://inventory")
PRODUCT @join__graph(name: "product" url: "http://products")
REVIEWS @join__graph(name: "reviews" url: "http://reviews")
}

type KeyValue {
Expand Down
Loading

0 comments on commit 9a3893c

Please sign in to comment.