Skip to content

Commit 053abf2

Browse files
committed
wip - require __typename for union selections
1 parent 464fb31 commit 053abf2

File tree

7 files changed

+195
-121
lines changed

7 files changed

+195
-121
lines changed

graphql_query_derive/src/fragments.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
use selection::Selection;
21
use proc_macro2::{Ident, Span, TokenStream};
32
use query::QueryContext;
3+
use selection::Selection;
44

55
#[derive(Debug, PartialEq)]
66
pub struct GqlFragment {

graphql_query_derive/src/interfaces.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
use selection::Selection;
21
use objects::GqlObjectField;
32
use proc_macro2::{Ident, Span, TokenStream};
43
use query::QueryContext;
4+
use selection::Selection;
55

66
#[derive(Debug, PartialEq)]
77
pub struct GqlInterface {

graphql_query_derive/src/objects.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ use field_type::FieldType;
33
use heck::{CamelCase, SnakeCase};
44
use proc_macro2::{Ident, Span, TokenStream};
55
use query::QueryContext;
6-
use shared::render_object_field;
76
use selection::*;
7+
use shared::render_object_field;
88

99
#[derive(Debug, PartialEq)]
1010
pub struct GqlObject {
@@ -47,6 +47,7 @@ impl GqlObject {
4747
selection
4848
.0
4949
.iter()
50+
.filter(|selected| selected.name() != "__typename")
5051
.map(|selected| {
5152
if let SelectionItem::Field(selected) = selected {
5253
let ty = self
@@ -102,9 +103,7 @@ impl GqlObject {
102103
#field_name: #type_name
103104
})
104105
}
105-
SelectionItem::InlineFragment(_) => {
106-
unreachable!("inline fragment on object field")
107-
}
106+
SelectionItem::InlineFragment(_) => unreachable!("inline fragment on object field"),
108107
}
109108
}
110109

graphql_query_derive/src/query.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ use field_type::FieldType;
33
use fragments::GqlFragment;
44
use proc_macro2::TokenStream;
55
use schema::Schema;
6-
use std::collections::BTreeMap;
76
use selection::Selection;
7+
use std::collections::BTreeMap;
88

99
pub struct QueryContext {
1010
pub _subscription_root: Option<Vec<TokenStream>>,

graphql_query_derive/src/schema.rs

Lines changed: 19 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ use interfaces::GqlInterface;
88
use objects::{GqlObject, GqlObjectField};
99
use proc_macro2::TokenStream;
1010
use query::QueryContext;
11-
use std::collections::{BTreeMap, BTreeSet};
1211
use selection::Selection;
12+
use std::collections::{BTreeMap, BTreeSet};
1313
use unions::GqlUnion;
1414

1515
pub const DEFAULT_SCALARS: &[&'static str] = &["ID", "String", "Int", "Float", "Boolean"];
@@ -60,16 +60,12 @@ impl Schema {
6060
let prefix = format!("RUST_{}", prefix);
6161
let selection = Selection::from(&q.selection_set);
6262

63-
definitions.extend(definition.field_impls_for_selection(
64-
&context,
65-
&selection,
66-
&prefix,
67-
)?);
68-
Some(definition.response_fields_for_selection(
69-
&context,
70-
&selection,
71-
&prefix,
72-
))
63+
definitions.extend(
64+
definition.field_impls_for_selection(&context, &selection, &prefix)?,
65+
);
66+
Some(
67+
definition.response_fields_for_selection(&context, &selection, &prefix),
68+
)
7369
};
7470
}
7571
query::Definition::Operation(query::OperationDefinition::Mutation(q)) => {
@@ -84,16 +80,12 @@ impl Schema {
8480
let prefix = format!("RUST_{}", prefix);
8581
let selection = Selection::from(&q.selection_set);
8682

87-
definitions.extend(definition.field_impls_for_selection(
88-
&context,
89-
&selection,
90-
&prefix,
91-
)?);
92-
Some(definition.response_fields_for_selection(
93-
&context,
94-
&selection,
95-
&prefix,
96-
))
83+
definitions.extend(
84+
definition.field_impls_for_selection(&context, &selection, &prefix)?,
85+
);
86+
Some(
87+
definition.response_fields_for_selection(&context, &selection, &prefix),
88+
)
9789
};
9890
}
9991
query::Definition::Operation(query::OperationDefinition::Subscription(q)) => {
@@ -110,16 +102,12 @@ impl Schema {
110102
let prefix = format!("RUST_{}", prefix);
111103
let selection = Selection::from(&q.selection_set);
112104

113-
definitions.extend(definition.field_impls_for_selection(
114-
&context,
115-
&selection,
116-
&prefix,
117-
)?);
118-
Some(definition.response_fields_for_selection(
119-
&context,
120-
&selection,
121-
&prefix,
122-
))
105+
definitions.extend(
106+
definition.field_impls_for_selection(&context, &selection, &prefix)?,
107+
);
108+
Some(
109+
definition.response_fields_for_selection(&context, &selection, &prefix),
110+
)
123111
};
124112
}
125113
query::Definition::Operation(query::OperationDefinition::SelectionSet(_)) => {

graphql_query_derive/src/selection.rs

Lines changed: 26 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -108,34 +108,32 @@ mod tests {
108108
Selection(vec![SelectionItem::Field(SelectionField {
109109
name: "animal".to_string(),
110110
fields: Selection(vec![
111-
SelectionItem::Field(SelectionField {
112-
name: "isCat".to_string(),
113-
fields: Selection(Vec::new()),
114-
}),
115-
SelectionItem::Field(SelectionField {
116-
name: "isHorse".to_string(),
117-
fields: Selection(Vec::new()),
118-
}),
119-
SelectionItem::FragmentSpread(SelectionFragmentSpread {
120-
fragment_name: "Timestamps".to_string(),
121-
}),
122-
SelectionItem::Field(SelectionField {
123-
name: "barks".to_string(),
124-
fields: Selection(Vec::new()),
125-
}),
126-
SelectionItem::InlineFragment(SelectionInlineFragment {
127-
on: "Dog".to_string(),
128-
fields: Selection(vec![
129-
SelectionItem::Field(SelectionField {
130-
name: "rating".to_string(),
131-
fields: Selection(Vec::new()),
132-
}),
133-
]),
134-
}),
135-
SelectionItem::Field(SelectionField {
136-
name: "pawsCount".to_string(),
137-
fields: Selection(Vec::new()),
138-
}),
111+
SelectionItem::Field(SelectionField {
112+
name: "isCat".to_string(),
113+
fields: Selection(Vec::new()),
114+
}),
115+
SelectionItem::Field(SelectionField {
116+
name: "isHorse".to_string(),
117+
fields: Selection(Vec::new()),
118+
}),
119+
SelectionItem::FragmentSpread(SelectionFragmentSpread {
120+
fragment_name: "Timestamps".to_string(),
121+
}),
122+
SelectionItem::Field(SelectionField {
123+
name: "barks".to_string(),
124+
fields: Selection(Vec::new()),
125+
}),
126+
SelectionItem::InlineFragment(SelectionInlineFragment {
127+
on: "Dog".to_string(),
128+
fields: Selection(vec![SelectionItem::Field(SelectionField {
129+
name: "rating".to_string(),
130+
fields: Selection(Vec::new()),
131+
})]),
132+
}),
133+
SelectionItem::Field(SelectionField {
134+
name: "pawsCount".to_string(),
135+
fields: Selection(Vec::new()),
136+
}),
139137
]),
140138
})])
141139
);

0 commit comments

Comments
 (0)