Skip to content

Commit 2024a4f

Browse files
committed
Derive definitions for introspected schema (initial draft)
1 parent 3910147 commit 2024a4f

File tree

11 files changed

+380
-2628
lines changed

11 files changed

+380
-2628
lines changed

graphql_query_derive/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,7 @@ failure = "*"
1111
quote = "^0.6"
1212
syn = "*"
1313
proc-macro2 = { version = "*", features = ["nightly"] }
14+
serde = "1.0"
15+
serde_derive = "1.0"
1416
heck = "*"
1517
graphql-parser = "*"

graphql_query_derive/src/enums.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use proc_macro2::{Ident, Span, TokenStream};
22

3-
pub const ENUMS_PREFIX: &'static str = "RustEnum_";
3+
pub const ENUMS_PREFIX: &'static str = "";
44

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

graphql_query_derive/src/field_type.rs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1+
use enums::ENUMS_PREFIX;
12
use graphql_parser::schema;
23
use proc_macro2::{Ident, Span, TokenStream};
34
use query::QueryContext;
45
use schema::DEFAULT_SCALARS;
5-
use enums::ENUMS_PREFIX;
66

77
#[derive(Debug, PartialEq)]
88
pub enum FieldType {
@@ -19,10 +19,18 @@ impl FieldType {
1919
let just_the_prefix = Ident::new(prefix, Span::call_site());
2020
let name_string = name.to_string();
2121

22-
let name = if context.schema.scalars.contains(&name_string) || DEFAULT_SCALARS.iter().find(|elem| elem == &&name_string).is_some() {
22+
let name = if context.schema.scalars.contains(&name_string)
23+
|| DEFAULT_SCALARS
24+
.iter()
25+
.find(|elem| elem == &&name_string)
26+
.is_some()
27+
{
2328
name.clone()
2429
} else if context.schema.enums.contains_key(&name_string) {
25-
Ident::new(&format!("{}{}", ENUMS_PREFIX, &name_string), Span::call_site())
30+
Ident::new(
31+
&format!("{}{}", ENUMS_PREFIX, &name_string),
32+
Span::call_site(),
33+
)
2634
} else {
2735
just_the_prefix
2836
};

graphql_query_derive/src/fragments.rs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1+
use graphql_parser::query::SelectionSet;
12
use proc_macro2::{Ident, Span, TokenStream};
23
use query::QueryContext;
3-
use graphql_parser::query::SelectionSet;
44

55
#[derive(Debug, PartialEq)]
66
pub struct GqlFragment {
@@ -12,10 +12,19 @@ pub struct GqlFragment {
1212
impl GqlFragment {
1313
pub fn to_rust(&self, context: &QueryContext) -> TokenStream {
1414
let name_ident = Ident::new(&self.name, Span::call_site());
15+
let object = context.schema.objects.get(&self.on).expect("oh, noes");
16+
let field_impls = object
17+
.field_impls_for_selection(context, &self.selection, &self.name)
18+
.unwrap();
19+
let fields = object.response_fields_for_selection(context, &self.selection, &self.name);
1520

1621
quote!{
1722
#[derive(Debug, Deserialize)]
18-
pub struct #name_ident;
23+
pub struct #name_ident {
24+
#(#fields,)*
25+
}
26+
27+
#(#field_impls)*
1928
}
2029
}
2130
}

0 commit comments

Comments
 (0)