Skip to content

Commit

Permalink
feat(composition): default to routing url from graph registry (#873)
Browse files Browse the repository at this point in the history
  • Loading branch information
EverlastingBugstopper authored Oct 19, 2021
1 parent 509045b commit de2e61b
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 59 deletions.
62 changes: 32 additions & 30 deletions crates/rover-client/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ query SubgraphFetchQuery($graph_id: ID!, $variant: String!) {
... on FederatedImplementingServices {
services {
name
url
activePartialSchema {
sdl
}
Expand Down
31 changes: 19 additions & 12 deletions crates/rover-client/src/operations/subgraph/fetch/runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,13 @@ fn get_sdl_from_response_data(
) -> Result<FetchResponse, RoverClientError> {
let graph_ref = input.graph_ref.clone();
let service_list = get_services_from_response_data(graph_ref, response_data)?;
let sdl_contents = get_sdl_for_service(&input.subgraph, service_list)?;
let subgraph = get_subgraph(&input.subgraph, service_list)?;
Ok(FetchResponse {
sdl: Sdl {
contents: sdl_contents,
r#type: SdlType::Subgraph,
contents: subgraph.sdl,
r#type: SdlType::Subgraph {
routing_url: subgraph.url,
},
},
})
}
Expand Down Expand Up @@ -74,17 +76,22 @@ fn get_services_from_response_data(
}
}

fn get_sdl_for_service(
subgraph_name: &str,
services: ServiceList,
) -> Result<String, RoverClientError> {
struct Subgraph {
url: Option<String>,
sdl: String,
}

fn get_subgraph(subgraph_name: &str, services: ServiceList) -> Result<Subgraph, RoverClientError> {
// find the right service by name
let service = services.iter().find(|svc| svc.name == subgraph_name);

// if there is a service, get it's active sdl, otherwise, error and list
// if there is a service, get its active sdl, otherwise, error and list
// available services to fetch
if let Some(service) = service {
Ok(service.active_partial_schema.sdl.clone())
Ok(Subgraph {
url: service.url.clone(),
sdl: service.active_partial_schema.sdl.clone(),
})
} else {
let valid_subgraphs: Vec<String> = services.iter().map(|svc| svc.name.clone()).collect();

Expand Down Expand Up @@ -175,9 +182,9 @@ mod tests {
}
]);
let service_list: ServiceList = serde_json::from_value(json_service_list).unwrap();
let output = get_sdl_for_service("accounts2", service_list);
let output = get_subgraph("accounts2", service_list);
assert_eq!(
output.unwrap(),
output.unwrap().sdl,
"extend type User @key(fields: \"id\") {\n id: ID! @external\n age: Int\n}\n"
.to_string()
);
Expand All @@ -200,7 +207,7 @@ mod tests {
}
]);
let service_list: ServiceList = serde_json::from_value(json_service_list).unwrap();
let output = get_sdl_for_service("harambe-was-an-inside-job", service_list);
let output = get_subgraph("harambe-was-an-inside-job", service_list);
assert!(output.is_err());
}

Expand Down
2 changes: 1 addition & 1 deletion crates/rover-client/src/shared/fetch_response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@ pub struct Sdl {
#[serde(rename_all(serialize = "lowercase"))]
pub enum SdlType {
Graph,
Subgraph,
Subgraph { routing_url: Option<String> },
Supergraph,
}
6 changes: 4 additions & 2 deletions src/command/output.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ impl RoverOutput {
}
RoverOutput::FetchResponse(fetch_response) => {
match fetch_response.sdl.r#type {
SdlType::Graph | SdlType::Subgraph => print_descriptor("SDL"),
SdlType::Graph | SdlType::Subgraph { .. } => print_descriptor("SDL"),
SdlType::Supergraph => print_descriptor("Supergraph SDL"),
}
print_content(&fetch_response.sdl.contents);
Expand Down Expand Up @@ -469,7 +469,9 @@ mod tests {
let mock_fetch_response = FetchResponse {
sdl: Sdl {
contents: "sdl contents".to_string(),
r#type: SdlType::Subgraph,
r#type: SdlType::Subgraph {
routing_url: Some("http://localhost:8000/graphql".to_string()),
},
},
};
let actual_json: JsonOutput = RoverOutput::FetchResponse(mock_fetch_response).into();
Expand Down
40 changes: 26 additions & 14 deletions src/command/supergraph/compose/do_compose.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,13 @@ pub(crate) fn get_subgraph_definitions(
) -> Result<Vec<SubgraphDefinition>> {
let mut subgraphs = Vec::new();

let err_no_routing_url = || {
let err = anyhow!("No routing_url found for schema file.");
let mut err = RoverError::new(err);
err.set_suggestion(Suggestion::ValidComposeRoutingUrl);
err
};

for (subgraph_name, subgraph_data) in &supergraph_config.subgraphs {
match &subgraph_data.schema {
SchemaSource::File { file } => {
Expand All @@ -85,12 +92,10 @@ pub(crate) fn get_subgraph_definitions(
err
})?;

let url = &subgraph_data.routing_url.clone().ok_or_else(|| {
let err = anyhow!("No routing_url found for schema file.");
let mut err = RoverError::new(err);
err.set_suggestion(Suggestion::ValidComposeRoutingUrl);
err
})?;
let url = &subgraph_data
.routing_url
.clone()
.ok_or_else(err_no_routing_url)?;

let subgraph_definition = SubgraphDefinition::new(subgraph_name, url, &schema);
subgraphs.push(subgraph_definition);
Expand All @@ -111,8 +116,8 @@ pub(crate) fn get_subgraph_definitions(
)?;
let schema = introspection_response.result;

// We don't require a routing_url for this variant of a schema,
// if none are provided, just use an empty string.
// We don't require a routing_url in config for this variant of a schema,
// if one isn't provided, just use the URL they passed for introspection.
let url = &subgraph_data
.routing_url
.clone()
Expand All @@ -136,12 +141,19 @@ pub(crate) fn get_subgraph_definitions(
&client,
)?;

// We don't require a routing_url for this variant of a schema,
// if none are provided, just use an empty string.
//
// TODO: this should eventually get the url from the registry
// and use that when no routing_url is provided.
let url = &subgraph_data.routing_url.clone().unwrap_or_default();
// We don't require a routing_url in config for this variant of a schema,
// if one isn't provided, just use the routing URL from the graph registry (if it exists).
let url = if let rover_client::shared::SdlType::Subgraph {
routing_url: Some(graph_registry_routing_url),
} = result.sdl.r#type
{
Ok(subgraph_data
.routing_url
.clone()
.unwrap_or(graph_registry_routing_url))
} else {
Err(err_no_routing_url())
}?;

let subgraph_definition =
SubgraphDefinition::new(subgraph_name, url, &result.sdl.contents);
Expand Down

0 comments on commit de2e61b

Please sign in to comment.