Skip to content

Commit ef44b4a

Browse files
committed
add unions
1 parent 8da1716 commit ef44b4a

File tree

2 files changed

+28
-7
lines changed

2 files changed

+28
-7
lines changed

graphql_query_derive/src/schema.rs

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -276,8 +276,14 @@ impl ::std::convert::From<graphql_parser::schema::Document> for Schema {
276276
schema.scalars.insert(scalar.name);
277277
}
278278
schema::TypeDefinition::Union(union) => {
279-
let tys: BTreeSet<String> = union.types.into_iter().collect();
280-
schema.unions.insert(union.name, GqlUnion(tys));
279+
let variants: BTreeSet<String> = union.types.into_iter().collect();
280+
schema.unions.insert(
281+
union.name,
282+
GqlUnion {
283+
variants,
284+
description: union.description,
285+
},
286+
);
281287
}
282288
schema::TypeDefinition::Interface(interface) => {
283289
let mut iface = GqlInterface::new(
@@ -374,7 +380,13 @@ impl ::std::convert::From<::introspection_response::IntrospectionResponse> for S
374380
.into_iter()
375381
.filter_map(|t| t.and_then(|t| t.type_ref.name.clone()))
376382
.collect();
377-
schema.unions.insert(name.clone(), GqlUnion(variants));
383+
schema.unions.insert(
384+
name.clone(),
385+
GqlUnion {
386+
description: ty.description.as_ref().map(|d| d.to_owned()),
387+
variants,
388+
},
389+
);
378390
}
379391
Some(__TypeKind::OBJECT) => {
380392
for implementing in ty

graphql_query_derive/src/unions.rs

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,10 @@ use selection::{Selection, SelectionItem};
66
use std::collections::BTreeSet;
77

88
#[derive(Debug, PartialEq)]
9-
pub struct GqlUnion(pub BTreeSet<String>);
9+
pub struct GqlUnion {
10+
pub description: Option<String>,
11+
pub variants: BTreeSet<String>,
12+
}
1013

1114
#[derive(Debug, Fail)]
1215
#[fail(display = "UnionError")]
@@ -109,7 +112,7 @@ impl GqlUnion {
109112
union_variants(selection, query_context, prefix)?;
110113

111114
variants.extend(
112-
self.0
115+
self.variants
113116
.iter()
114117
.filter(|v| used_variants.iter().find(|a| a == v).is_none())
115118
.map(|v| {
@@ -158,7 +161,10 @@ mod tests {
158161
let mut context = QueryContext::new_empty();
159162
let selection = Selection(fields);
160163
let prefix = "Meow";
161-
let union = GqlUnion(BTreeSet::new());
164+
let union = GqlUnion {
165+
description: None,
166+
variants: BTreeSet::new(),
167+
};
162168

163169
context.schema.objects.insert(
164170
"User".to_string(),
@@ -240,7 +246,10 @@ mod tests {
240246
let mut context = QueryContext::new_empty();
241247
let selection = Selection(fields);
242248
let prefix = "Meow";
243-
let union = GqlUnion(BTreeSet::new());
249+
let union = GqlUnion {
250+
description: None,
251+
variants: BTreeSet::new(),
252+
};
244253

245254
let result = union.response_for_selection(&context, &selection, &prefix);
246255

0 commit comments

Comments
 (0)