Skip to content

Commit 20c8e6d

Browse files
committed
create Unknow variant for fragment as default behaviour
1 parent fa05573 commit 20c8e6d

File tree

8 files changed

+10
-163
lines changed

8 files changed

+10
-163
lines changed

graphql_client_cli/README.md

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,6 @@ OPTIONS:
5757
-s, --schema-path <schema_path> Path to GraphQL schema file (.json or .graphql).
5858
-o, --selected-operation <selected_operation>
5959
Name of target query. If you don't set this parameter, cli generate all queries in query file.
60-
--fragments-other-variant
61-
Generate an Unknow variant for enums generated by fragments.
62-
6360
6461
ARGS:
6562
<query_path> Path to the GraphQL query file.

graphql_client_cli/src/generate.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ pub(crate) struct CliCodegenParams {
2121
pub module_visibility: Option<String>,
2222
pub output_directory: Option<PathBuf>,
2323
pub custom_scalars_module: Option<String>,
24-
pub fragments_other_variant: bool,
2524
}
2625

2726
pub(crate) fn generate_code(params: CliCodegenParams) -> CliResult<()> {
@@ -36,7 +35,6 @@ pub(crate) fn generate_code(params: CliCodegenParams) -> CliResult<()> {
3635
schema_path,
3736
selected_operation,
3837
custom_scalars_module,
39-
fragments_other_variant,
4038
} = params;
4139

4240
let deprecation_strategy = deprecation_strategy.as_ref().and_then(|s| s.parse().ok());
@@ -50,8 +48,6 @@ pub(crate) fn generate_code(params: CliCodegenParams) -> CliResult<()> {
5048
.into(),
5149
);
5250

53-
options.set_fragments_other_variant(fragments_other_variant);
54-
5551
if let Some(selected_operation) = selected_operation {
5652
options.set_operation_name(selected_operation);
5753
}

graphql_client_cli/src/main.rs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -77,10 +77,6 @@ enum Cli {
7777
/// --custom-scalars-module='crate::gql::custom_scalars'
7878
#[structopt(short = "p", long = "custom-scalars-module")]
7979
custom_scalars_module: Option<String>,
80-
/// A flag indicating if the enum representing the variants of a fragment union/interface should have a "other" variant
81-
/// --fragments-other-variant
82-
#[structopt(long = "fragments-other-variant")]
83-
fragments_other_variant: bool,
8480
},
8581
}
8682

@@ -113,7 +109,6 @@ fn main() -> CliResult<()> {
113109
schema_path,
114110
selected_operation,
115111
custom_scalars_module,
116-
fragments_other_variant,
117112
} => generate::generate_code(generate::CliCodegenParams {
118113
query_path,
119114
schema_path,
@@ -125,7 +120,6 @@ fn main() -> CliResult<()> {
125120
module_visibility,
126121
output_directory,
127122
custom_scalars_module,
128-
fragments_other_variant,
129123
}),
130124
}
131125
}

graphql_client_codegen/src/codegen/selection.rs

Lines changed: 9 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,6 @@ fn calculate_selection<'a>(
197197
name: variant_name_str.into(),
198198
variant_type: Some(variant_struct_name_str.clone().into()),
199199
on: struct_id,
200-
is_default_variant: false,
201200
});
202201

203202
let expanded_type = ExpandedType {
@@ -248,19 +247,9 @@ fn calculate_selection<'a>(
248247
name: variant_name_str.into(),
249248
on: struct_id,
250249
variant_type: None,
251-
is_default_variant: false,
252250
});
253251
}
254252
}
255-
256-
if *options.fragments_other_variant() {
257-
context.push_variant(ExpandedVariant {
258-
name: "Unknown".into(),
259-
on: struct_id,
260-
variant_type: None,
261-
is_default_variant: true,
262-
});
263-
}
264253
}
265254
}
266255

@@ -441,7 +430,6 @@ struct ExpandedVariant<'a> {
441430
name: Cow<'a, str>,
442431
variant_type: Option<Cow<'a, str>>,
443432
on: ResponseTypeId,
444-
is_default_variant: bool,
445433
}
446434

447435
impl<'a> ExpandedVariant<'a> {
@@ -452,14 +440,7 @@ impl<'a> ExpandedVariant<'a> {
452440
quote!((#ident))
453441
});
454442

455-
if self.is_default_variant {
456-
quote! {
457-
#[serde(other)]
458-
#name_ident #optional_type_ident
459-
}
460-
} else {
461-
quote!(#name_ident #optional_type_ident)
462-
}
443+
quote!(#name_ident #optional_type_ident)
463444
}
464445
}
465446

@@ -557,12 +538,14 @@ impl<'a> ExpandedSelection<'a> {
557538
// of the variants.
558539
if fields.peek().is_none() {
559540
let item = quote! {
560-
#response_derives
561-
#[serde(tag = "__typename")]
562-
pub enum #struct_name {
563-
#(#on_variants),*
564-
}
565-
};
541+
#response_derives
542+
#[serde(tag = "__typename")]
543+
pub enum #struct_name {
544+
#(#on_variants,)*
545+
#[serde(other)]
546+
Unknown,
547+
}
548+
};
566549
items.push(item);
567550
continue;
568551
}

graphql_client_codegen/src/codegen_options.rs

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,6 @@ pub struct GraphQLClientCodegenOptions {
4343
custom_scalars_module: Option<syn::Path>,
4444
/// List of externally defined enum types. Type names must match those used in the schema exactly.
4545
extern_enums: Vec<String>,
46-
/// Flag to trigger generation of Other variant for fragments Enum
47-
fragments_other_variant: bool,
4846
}
4947

5048
impl GraphQLClientCodegenOptions {
@@ -64,7 +62,6 @@ impl GraphQLClientCodegenOptions {
6462
normalization: Normalization::None,
6563
custom_scalars_module: Default::default(),
6664
extern_enums: Default::default(),
67-
fragments_other_variant: Default::default(),
6865
}
6966
}
7067

@@ -203,14 +200,4 @@ impl GraphQLClientCodegenOptions {
203200
pub fn set_extern_enums(&mut self, enums: Vec<String>) {
204201
self.extern_enums = enums;
205202
}
206-
207-
/// Set the graphql client codegen options's fragments other variant.
208-
pub fn set_fragments_other_variant(&mut self, fragments_other_variant: bool) {
209-
self.fragments_other_variant = fragments_other_variant;
210-
}
211-
212-
/// Get a reference to the graphql client codegen options's fragments other variant.
213-
pub fn fragments_other_variant(&self) -> &bool {
214-
&self.fragments_other_variant
215-
}
216203
}

graphql_client_codegen/src/tests/mod.rs

Lines changed: 1 addition & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -46,45 +46,6 @@ fn fragments_other_variant_should_generate_unknown_other_variant() {
4646
.expect("Parse foobars schema");
4747
let schema = Schema::from(schema);
4848

49-
let mut options = GraphQLClientCodegenOptions::new(CodegenMode::Cli);
50-
51-
options.set_fragments_other_variant(true);
52-
let query = crate::query::resolve(&schema, &query).unwrap();
53-
54-
for (_id, operation) in query.operations() {
55-
let generated_tokens = generated_module::GeneratedModule {
56-
query_string,
57-
schema: &schema,
58-
operation: &operation.name,
59-
resolved_query: &query,
60-
options: &options,
61-
}
62-
.to_token_stream()
63-
.expect("Generate foobars module");
64-
let generated_code = generated_tokens.to_string();
65-
66-
let r: syn::parse::Result<proc_macro2::TokenStream> = syn::parse2(generated_tokens);
67-
match r {
68-
Ok(_) => {
69-
// Rust keywords should be escaped / renamed now
70-
assert!(generated_code.contains("# [serde (other)] Unknown"));
71-
assert!(generated_code.contains("Unknown"));
72-
}
73-
Err(e) => {
74-
panic!("Error: {}\n Generated content: {}\n", e, &generated_code);
75-
}
76-
};
77-
}
78-
}
79-
80-
#[test]
81-
fn fragments_other_variant_false_should_not_generate_unknown_other_variant() {
82-
let query_string = include_str!("foobars_query.graphql");
83-
let query = graphql_parser::parse_query(query_string).expect("Parse foobars query");
84-
let schema = graphql_parser::parse_schema(include_str!("foobars_schema.graphql"))
85-
.expect("Parse foobars schema");
86-
let schema = Schema::from(schema);
87-
8849
let options = GraphQLClientCodegenOptions::new(CodegenMode::Cli);
8950

9051
let query = crate::query::resolve(&schema, &query).unwrap();
@@ -105,8 +66,7 @@ fn fragments_other_variant_false_should_not_generate_unknown_other_variant() {
10566
match r {
10667
Ok(_) => {
10768
// Rust keywords should be escaped / renamed now
108-
assert!(!generated_code.contains("# [serde (other)] Unknown"));
109-
assert!(!generated_code.contains("Unknown"));
69+
assert!(generated_code.contains("# [serde (other)] Unknown"));
11070
}
11171
Err(e) => {
11272
panic!("Error: {}\n Generated content: {}\n", e, &generated_code);

graphql_query_derive/src/attributes.rs

Lines changed: 0 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
use std::str::FromStr;
2-
31
use graphql_client_codegen::deprecation::DeprecationStrategy;
42
use graphql_client_codegen::normalization::Normalization;
53

@@ -96,13 +94,6 @@ pub fn extract_normalization(ast: &syn::DeriveInput) -> Result<Normalization, sy
9694
.map_err(|_| syn::Error::new_spanned(ast, NORMALIZATION_ERROR))
9795
}
9896

99-
pub fn extract_fragments_other_variant(ast: &syn::DeriveInput) -> bool {
100-
extract_attr(&ast, "fragments_other_variant")
101-
.ok()
102-
.and_then(|s| FromStr::from_str(s.as_str()).ok())
103-
.unwrap_or(false)
104-
}
105-
10697
#[cfg(test)]
10798
mod test {
10899
use super::*;
@@ -160,63 +151,4 @@ mod test {
160151
Err(e) => assert_eq!(&format!("{}", e), DEPRECATION_ERROR),
161152
};
162153
}
163-
164-
#[test]
165-
fn test_fragments_other_variant_set_to_true() {
166-
let input = "
167-
#[derive(GraphQLQuery)]
168-
#[graphql(
169-
schema_path = \"x\",
170-
query_path = \"x\",
171-
fragments_other_variant = \"true\",
172-
)]
173-
struct MyQuery;
174-
";
175-
let parsed = syn::parse_str(input).unwrap();
176-
assert_eq!(extract_fragments_other_variant(&parsed), true);
177-
}
178-
179-
#[test]
180-
fn test_fragments_other_variant_set_to_false() {
181-
let input = "
182-
#[derive(GraphQLQuery)]
183-
#[graphql(
184-
schema_path = \"x\",
185-
query_path = \"x\",
186-
fragments_other_variant = \"false\",
187-
)]
188-
struct MyQuery;
189-
";
190-
let parsed = syn::parse_str(input).unwrap();
191-
assert_eq!(extract_fragments_other_variant(&parsed), false);
192-
}
193-
194-
#[test]
195-
fn test_fragments_other_variant_set_to_invalid() {
196-
let input = "
197-
#[derive(GraphQLQuery)]
198-
#[graphql(
199-
schema_path = \"x\",
200-
query_path = \"x\",
201-
fragments_other_variant = \"invalid\",
202-
)]
203-
struct MyQuery;
204-
";
205-
let parsed = syn::parse_str(input).unwrap();
206-
assert_eq!(extract_fragments_other_variant(&parsed), false);
207-
}
208-
209-
#[test]
210-
fn test_fragments_other_variant_unset() {
211-
let input = "
212-
#[derive(GraphQLQuery)]
213-
#[graphql(
214-
schema_path = \"x\",
215-
query_path = \"x\",
216-
)]
217-
struct MyQuery;
218-
";
219-
let parsed = syn::parse_str(input).unwrap();
220-
assert_eq!(extract_fragments_other_variant(&parsed), false);
221-
}
222154
}

graphql_query_derive/src/lib.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,11 +63,9 @@ fn build_graphql_client_derive_options(
6363
let response_derives = attributes::extract_attr(input, "response_derives").ok();
6464
let custom_scalars_module = attributes::extract_attr(input, "custom_scalars_module").ok();
6565
let extern_enums = attributes::extract_attr_list(input, "extern_enums").ok();
66-
let fragments_other_variant: bool = attributes::extract_fragments_other_variant(input);
6766

6867
let mut options = GraphQLClientCodegenOptions::new(CodegenMode::Derive);
6968
options.set_query_file(query_path);
70-
options.set_fragments_other_variant(fragments_other_variant);
7169

7270
if let Some(variables_derives) = variables_derives {
7371
options.set_variables_derives(variables_derives);

0 commit comments

Comments
 (0)