Skip to content

Commit 77e640e

Browse files
committed
Make the simple star wars query example compile
1 parent d28f9a3 commit 77e640e

File tree

5 files changed

+38
-20
lines changed

5 files changed

+38
-20
lines changed

graphql_query_derive/src/enums.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ impl GqlEnum {
3131
fn serialize<S: serde::Serializer>(&self, ser: S) -> Result<S::Ok, S::Error> {
3232
ser.serialize_str(match *self {
3333
#(#constructors => #variant_str,)*
34-
#name::Other(s) => s.as_str(),
34+
#name::Other(ref s) => s.as_str(),
3535
})
3636
}
3737
}

graphql_query_derive/src/field_type.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use graphql_parser::schema;
22
use proc_macro2::{Ident, Span, TokenStream};
3+
use query::QueryContext;
34

45
#[derive(Debug, PartialEq)]
56
pub enum FieldType {
@@ -9,15 +10,18 @@ pub enum FieldType {
910
}
1011

1112
impl FieldType {
12-
pub fn to_rust(&self) -> TokenStream {
13+
pub fn to_rust(&self, context: &QueryContext, prefix: &str) -> TokenStream {
1314
match &self {
14-
FieldType::Named(name) => quote!(#name),
15+
FieldType::Named(name) => {
16+
let name = Ident::new(&format!("{}{}", prefix, name), Span::call_site());
17+
quote!(#name)
18+
}
1519
FieldType::Optional(inner) => {
16-
let inner = inner.to_rust();
20+
let inner = inner.to_rust(context, prefix);
1721
quote!( Option<#inner>)
1822
}
1923
FieldType::Vector(inner) => {
20-
let inner = inner.to_rust();
24+
let inner = inner.to_rust(context, prefix);
2125
quote!( Vec<#inner>)
2226
}
2327
}

graphql_query_derive/src/objects.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,9 @@ impl GqlObject {
2424
prefix: &str,
2525
) -> TokenStream {
2626
let name = Ident::new(&format!("{}{}", prefix, self.name), Span::call_site());
27-
let fields = self.response_fields_for_selection(query_context, selection);
28-
let field_impls = self.field_impls_for_selection(query_context, selection, prefix);
27+
let fields = self.response_fields_for_selection(query_context, selection, prefix);
28+
let prefix = format!("{}{}", prefix, self.name);
29+
let field_impls = self.field_impls_for_selection(query_context, selection, &prefix);
2930
quote! {
3031
#(#field_impls)*
3132

@@ -42,7 +43,6 @@ impl GqlObject {
4243
selection: &query::SelectionSet,
4344
prefix: &str,
4445
) -> Vec<TokenStream> {
45-
let prefix = format!("{}{}", prefix, self.name);
4646
selection
4747
.items
4848
.iter()
@@ -67,6 +67,7 @@ impl GqlObject {
6767
&self,
6868
query_context: &QueryContext,
6969
selection: &query::SelectionSet,
70+
prefix: &str,
7071
) -> Vec<TokenStream> {
7172
let mut fields = Vec::new();
7273

@@ -81,7 +82,7 @@ impl GqlObject {
8182
.unwrap()
8283
.type_;
8384
let name = Ident::new(name, Span::call_site());
84-
let ty = ty.to_rust();
85+
let ty = ty.to_rust(query_context, prefix);
8586
fields.push(quote!(#name: #ty));
8687
}
8788
query::Selection::FragmentSpread(fragment) => {

graphql_query_derive/src/schema.rs

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -51,13 +51,17 @@ impl Schema {
5151
.clone()
5252
.and_then(|query_type| context.schema.objects.get(&query_type))
5353
.expect("query type is defined");
54+
let prefix = &q.name.expect("unnamed operation");
5455
definitions.extend(definition.field_impls_for_selection(
5556
&context,
5657
&q.selection_set,
57-
"",
58+
prefix,
59+
));
60+
context.query_root = Some(definition.response_fields_for_selection(
61+
&context,
62+
&q.selection_set,
63+
prefix,
5864
));
59-
context.query_root =
60-
Some(definition.response_fields_for_selection(&context, &q.selection_set));
6165
}
6266
query::Definition::Operation(query::OperationDefinition::Mutation(q)) => {
6367
let definition = context
@@ -66,13 +70,18 @@ impl Schema {
6670
.clone()
6771
.and_then(|mutation_type| context.schema.objects.get(&mutation_type))
6872
.expect("mutation type is defined");
73+
let prefix = &q.name.expect("unnamed operation");
74+
6975
definitions.extend(definition.field_impls_for_selection(
7076
&context,
7177
&q.selection_set,
72-
"",
78+
prefix,
79+
));
80+
context.mutation_root = Some(definition.response_fields_for_selection(
81+
&context,
82+
&q.selection_set,
83+
prefix,
7384
));
74-
context.mutation_root =
75-
Some(definition.response_fields_for_selection(&context, &q.selection_set));
7685
}
7786
query::Definition::Operation(query::OperationDefinition::Subscription(q)) => {
7887
let definition = context
@@ -83,13 +92,17 @@ impl Schema {
8392
context.schema.objects.get(&subscription_type)
8493
})
8594
.expect("subscription type is defined");
95+
let prefix = &q.name.expect("unnamed operation");
8696
definitions.extend(definition.field_impls_for_selection(
8797
&context,
8898
&q.selection_set,
89-
"",
99+
&prefix,
100+
));
101+
context._subscription_root = Some(definition.response_fields_for_selection(
102+
&context,
103+
&q.selection_set,
104+
&prefix,
90105
));
91-
context._subscription_root =
92-
Some(definition.response_fields_for_selection(&context, &q.selection_set));
93106
}
94107
query::Definition::Operation(query::OperationDefinition::SelectionSet(_)) => {
95108
unimplemented!()

src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ pub struct GraphQLQueryBody<Variables>
2121
where
2222
Variables: serde::Serialize,
2323
{
24-
variables: Variables,
25-
query: &'static str,
24+
pub variables: Variables,
25+
pub query: &'static str,
2626
}
2727

2828
#[derive(Debug, Serialize, Deserialize)]

0 commit comments

Comments
 (0)