Skip to content

Commit

Permalink
Merge pull request #1303 from apollographql/simon/graphql-module
Browse files Browse the repository at this point in the history
Move apollo_router::{Request, Response, error::Error} to a new graphql module
  • Loading branch information
SimonSapin authored Jun 24, 2022
2 parents 3cd4104 + d10c784 commit 11a6a77
Show file tree
Hide file tree
Showing 34 changed files with 269 additions and 210 deletions.
9 changes: 6 additions & 3 deletions NEXT_CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ such as the Request::mock() fn.

By [@garypen](https://github.com/garypen) in https://github.com/apollographql/router/pull/1257

### Rework the entire public API structure ([PR #1216](https://github.com/apollographql/router/pull/1216), [PR #1242](https://github.com/apollographql/router/pull/1242), [PR #1267](https://github.com/apollographql/router/pull/1267), [PR #1277](https://github.com/apollographql/router/pull/1277))
### Rework the entire public API structure ([PR #1216](https://github.com/apollographql/router/pull/1216), [PR #1242](https://github.com/apollographql/router/pull/1242), [PR #1267](https://github.com/apollographql/router/pull/1267), [PR #1277](https://github.com/apollographql/router/pull/1277), [PR #1303](https://github.com/apollographql/router/pull/1303))

* Many items have been removed from the public API and made private.
If you still need some of them, please file an issue.
Expand Down Expand Up @@ -76,24 +76,27 @@ use apollo_router::ApolloRouter;
use apollo_router::Configuration;
use apollo_router::ConfigurationKind;
use apollo_router::Context;
use apollo_router::Error;
use apollo_router::Executable;
use apollo_router::Request;
use apollo_router::Response;
use apollo_router::Schema;
use apollo_router::SchemaKind;
use apollo_router::ShutdownKind;
use apollo_router::error::CacheResolverError;
use apollo_router::error::Error;
use apollo_router::error::FetchError;
use apollo_router::error::JsonExtError;
use apollo_router::error::Location;
use apollo_router::error::NewErrorBuilder;
use apollo_router::error::ParseErrors;
use apollo_router::error::PlannerErrors;
use apollo_router::error::QueryPlannerError;
use apollo_router::error::SchemaError;
use apollo_router::error::ServiceBuildError;
use apollo_router::error::SpecError;
use apollo_router::graphql::Error;
use apollo_router::graphql::NewErrorBuilder;
use apollo_router::graphql::Request;
use apollo_router::graphql::Response;
use apollo_router::json_ext::Object;
use apollo_router::json_ext::Path;
use apollo_router::json_ext::PathElement;
Expand Down
67 changes: 34 additions & 33 deletions apollo-router/src/axum_http_server_factory.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
//! Axum http server factory. Axum provides routing capability on top of Hyper HTTP.
use crate::configuration::{Configuration, ListenAddr};
use crate::graphql;
use crate::http_ext;
use crate::http_server_factory::{HttpServerFactory, HttpServerHandle, Listener, NetworkStream};
use crate::layers::DEFAULT_BUFFER_SIZE;
Expand Down Expand Up @@ -56,11 +57,11 @@ impl AxumHttpServerFactory {

type BufferedService = Buffer<
BoxService<
http_ext::Request<crate::Request>,
http_ext::Request<graphql::Request>,
http_ext::Response<BoxStream<'static, ResponseBody>>,
BoxError,
>,
http_ext::Request<crate::Request>,
http_ext::Request<graphql::Request>,
>;

impl HttpServerFactory for AxumHttpServerFactory {
Expand All @@ -75,15 +76,15 @@ impl HttpServerFactory for AxumHttpServerFactory {
) -> Self::Future
where
RS: Service<
http_ext::Request<crate::Request>,
http_ext::Request<graphql::Request>,
Response = http_ext::Response<BoxStream<'static, ResponseBody>>,
Error = BoxError,
> + Send
+ Sync
+ Clone
+ 'static,

<RS as Service<http_ext::Request<crate::Request>>>::Future: std::marker::Send,
<RS as Service<http_ext::Request<graphql::Request>>>::Future: std::marker::Send,
{
let boxed_service = Buffer::new(service.boxed(), DEFAULT_BUFFER_SIZE);
Box::pin(async move {
Expand Down Expand Up @@ -439,7 +440,7 @@ async fn handle_get(
if let Some(request) = http_request
.uri()
.query()
.and_then(|q| crate::Request::from_urlencoded_query(q.to_string()).ok())
.and_then(|q| graphql::Request::from_urlencoded_query(q.to_string()).ok())
{
let mut http_request = http_request.map(|_| request);
*http_request.uri_mut() = Uri::from_str(&format!("http://{}{}", host, http_request.uri()))
Expand All @@ -455,7 +456,7 @@ async fn handle_get(
async fn handle_post(
Host(host): Host,
OriginalUri(uri): OriginalUri,
Json(request): Json<crate::Request>,
Json(request): Json<graphql::Request>,
Extension(service): Extension<BufferedService>,
header_map: HeaderMap,
) -> impl IntoResponse {
Expand Down Expand Up @@ -483,7 +484,7 @@ async fn health_check() -> impl IntoResponse {

async fn run_graphql_request(
service: BufferedService,
http_request: Request<crate::Request>,
http_request: Request<graphql::Request>,
) -> impl IntoResponse {
match service.ready_oneshot().await {
Ok(mut service) => {
Expand Down Expand Up @@ -718,7 +719,7 @@ mod tests {
mock! {
#[derive(Debug)]
RouterService {
fn service_call(&mut self, req: Request<crate::Request>) -> Result<http_ext::Response<BoxStream<'static, ResponseBody>>, BoxError>;
fn service_call(&mut self, req: Request<graphql::Request>) -> Result<http_ext::Response<BoxStream<'static, ResponseBody>>, BoxError>;
}
}

Expand Down Expand Up @@ -880,7 +881,7 @@ mod tests {

#[tokio::test]
async fn it_compress_response_body() -> Result<(), ApolloRouterError> {
let expected_response = crate::Response::builder()
let expected_response = graphql::Response::builder()
.data(json!({"response": "yayyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy"})) // Body must be bigger than 32 to be compressed
.build();
let example_response = expected_response.clone();
Expand Down Expand Up @@ -921,7 +922,7 @@ mod tests {
decoder.write_all(&body_bytes.to_vec()).await.unwrap();
decoder.shutdown().await.unwrap();
let response = decoder.into_inner();
let graphql_resp: crate::Response = serde_json::from_slice(&response).unwrap();
let graphql_resp: graphql::Response = serde_json::from_slice(&response).unwrap();
assert_eq!(graphql_resp, expected_response);

// Get query
Expand Down Expand Up @@ -950,7 +951,7 @@ mod tests {
decoder.write_all(&body_bytes.to_vec()).await.unwrap();
decoder.shutdown().await.unwrap();
let response = decoder.into_inner();
let graphql_resp: crate::Response = serde_json::from_slice(&response).unwrap();
let graphql_resp: graphql::Response = serde_json::from_slice(&response).unwrap();
assert_eq!(graphql_resp, expected_response);

server.shutdown().await?;
Expand All @@ -967,7 +968,7 @@ mod tests {
.unwrap();
encoder.shutdown().await.unwrap();
let compressed_body = encoder.into_inner();
let expected_response = crate::Response::builder()
let expected_response = graphql::Response::builder()
.data(json!({"response": "yayyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy"})) // Body must be bigger than 32 to be compressed
.build();
let example_response = expected_response.clone();
Expand Down Expand Up @@ -1003,7 +1004,7 @@ mod tests {
.unwrap();

assert_eq!(
response.json::<crate::Response>().await.unwrap(),
response.json::<graphql::Response>().await.unwrap(),
expected_response,
);

Expand Down Expand Up @@ -1033,7 +1034,7 @@ mod tests {
// let root_span = info_span!("root");
// {
// let _guard = root_span.enter();
let expected_response = crate::Response::builder()
let expected_response = graphql::Response::builder()
.data(json!({"response": "yay"}))
.build();
let example_response = expected_response.clone();
Expand Down Expand Up @@ -1064,7 +1065,7 @@ mod tests {
.unwrap();

assert_eq!(
response.json::<crate::Response>().await.unwrap(),
response.json::<graphql::Response>().await.unwrap(),
expected_response,
);

Expand All @@ -1084,7 +1085,7 @@ mod tests {
);

assert_eq!(
response.json::<crate::Response>().await.unwrap(),
response.json::<graphql::Response>().await.unwrap(),
expected_response,
);

Expand Down Expand Up @@ -1135,7 +1136,7 @@ mod tests {

#[tokio::test]
async fn response_with_custom_endpoint() -> Result<(), ApolloRouterError> {
let expected_response = crate::Response::builder()
let expected_response = graphql::Response::builder()
.data(json!({"response": "yay"}))
.build();
let example_response = expected_response.clone();
Expand Down Expand Up @@ -1179,7 +1180,7 @@ mod tests {
.unwrap();

assert_eq!(
response.json::<crate::Response>().await.unwrap(),
response.json::<graphql::Response>().await.unwrap(),
expected_response,
);

Expand All @@ -1194,7 +1195,7 @@ mod tests {
.unwrap();

assert_eq!(
response.json::<crate::Response>().await.unwrap(),
response.json::<graphql::Response>().await.unwrap(),
expected_response,
);

Expand All @@ -1204,7 +1205,7 @@ mod tests {

#[tokio::test]
async fn response_with_custom_prefix_endpoint() -> Result<(), ApolloRouterError> {
let expected_response = crate::Response::builder()
let expected_response = graphql::Response::builder()
.data(json!({"response": "yay"}))
.build();
let example_response = expected_response.clone();
Expand Down Expand Up @@ -1248,7 +1249,7 @@ mod tests {
.unwrap();

assert_eq!(
response.json::<crate::Response>().await.unwrap(),
response.json::<graphql::Response>().await.unwrap(),
expected_response,
);

Expand All @@ -1263,7 +1264,7 @@ mod tests {
.unwrap();

assert_eq!(
response.json::<crate::Response>().await.unwrap(),
response.json::<graphql::Response>().await.unwrap(),
expected_response,
);

Expand All @@ -1273,7 +1274,7 @@ mod tests {

#[tokio::test]
async fn response_with_custom_endpoint_wildcard() -> Result<(), ApolloRouterError> {
let expected_response = crate::Response::builder()
let expected_response = graphql::Response::builder()
.data(json!({"response": "yay"}))
.build();
let example_response = expected_response.clone();
Expand Down Expand Up @@ -1319,7 +1320,7 @@ mod tests {
.unwrap();

assert_eq!(
response.json::<crate::Response>().await.unwrap(),
response.json::<graphql::Response>().await.unwrap(),
expected_response,
);

Expand All @@ -1334,7 +1335,7 @@ mod tests {
.unwrap();

assert_eq!(
response.json::<crate::Response>().await.unwrap(),
response.json::<graphql::Response>().await.unwrap(),
expected_response,
);
}
Expand All @@ -1356,7 +1357,7 @@ mod tests {
let operation_name = "operationName";
let expected_operation_name = operation_name;

let expected_response = crate::Response::builder()
let expected_response = graphql::Response::builder()
.data(json!({"response": "yay"}))
.build();
let example_response = expected_response.clone();
Expand Down Expand Up @@ -1395,7 +1396,7 @@ mod tests {
.unwrap();

assert_eq!(
response.json::<crate::Response>().await.unwrap(),
response.json::<graphql::Response>().await.unwrap(),
expected_response,
);

Expand All @@ -1416,7 +1417,7 @@ mod tests {
let operation_name = "operationName";
let expected_operation_name = operation_name;

let expected_response = crate::Response::builder()
let expected_response = graphql::Response::builder()
.data(json!({"response": "yay"}))
.build();
let example_response = expected_response.clone();
Expand Down Expand Up @@ -1455,7 +1456,7 @@ mod tests {
.unwrap();

assert_eq!(
response.json::<crate::Response>().await.unwrap(),
response.json::<graphql::Response>().await.unwrap(),
expected_response,
);

Expand Down Expand Up @@ -1495,7 +1496,7 @@ mod tests {
.send()
.await
.unwrap()
.json::<crate::Response>()
.json::<graphql::Response>()
.await
.unwrap();

Expand Down Expand Up @@ -1557,7 +1558,7 @@ mod tests {
#[cfg(unix)]
async fn listening_to_unix_socket() {
let temp_dir = tempfile::tempdir().unwrap();
let expected_response = crate::Response::builder()
let expected_response = graphql::Response::builder()
.data(json!({"response": "yay"}))
.build();
let example_response = expected_response.clone();
Expand Down Expand Up @@ -1586,7 +1587,7 @@ mod tests {
.await;

assert_eq!(
serde_json::from_slice::<crate::Response>(&output).unwrap(),
serde_json::from_slice::<graphql::Response>(&output).unwrap(),
expected_response,
);

Expand All @@ -1595,7 +1596,7 @@ mod tests {
send_to_unix_socket(server.listen_address(), Method::GET, r#"query=query"#).await;

assert_eq!(
serde_json::from_slice::<crate::Response>(&output).unwrap(),
serde_json::from_slice::<graphql::Response>(&output).unwrap(),
expected_response,
);

Expand Down
Loading

0 comments on commit 11a6a77

Please sign in to comment.