Skip to content

Commit 8acd5ab

Browse files
authored
handle preserved scalar and field replacement (#15)
1 parent c433a44 commit 8acd5ab

File tree

13 files changed

+1122
-252
lines changed

13 files changed

+1122
-252
lines changed

Cargo.lock

+952-211
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+12
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ version = "0.4.0"
44
authors = ["tacogips <[email protected]>"]
55
edition = "2018"
66

7+
8+
# TODO(tacogips) is excluding apparantly not working when cargo test?
9+
exclude = ["examples"]
10+
711
[[bin]]
812
name = "async-graphql-reverse"
913
path = "src/bin/main.rs"
@@ -14,6 +18,8 @@ async-graphql = "4.0"
1418
async-graphql-parser = "4.0"
1519
clap = { version = "3.1.0", features = ["derive"] }
1620
env_logger = "0.8"
21+
# TODO(tacogips) lazy_static would be deprecated after rust 1.70
22+
# https://github.com/rust-lang-nursery/lazy-static.rs/issues/214
1723
lazy_static = "1.4"
1824
log = "0.4"
1925
structopt = "0.3"
@@ -26,3 +32,9 @@ heck = "0.3"
2632
paste = "1.0"
2733
toml = "0.5"
2834
serde = { version = "1.0", features = ["derive"] }
35+
36+
37+
[dev-dependencies]
38+
axum = { version = "0.5.1", features = ["headers"] }
39+
tokio = { version = "1.8", features = ["macros", "rt-multi-thread"] }
40+
async-graphql-axum = "4.0.10"

examples/simple/input/reverse.toml

+6
Original file line numberDiff line numberDiff line change
@@ -77,3 +77,9 @@ field_def = "this_is_another_hidden_field: i64"
7777
[ignore]
7878
object = ["WillBeIgnoredType"]
7979
enum = ["IgnoreStatus"]
80+
81+
82+
[[field]]
83+
target_type = "CreateFriendMutationInput"
84+
target_field = "large_data"
85+
replace_field_type = "Upload!"

examples/simple/input/schema.graphql

+3
Original file line numberDiff line numberDiff line change
@@ -51,13 +51,15 @@ type CreateFriendMutationPayload
5151
friend: Friend!
5252
}
5353

54+
scalar Upload
5455
"""
5556
comment for object
5657
in multi line
5758
"""
5859
input CreateFriendMutationInput {
5960
userId: ID!
6061
ignore_input: WillBeIgnoredType
62+
large_data: String
6163
}
6264

6365
input RecursiveInput {
@@ -100,6 +102,7 @@ type Friend implements User {
100102
userType: UserType
101103
others: [Friend]
102104
isActive: Boolean!
105+
favo_rate: Int!
103106
}
104107

105108
type FriendConnection {

examples/simple/output/input_objects.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,12 @@
33
use async_graphql::*;
44
///comment for object
55
///in multi line
6-
#[derive(Debug, InputObject, Clone)]
6+
#[derive(InputObject)]
77
pub struct CreateFriendMutationInput {
88
pub user_id: ID,
9+
pub large_data: Upload,
910
}
10-
#[derive(Debug, InputObject, Clone)]
11+
#[derive(InputObject)]
1112
pub struct RecursiveInput {
1213
pub id: Option<String>,
1314
pub input1: Option<Box<RecursiveInput>>,

examples/simple/output/objects.rs

+4
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ impl Me {
9696
pub struct Friend {
9797
pub id: ID,
9898
pub is_active: bool,
99+
pub favo_rate: i64,
99100
this_is_a_hidden_field: String,
100101
this_is_another_hidden_field: i64,
101102
}
@@ -127,6 +128,9 @@ impl Friend {
127128
pub async fn is_active(&self) -> bool {
128129
self.is_active
129130
}
131+
pub async fn favo_rate(&self) -> i64 {
132+
self.favo_rate
133+
}
130134
}
131135
#[derive(Debug, Clone)]
132136
pub struct FriendConnection {

src/config.rs

+48-12
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,13 @@ pub struct ResolverSetting {
5050
pub argument: Option<Vec<ResolverArgument>>,
5151
}
5252

53+
#[derive(Deserialize, Debug, Clone)]
54+
pub struct FieldSetting {
55+
pub target_type: String,
56+
pub target_field: String,
57+
pub replace_field_type: Option<String>,
58+
}
59+
5360
#[derive(Deserialize, Debug, Clone)]
5461
pub struct Additional {
5562
pub body: String,
@@ -93,7 +100,13 @@ pub struct DefaultSetting {
93100
pub enum_rename_items: Option<String>,
94101
}
95102

96-
pub type FieldsResolverSetting<'a> = HashMap<String, &'a ResolverSetting>;
103+
pub type DefinedEnumName = String;
104+
pub type DefinedTypeName = String;
105+
pub type DefinedFieldName = String;
106+
107+
pub type FieldsResolverSetting<'a> = HashMap<DefinedFieldName, &'a ResolverSetting>;
108+
pub type FieldsSetting<'a> = HashMap<DefinedFieldName, &'a FieldSetting>;
109+
97110
#[derive(Deserialize, Default, Debug)]
98111
pub struct RendererConfig {
99112
pub using: Option<HashMap<String, String>>,
@@ -106,6 +119,7 @@ pub struct RendererConfig {
106119
pub additional: Option<Vec<Additional>>,
107120
pub ignore: Option<Ignore>,
108121
pub r#enum: Option<Vec<EnumSetting>>,
122+
pub field: Option<Vec<FieldSetting>>,
109123
}
110124

111125
impl RendererConfig {
@@ -127,25 +141,25 @@ impl RendererConfig {
127141
}
128142

129143
/// if a type contained this set, the field that has the type supposed to be a member instead of resolver method.
130-
pub fn custom_member_types(&self) -> HashSet<String> {
144+
pub fn custom_member_types(&self) -> HashSet<DefinedTypeName> {
131145
match self.custom_member_types.as_ref() {
132-
None => HashSet::<String>::new(),
146+
None => HashSet::<DefinedTypeName>::new(),
133147
Some(member_types) => member_types.iter().map(|v| v.to_string()).collect(),
134148
}
135149
}
136150

137-
pub fn resolver_setting(&self) -> HashMap<String, FieldsResolverSetting> {
151+
pub fn resolver_setting(&self) -> HashMap<DefinedTypeName, FieldsResolverSetting> {
138152
match self.resolver.as_ref() {
139153
None => return HashMap::new(),
140154
Some(resolver) => {
141155
if resolver.is_empty() {
142156
return HashMap::new();
143157
} else {
144-
let mut result = HashMap::<String, HashMap<String, &ResolverSetting>>::new();
158+
let mut result = HashMap::<DefinedTypeName, FieldsResolverSetting>::new();
145159
for each_resolver in resolver.iter() {
146160
let field_and_resolver_type = result
147161
.entry(each_resolver.target_type.to_string())
148-
.or_insert(HashMap::<String, &ResolverSetting>::new());
162+
.or_insert(FieldsResolverSetting::new());
149163
field_and_resolver_type
150164
.insert(each_resolver.target_field.to_string(), each_resolver);
151165
}
@@ -155,7 +169,29 @@ impl RendererConfig {
155169
}
156170
}
157171

158-
pub fn enum_settings(&self) -> HashMap<String, EnumSetting> {
172+
pub fn field_setting(&self) -> HashMap<DefinedTypeName, FieldsSetting> {
173+
match self.field.as_ref() {
174+
None => return HashMap::new(),
175+
Some(resolver) => {
176+
if resolver.is_empty() {
177+
return HashMap::new();
178+
} else {
179+
let mut result = HashMap::<DefinedTypeName, FieldsSetting>::new();
180+
for each_field in resolver.iter() {
181+
let field_and_resolver_type = result
182+
.entry(each_field.target_type.to_string())
183+
.or_insert(FieldsSetting::new());
184+
185+
field_and_resolver_type
186+
.insert(each_field.target_field.to_string(), each_field);
187+
}
188+
result
189+
}
190+
}
191+
}
192+
}
193+
194+
pub fn enum_settings(&self) -> HashMap<DefinedEnumName, EnumSetting> {
159195
match self.r#enum.as_ref() {
160196
None => HashMap::new(),
161197
Some(enum_settings) => {
@@ -165,20 +201,20 @@ impl RendererConfig {
165201
enum_settings
166202
.into_iter()
167203
.map(|each_enum| (each_enum.target_enum.to_string(), each_enum.clone()))
168-
.collect::<HashMap<String, EnumSetting>>()
204+
.collect::<HashMap<DefinedEnumName, EnumSetting>>()
169205
}
170206
}
171207
}
172208
}
173209

174-
pub fn hidden_fields(&self) -> HashMap<String, HiddenFields> {
210+
pub fn hidden_fields(&self) -> HashMap<DefinedTypeName, HiddenFields> {
175211
match self.hidden_field.as_ref() {
176212
None => return HashMap::new(),
177213
Some(hidden_field) => {
178214
if hidden_field.is_empty() {
179215
return HashMap::new();
180216
} else {
181-
let mut result = HashMap::<String, HiddenFields>::new();
217+
let mut result = HashMap::<DefinedTypeName, HiddenFields>::new();
182218
for each_hidden_field in hidden_field.iter() {
183219
let hidden_field = result
184220
.entry(each_hidden_field.target_type.to_string())
@@ -199,14 +235,14 @@ impl RendererConfig {
199235
}
200236
}
201237

202-
pub fn additional_resolvers(&self) -> HashMap<String, CustomResolvers> {
238+
pub fn additional_resolvers(&self) -> HashMap<DefinedTypeName, CustomResolvers> {
203239
match self.additional_resolver.as_ref() {
204240
None => return HashMap::new(),
205241
Some(additional_resolver) => {
206242
if additional_resolver.is_empty() {
207243
return HashMap::new();
208244
} else {
209-
let mut result = HashMap::<String, CustomResolvers>::new();
245+
let mut result = HashMap::<DefinedTypeName, CustomResolvers>::new();
210246
for custom_resolver in additional_resolver.iter() {
211247
let custom_resolvers = result
212248
.entry(custom_resolver.target_type.to_string())

src/parse/ignoring/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ macro_rules! accumulate_remove_target {
5959
{
6060
Err(e) => {
6161
println!(
62-
"WARN: ignore element not found. {:?}, error {}",
62+
"WARN: ignore unknown element. {:?}, error {}",
6363
$each_field.typ, e
6464
);
6565
}

0 commit comments

Comments
 (0)