11use std:: sync:: Arc ;
22
3- use graphql_parser:: schema:: Document ;
43use graphql_tools:: validation:: { utils:: ValidationError , validate:: ValidationPlan } ;
54use hive_router_config:: HiveRouterConfig ;
65use hive_router_plan_executor:: {
6+ executors:: error:: SubgraphExecutorError ,
77 introspection:: schema:: { SchemaMetadata , SchemaWithMetadata } ,
88 SubgraphExecutorMap ,
99} ;
1010use hive_router_query_planner:: {
11- planner:: { plan_nodes:: QueryPlan , Planner } ,
11+ planner:: { plan_nodes:: QueryPlan , Planner , PlannerError } ,
1212 state:: supergraph_state:: SupergraphState ,
13+ utils:: parsing:: parse_schema,
1314} ;
1415use moka:: future:: Cache ;
1516
16- use crate :: pipeline:: normalize:: GraphQLNormalizationPayload ;
17+ use crate :: {
18+ pipeline:: normalize:: GraphQLNormalizationPayload ,
19+ supergraph:: { base:: LoadSupergraphError , resolve_from_config} ,
20+ } ;
1721
1822pub struct RouterSharedState {
1923 pub schema_metadata : SchemaMetadata ,
@@ -27,23 +31,32 @@ pub struct RouterSharedState {
2731 pub router_config : HiveRouterConfig ,
2832}
2933
34+ #[ derive( Debug , thiserror:: Error ) ]
35+ pub enum RouterSharedStateError {
36+ #[ error( "Failed to load supergraph: {0}" ) ]
37+ SupergraphInitError ( #[ from] LoadSupergraphError ) ,
38+ #[ error( "Failed to init planner: {0}" ) ]
39+ PlannerInitError ( #[ from] PlannerError ) ,
40+ #[ error( "Failed to init executor: {0}" ) ]
41+ ExecutorInitError ( #[ from] SubgraphExecutorError ) ,
42+ }
43+
3044impl RouterSharedState {
31- pub fn new (
32- parsed_supergraph_sdl : Document < ' static , String > ,
33- router_config : HiveRouterConfig ,
34- ) -> Arc < Self > {
45+ pub async fn new ( router_config : HiveRouterConfig ) -> Result < Arc < Self > , RouterSharedStateError > {
46+ let mut supergraph_source_loader = resolve_from_config ( & router_config. supergraph ) . await ?;
47+ supergraph_source_loader. reload ( ) . await ?;
48+ let supergraph_sdl = supergraph_source_loader. current ( ) . unwrap ( ) ;
49+ let parsed_supergraph_sdl = parse_schema ( supergraph_sdl) ;
3550 let supergraph_state = SupergraphState :: new ( & parsed_supergraph_sdl) ;
36- let planner =
37- Planner :: new_from_supergraph ( & parsed_supergraph_sdl) . expect ( "failed to create planner" ) ;
51+ let planner = Planner :: new_from_supergraph ( & parsed_supergraph_sdl) ?;
3852 let schema_metadata = planner. consumer_schema . schema_metadata ( ) ;
3953
4054 let subgraph_executor_map = SubgraphExecutorMap :: from_http_endpoint_map (
4155 supergraph_state. subgraph_endpoint_map ,
4256 router_config. traffic_shaping . clone ( ) ,
43- )
44- . expect ( "Failed to create subgraph executor map" ) ;
57+ ) ?;
4558
46- Arc :: new ( Self {
59+ Ok ( Arc :: new ( Self {
4760 schema_metadata,
4861 planner,
4962 validation_plan : graphql_tools:: validation:: rules:: default_rules_validation_plan ( ) ,
@@ -53,6 +66,6 @@ impl RouterSharedState {
5366 parse_cache : moka:: future:: Cache :: new ( 1000 ) ,
5467 normalize_cache : moka:: future:: Cache :: new ( 1000 ) ,
5568 router_config,
56- } )
69+ } ) )
5770 }
5871}
0 commit comments