Skip to content

Commit 102dce0

Browse files
committed
Add descriptions to scalars
1 parent 46c7a27 commit 102dce0

File tree

4 files changed

+46
-12
lines changed

4 files changed

+46
-12
lines changed

graphql_query_derive/src/field_type.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ impl FieldType {
2424
FieldType::Named(name) => {
2525
let name_string = name.to_string();
2626

27-
let name = if context.schema.scalars.contains(&name_string)
27+
let name = if context.schema.scalars.contains_key(&name_string)
2828
|| DEFAULT_SCALARS
2929
.iter()
3030
.any(|elem| elem == &name_string.as_str())

graphql_query_derive/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ mod interfaces;
2525
mod introspection_response;
2626
mod objects;
2727
mod query;
28+
mod scalars;
2829
mod schema;
2930
mod selection;
3031
mod shared;

graphql_query_derive/src/scalars.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
use proc_macro2;
2+
3+
#[derive(Debug, PartialEq, PartialOrd, Ord, Eq)]
4+
pub struct Scalar {
5+
pub name: String,
6+
pub description: Option<String>,
7+
}
8+
9+
impl Scalar {
10+
// TODO: do something smarter here
11+
pub fn to_rust(&self) -> proc_macro2::TokenStream {
12+
use proc_macro2::{Ident, Span};
13+
let ident = Ident::new(&self.name, Span::call_site());
14+
let description = match &self.description {
15+
Some(d) => quote!(#[doc = #d]),
16+
None => quote!(),
17+
};
18+
quote!(#description type #ident = String;)
19+
}
20+
}

graphql_query_derive/src/schema.rs

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ use interfaces::GqlInterface;
88
use objects::{GqlObject, GqlObjectField};
99
use proc_macro2::TokenStream;
1010
use query::QueryContext;
11+
use scalars::Scalar;
1112
use selection::Selection;
1213
use std::collections::{BTreeMap, BTreeSet};
1314
use unions::GqlUnion;
@@ -48,7 +49,7 @@ pub struct Schema {
4849
pub inputs: BTreeMap<String, GqlInput>,
4950
pub interfaces: BTreeMap<String, GqlInterface>,
5051
pub objects: BTreeMap<String, GqlObject>,
51-
pub scalars: BTreeSet<String>,
52+
pub scalars: BTreeMap<String, Scalar>,
5253
pub unions: BTreeMap<String, GqlUnion>,
5354
pub query_type: Option<String>,
5455
pub mutation_type: Option<String>,
@@ -62,7 +63,7 @@ impl Schema {
6263
inputs: BTreeMap::new(),
6364
interfaces: BTreeMap::new(),
6465
objects: BTreeMap::new(),
65-
scalars: BTreeSet::new(),
66+
scalars: BTreeMap::new(),
6667
unions: BTreeMap::new(),
6768
query_type: None,
6869
mutation_type: None,
@@ -179,13 +180,6 @@ impl Schema {
179180
.or_else(|| context._subscription_root.as_ref())
180181
.expect("no selection defined");
181182

182-
// TODO: do something smarter here
183-
let scalar_definitions = context.schema.scalars.iter().map(|scalar_name| {
184-
use proc_macro2::{Ident, Span};
185-
let ident = Ident::new(scalar_name, Span::call_site());
186-
quote!(type #ident = String;)
187-
});
188-
189183
let input_object_definitions: Result<Vec<TokenStream>, _> = context
190184
.schema
191185
.inputs
@@ -194,6 +188,13 @@ impl Schema {
194188
.collect();
195189
let input_object_definitions = input_object_definitions?;
196190

191+
let scalar_definitions: Vec<TokenStream> = context
192+
.schema
193+
.scalars
194+
.values()
195+
.map(|s| s.to_rust())
196+
.collect();
197+
197198
Ok(quote! {
198199
type Boolean = bool;
199200
type Float = f64;
@@ -273,7 +274,13 @@ impl ::std::convert::From<graphql_parser::schema::Document> for Schema {
273274
);
274275
}
275276
schema::TypeDefinition::Scalar(scalar) => {
276-
schema.scalars.insert(scalar.name);
277+
schema.scalars.insert(
278+
scalar.name.clone(),
279+
Scalar {
280+
name: scalar.name,
281+
description: scalar.description,
282+
},
283+
);
277284
}
278285
schema::TypeDefinition::Union(union) => {
279286
let variants: BTreeSet<String> = union.types.into_iter().collect();
@@ -369,7 +376,13 @@ impl ::std::convert::From<::introspection_response::IntrospectionResponse> for S
369376
.find(|s| s == &&name.as_str())
370377
.is_none()
371378
{
372-
schema.scalars.insert(name);
379+
schema.scalars.insert(
380+
name.clone(),
381+
Scalar {
382+
name,
383+
description: ty.description.as_ref().map(|d| d.clone()),
384+
},
385+
);
373386
}
374387
}
375388
Some(__TypeKind::UNION) => {

0 commit comments

Comments
 (0)