Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions graphql_query_derive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ mod schema;
mod selection;
mod shared;
mod unions;
mod variables;

#[cfg(test)]
mod tests;
Expand Down
38 changes: 33 additions & 5 deletions graphql_query_derive/src/query.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
use failure;
use field_type::FieldType;
use fragments::GqlFragment;
use proc_macro2::TokenStream;
use graphql_parser;
use proc_macro2::{Ident, Span, TokenStream};
use schema::Schema;
use selection::Selection;
use std::collections::BTreeMap;
use variables::Variable;

pub struct QueryContext {
pub _subscription_root: Option<Vec<TokenStream>>,
pub fragments: BTreeMap<String, GqlFragment>,
pub mutation_root: Option<Vec<TokenStream>>,
pub query_root: Option<Vec<TokenStream>>,
pub schema: Schema,
pub variables: BTreeMap<String, FieldType>,
pub variables: Vec<Variable>,
}

impl QueryContext {
Expand All @@ -23,7 +24,34 @@ impl QueryContext {
mutation_root: None,
query_root: None,
schema,
variables: BTreeMap::new(),
variables: Vec::new(),
}
}

pub fn register_variables(&mut self, variables: &[graphql_parser::query::VariableDefinition]) {
variables.iter().for_each(|variable| {
self.variables.push(variable.clone().into());
});
}

pub fn expand_variables(&self) -> TokenStream {
if self.variables.is_empty() {
return quote!(#[derive(Serialize)]
pub struct Variables;);
}

let fields = self.variables.iter().map(|variable| {
let name = &variable.name;
let ty = variable.ty.to_rust(self, name);
let name = Ident::new(name, Span::call_site());
quote!(pub #name: #ty)
});

quote! {
#[derive(Serialize)]
pub struct Variables {
#(#fields,)*
}
}
}

Expand All @@ -36,7 +64,7 @@ impl QueryContext {
mutation_root: None,
query_root: None,
schema: Schema::new(),
variables: BTreeMap::new(),
variables: Vec::new(),
}
}

Expand Down
9 changes: 7 additions & 2 deletions graphql_query_derive/src/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ impl Schema {
definition.response_fields_for_selection(&context, &selection, &prefix),
)
};

context.register_variables(&q.variable_definitions);
}
query::Definition::Operation(query::OperationDefinition::Mutation(q)) => {
context.mutation_root = {
Expand All @@ -87,6 +89,8 @@ impl Schema {
definition.response_fields_for_selection(&context, &selection, &prefix),
)
};

context.register_variables(&q.variable_definitions);
}
query::Definition::Operation(query::OperationDefinition::Subscription(q)) => {
context._subscription_root = {
Expand All @@ -109,6 +113,8 @@ impl Schema {
definition.response_fields_for_selection(&context, &selection, &prefix),
)
};

context.register_variables(&q.variable_definitions);
}
query::Definition::Operation(query::OperationDefinition::SelectionSet(_)) => {
unimplemented!()
Expand All @@ -132,8 +138,7 @@ impl Schema {
.fragments
.values()
.map(|fragment| fragment.to_rust(&context));
let variables_struct = quote!(#[derive(Serialize)]
pub struct Variables;);
let variables_struct = context.expand_variables();
let response_data_fields = context
.query_root
.as_ref()
Expand Down
19 changes: 19 additions & 0 deletions graphql_query_derive/src/variables.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
use field_type::FieldType;
use graphql_parser;

#[derive(Debug)]
pub struct Variable {
pub name: String,
pub ty: FieldType,
pub default: Option<graphql_parser::query::Value>,
}

impl ::std::convert::From<graphql_parser::query::VariableDefinition> for Variable {
fn from(def: graphql_parser::query::VariableDefinition) -> Variable {
Variable {
name: def.name,
ty: FieldType::from(def.var_type),
default: def.default_value,
}
}
}
1 change: 1 addition & 0 deletions tests/introspection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ extern crate serde;
query_path = "tests/introspection_query.graphql",
schema_path = "tests/introspection_schema.graphql"
)]
#[allow(dead_code)]
struct IntrospectionQuery;

#[test]
Expand Down
21 changes: 21 additions & 0 deletions tests/scalar_variables.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#[macro_use]
extern crate graphql_client;
#[macro_use]
extern crate serde_derive;
extern crate serde;

#[derive(GraphQLQuery)]
#[GraphQLQuery(
query_path = "tests/scalar_variables_query.graphql",
schema_path = "tests/scalar_variables_schema.graphql"
)]
#[allow(dead_code)]
struct ScalarVariablesQuery;

#[test]
fn scalar_variables_query_variables_struct() {
scalar_variables_query::Variables {
msg: "hello".to_string(),
reps: Some(32),
};
}
5 changes: 5 additions & 0 deletions tests/scalar_variables_query.graphql
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
query VariablesQuery($msg: String!, $reps: Int) {
echo(message: $msg, repetitions: $reps) {
result
}
}
12 changes: 12 additions & 0 deletions tests/scalar_variables_schema.graphql
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@

schema {
query: ScalarVariablesQuery
}

type ScalarVariablesQuery {
echo(message: String!, repetitions: Int): EchoResult
}

type EchoResult {
result: String!
}