diff --git a/src/generator/from_proto.rs b/src/generator/from_proto.rs index 75a4301c16..6e85e06470 100644 --- a/src/generator/from_proto.rs +++ b/src/generator/from_proto.rs @@ -43,8 +43,12 @@ impl Context { for enum_ in enums { let mut ty = Type::default(); - let enum_name = enum_.name(); - ty.tag = Some(Tag { id: enum_name.to_string() }); + let enum_type = GraphQLType::new(enum_.name()) + .package(&self.package) + .as_enum() + .unwrap(); + + ty.tag = Some(Tag { id: enum_type.id() }); let variants = enum_ .value @@ -59,8 +63,7 @@ impl Context { ty.variants = Some(variants); - let type_name = GraphQLType::new(enum_name).as_enum().unwrap().to_string(); - self = self.insert_type(type_name, ty); + self = self.insert_type(enum_type.to_string(), ty); } self } @@ -139,6 +142,7 @@ impl Context { for method in &service.method { let field_name = GraphQLType::new(method.name()) .package(&self.package) + .append_package(&service_name) .as_method() .unwrap(); @@ -180,12 +184,14 @@ impl Context { grpc_method.service = service_name.clone(); grpc_method.name = field_name.to_string(); + let method = field_name.id(); + cfg_field.grpc = Some(Grpc { base_url: None, body: None, group_by: vec![], headers: vec![], - method: field_name.id(), + method, }); let ty = self diff --git a/src/generator/graphql_type.rs b/src/generator/graphql_type.rs index 67e305ce8a..e8289e9022 100644 --- a/src/generator/graphql_type.rs +++ b/src/generator/graphql_type.rs @@ -17,14 +17,13 @@ pub struct Parsed { #[derive(Debug, Clone)] pub struct Unparsed { - package: Option, + package: Vec, name: String, } #[derive(Debug, Clone)] struct Package { path: Vec, - input: String, } impl Package { @@ -34,12 +33,17 @@ impl Package { if path.is_empty() | input.is_empty() { None } else { - Some(Self { path, input: input.to_string() }) + Some(Self { path }) } } - fn source(&self) -> &str { - &self.input + fn source(&self) -> String { + self.path.join(".") + } + + fn combine(mut self, other: Package) -> Self { + self.path.extend(other.path); + self } } @@ -58,12 +62,16 @@ impl Display for Package { impl GraphQLType { pub fn new(input: &str) -> Self { - Self(Unparsed { package: None, name: input.to_string() }) + Self(Unparsed { package: Vec::new(), name: input.to_string() }) } fn parse(&self, entity: Entity) -> Option> { let unparsed = &self.0; - let parsed_package = unparsed.package.as_deref().and_then(Package::parse); + let parsed_package = unparsed + .package + .iter() + .filter_map(|package| Package::parse(package)) + .reduce(|a, b| a.combine(b)); // Name contains package if unparsed.name.contains(PACKAGE_SEPARATOR) { @@ -108,7 +116,17 @@ impl GraphQLType { } pub fn package(mut self, package: &str) -> Self { - self.0.package = Some(package.to_string()); + self.0.package = vec![package.to_string()]; + self + } + + pub fn prepend_package(mut self, package: &str) -> Self { + self.0.package.insert(0, package.to_string()); + self + } + + pub fn append_package(mut self, package: &str) -> Self { + self.0.package.push(package.to_string()); self } } diff --git a/src/generator/snapshots/tailcall__generator__from_proto__test__from_proto.snap b/src/generator/snapshots/tailcall__generator__from_proto__test__from_proto.snap index 06dd410dba..cbe6c6f860 100644 --- a/src/generator/snapshots/tailcall__generator__from_proto__test__from_proto.snap +++ b/src/generator/snapshots/tailcall__generator__from_proto__test__from_proto.snap @@ -36,6 +36,11 @@ input NEWS_NEWS_ID @tag(id: "news.NewsId") { id: Int } +enum NEWS_STATUS @tag(id: "news.Status") { + DRAFT + PUBLISHED +} + type GREETINGS_A_B_HELLO_REPLY @tag(id: "greetings_a.b.HelloReply") { message: String } @@ -49,12 +54,12 @@ type NEWS_NEWS_LIST @tag(id: "news.NewsList") { } type Query { - greetings_a_b_say_hello(hello_request: GREETINGS_A_B_HELLO_REQUEST!): GREETINGS_A_B_HELLO_REPLY! @grpc(method: "greetings_a.b.SayHello") - greetings_b_c_say_hello(hello_request: GREETINGS_B_C_HELLO_REQUEST!): GREETINGS_B_C_HELLO_REPLY! @grpc(method: "greetings_b.c.SayHello") - news_add_news(news: NEWS_NEWS!): NEWS_NEWS! @grpc(method: "news.AddNews") - news_delete_news(news_id: NEWS_NEWS_ID!): NEWS_EMPTY! @grpc(method: "news.DeleteNews") - news_edit_news(news: NEWS_NEWS!): NEWS_NEWS! @grpc(method: "news.EditNews") - news_get_all_news: NEWS_NEWS_LIST! @grpc(method: "news.GetAllNews") - news_get_multiple_news(multiple_news_id: NEWS_MULTIPLE_NEWS_ID!): NEWS_NEWS_LIST! @grpc(method: "news.GetMultipleNews") - news_get_news(news_id: NEWS_NEWS_ID!): NEWS_NEWS! @grpc(method: "news.GetNews") + greetings_a_b_greeter_say_hello(hello_request: GREETINGS_A_B_HELLO_REQUEST!): GREETINGS_A_B_HELLO_REPLY! @grpc(method: "greetings_a.b.Greeter.SayHello") + greetings_b_c_greeter_say_hello(hello_request: GREETINGS_B_C_HELLO_REQUEST!): GREETINGS_B_C_HELLO_REPLY! @grpc(method: "greetings_b.c.Greeter.SayHello") + news_news_service_add_news(news: NEWS_NEWS!): NEWS_NEWS! @grpc(method: "news.NewsService.AddNews") + news_news_service_delete_news(news_id: NEWS_NEWS_ID!): NEWS_EMPTY! @grpc(method: "news.NewsService.DeleteNews") + news_news_service_edit_news(news: NEWS_NEWS!): NEWS_NEWS! @grpc(method: "news.NewsService.EditNews") + news_news_service_get_all_news: NEWS_NEWS_LIST! @grpc(method: "news.NewsService.GetAllNews") + news_news_service_get_multiple_news(multiple_news_id: NEWS_MULTIPLE_NEWS_ID!): NEWS_NEWS_LIST! @grpc(method: "news.NewsService.GetMultipleNews") + news_news_service_get_news(news_id: NEWS_NEWS_ID!): NEWS_NEWS! @grpc(method: "news.NewsService.GetNews") } diff --git a/src/generator/snapshots/tailcall__generator__from_proto__test__from_proto_no_pkg_file.snap b/src/generator/snapshots/tailcall__generator__from_proto__test__from_proto_no_pkg_file.snap index 2947033ec3..8c4a722ac5 100644 --- a/src/generator/snapshots/tailcall__generator__from_proto__test__from_proto_no_pkg_file.snap +++ b/src/generator/snapshots/tailcall__generator__from_proto__test__from_proto_no_pkg_file.snap @@ -18,5 +18,5 @@ type NEWS_LIST @tag(id: "NewsList") { } type Query { - get_all_news: NEWS_LIST! @grpc(method: "GetAllNews") + news_service_get_all_news: NEWS_LIST! @grpc(method: "NewsService.GetAllNews") } diff --git a/src/generator/snapshots/tailcall__generator__from_proto__test__greetings_proto_file.snap b/src/generator/snapshots/tailcall__generator__from_proto__test__greetings_proto_file.snap index 65548ad9c4..bffcdf6bab 100644 --- a/src/generator/snapshots/tailcall__generator__from_proto__test__greetings_proto_file.snap +++ b/src/generator/snapshots/tailcall__generator__from_proto__test__greetings_proto_file.snap @@ -15,5 +15,5 @@ type GREETINGS_HELLO_REPLY @tag(id: "greetings.HelloReply") { } type Query { - greetings_say_hello(hello_request: GREETINGS_HELLO_REQUEST!): GREETINGS_HELLO_REPLY! @grpc(method: "greetings.SayHello") + greetings_greeter_say_hello(hello_request: GREETINGS_HELLO_REQUEST!): GREETINGS_HELLO_REPLY! @grpc(method: "greetings.Greeter.SayHello") } diff --git a/src/generator/snapshots/tailcall__generator__from_proto__test__required_types.snap b/src/generator/snapshots/tailcall__generator__from_proto__test__required_types.snap index 95ced99cc2..68d87fd6a5 100644 --- a/src/generator/snapshots/tailcall__generator__from_proto__test__required_types.snap +++ b/src/generator/snapshots/tailcall__generator__from_proto__test__required_types.snap @@ -20,5 +20,5 @@ type PERSON_PHONE_NUMBER @tag(id: "person.PhoneNumber") { } type Query { - person_get_person: PERSON_PERSON! @grpc(method: "person.GetPerson") + person_person_service_get_person: PERSON_PERSON! @grpc(method: "person.PersonService.GetPerson") }