Skip to content

Add env_logger to cli #193

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
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
2 changes: 1 addition & 1 deletion graphql_client/examples/github/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ type URI = String;
#[graphql(
schema_path = "src/schema.graphql",
query_path = "src/query_1.graphql",
response_derives = "Debug",
response_derives = "Debug"
)]
struct RepoView;

Expand Down
6 changes: 3 additions & 3 deletions graphql_client/tests/deprecation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,23 @@ extern crate serde_derive;
#[graphql(
schema_path = "tests/deprecation/schema.graphql",
query_path = "tests/deprecation/query.graphql",
deprecated = "allow",
deprecated = "allow"
)]
pub struct AllowDeprecation;

#[derive(GraphQLQuery)]
#[graphql(
schema_path = "tests/deprecation/schema.graphql",
query_path = "tests/deprecation/query.graphql",
deprecated = "deny",
deprecated = "deny"
)]
pub struct DenyDeprecation;

#[derive(GraphQLQuery)]
#[graphql(
schema_path = "tests/deprecation/schema.graphql",
query_path = "tests/deprecation/query.graphql",
deprecated = "warn",
deprecated = "warn"
)]
pub struct WarnDeprecation;

Expand Down
4 changes: 2 additions & 2 deletions graphql_client/tests/input_object_variables.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ extern crate serde_json;
#[graphql(
query_path = "tests/input_object_variables/input_object_variables_query.graphql",
schema_path = "tests/input_object_variables/input_object_variables_schema.graphql",
response_derives = "Debug",
response_derives = "Debug"
)]
pub struct InputObjectVariablesQuery;

Expand All @@ -34,7 +34,7 @@ type Email = String;
#[graphql(
query_path = "tests/input_object_variables/input_object_variables_query_defaults.graphql",
schema_path = "tests/input_object_variables/input_object_variables_schema.graphql",
response_derives = "Debug",
response_derives = "Debug"
)]
pub struct DefaultInputObjectVariablesQuery;

Expand Down
2 changes: 1 addition & 1 deletion graphql_client/tests/more_derives.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ extern crate serde_derive;
#[graphql(
schema_path = "tests/more_derives/schema.graphql",
query_path = "tests/more_derives/query.graphql",
response_derives = "Debug, PartialEq, PartialOrd",
response_derives = "Debug, PartialEq, PartialOrd"
)]
pub struct MoreDerives;

Expand Down
2 changes: 1 addition & 1 deletion graphql_client/tests/union_query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const RESPONSE: &'static str = include_str!("unions/union_query_response.json");
#[graphql(
query_path = "tests/unions/union_query.graphql",
schema_path = "tests/unions/union_schema.graphql",
response_derives = "PartialEq, Debug",
response_derives = "PartialEq, Debug"
)]
pub struct UnionQuery;

Expand Down
2 changes: 2 additions & 0 deletions graphql_client_cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ serde = "1.0"
serde_derive = "1.0"
serde_json = "1.0"
syn = "0.15"
log = "0.4.0"
env_logger = "0.6.0"

rustfmt-nightly = { version = "0.99" , optional = true }

Expand Down
8 changes: 5 additions & 3 deletions graphql_client_cli/src/generate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use std::io::Write as IoWrite;
use std::path::PathBuf;
use syn;

#[allow(too_many_arguments)]
#[allow(clippy::too_many_arguments)]
pub fn generate_code(
query_path: PathBuf,
schema_path: PathBuf,
Expand All @@ -29,11 +29,13 @@ pub fn generate_code(
let module_visibility = match module_visibility {
Some("pub") => syn::VisPublic {
pub_token: <Token![pub]>::default(),
}.into(),
}
.into(),
Some("private") => syn::Visibility::Inherited {},
_ => syn::VisPublic {
pub_token: <Token![pub]>::default(),
}.into(),
}
.into(),
};

let options = GraphQLClientDeriveOptions {
Expand Down
42 changes: 42 additions & 0 deletions graphql_client_cli/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
extern crate env_logger;
extern crate log;
use env_logger::fmt::{Color, Style, StyledValue};
use log::Level;

extern crate failure;
extern crate reqwest;
extern crate structopt;
Expand Down Expand Up @@ -70,6 +75,8 @@ enum Cli {
}

fn main() -> Result<(), failure::Error> {
set_env_logger();

let cli = Cli::from_args();
match cli {
Cli::IntrospectSchema {
Expand Down Expand Up @@ -100,3 +107,38 @@ fn main() -> Result<(), failure::Error> {
),
}
}

fn set_env_logger() {
use std::io::Write;

env_logger::Builder::from_default_env()
.format(|f, record| {
let mut style = f.style();
let level = colored_level(&mut style, record.level());
let mut style = f.style();
let file = style.set_bold(true).value("file");
let mut style = f.style();
let module = style.set_bold(true).value("module");
writeln!(
f,
"{} {}: {} {}: {}\n{}",
level,
file,
record.file().unwrap(),
module,
record.target(),
record.args()
)
})
.init();
}

fn colored_level<'a>(style: &'a mut Style, level: Level) -> StyledValue<'a, &'static str> {
match level {
Level::Trace => style.set_color(Color::Magenta).value("TRACE"),
Level::Debug => style.set_color(Color::Blue).value("DEBUG"),
Level::Info => style.set_color(Color::Green).value("INFO "),
Level::Warn => style.set_color(Color::Yellow).value("WARN "),
Level::Error => style.set_color(Color::Red).value("ERROR"),
}
}
9 changes: 6 additions & 3 deletions graphql_client_codegen/src/codegen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,8 @@ pub fn response_for_query(
} else {
None
}
}).collect();
})
.collect();
let fragment_definitions = fragment_definitions?;
let variables_struct =
operation.expand_variables(&context, &operation.name, multiple_operation);
Expand All @@ -131,7 +132,8 @@ pub fn response_for_query(
} else {
None
}
}).collect();
})
.collect();
let input_object_definitions = input_object_definitions?;

let scalar_definitions: Vec<TokenStream> = context
Expand All @@ -144,7 +146,8 @@ pub fn response_for_query(
} else {
None
}
}).collect();
})
.collect();

let response_derives = context.response_derives();

Expand Down
6 changes: 4 additions & 2 deletions graphql_client_codegen/src/enums.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ impl GqlEnum {
let description = &v.description;
let description = description.as_ref().map(|d| quote!(#[doc = #d]));
quote!(#description #name)
}).collect();
})
.collect();
let variant_names = &variant_names;
let name_ident = Ident::new(&format!("{}{}", ENUMS_PREFIX, self.name), Span::call_site());
let constructors: Vec<_> = self
Expand All @@ -37,7 +38,8 @@ impl GqlEnum {
.map(|v| {
let v = Ident::new(&v.name, Span::call_site());
quote!(#name_ident::#v)
}).collect();
})
.collect();
let constructors = &constructors;
let variant_str: Vec<&str> = self.variants.iter().map(|v| v.name.as_str()).collect();
let variant_str = &variant_str;
Expand Down
2 changes: 1 addition & 1 deletion graphql_client_codegen/src/fragments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ impl GqlFragment {
);
};

Ok(quote!{
Ok(quote! {
#derives
pub struct #name_ident {
#(#fields,)*
Expand Down
12 changes: 8 additions & 4 deletions graphql_client_codegen/src/inputs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,8 @@ impl ::std::convert::From<graphql_parser::schema::InputObjectType> for GqlInput
deprecation: DeprecationStatus::Current,
};
(name, field)
}).collect(),
})
.collect(),
is_required: false.into(),
}
}
Expand Down Expand Up @@ -101,7 +102,8 @@ impl ::std::convert::From<introspection_response::FullType> for GqlInput {
deprecation: DeprecationStatus::Current,
};
(name, field)
}).collect(),
})
.collect(),
is_required: false.into(),
}
}
Expand Down Expand Up @@ -148,7 +150,8 @@ mod tests {
deprecation: DeprecationStatus::Current,
},
),
].into_iter()
]
.into_iter()
.collect(),
is_required: false.into(),
};
Expand All @@ -161,7 +164,8 @@ mod tests {
"pub paws_count : Float , ",
"pub requirements : Option < CatRequirements > , ",
"}",
].into_iter()
]
.into_iter()
.collect();

let mut context = QueryContext::new_empty();
Expand Down
6 changes: 4 additions & 2 deletions graphql_client_codegen/src/interfaces.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ impl GqlInterface {
fragment.on == self.name
}
SelectionItem::InlineFragment(_) => false,
}).map(|a| (*a).clone())
})
.map(|a| (*a).clone())
.collect(),
)
}
Expand All @@ -72,7 +73,8 @@ impl GqlInterface {
fragment.on != self.name
}
SelectionItem::Field(SelectionField { name, .. }) => name == "__typename",
}).map(|a| (*a).clone())
})
.map(|a| (*a).clone())
.collect(),
)
}
Expand Down
11 changes: 5 additions & 6 deletions graphql_client_codegen/src/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,8 @@ impl QueryContext {
.filter(|derive| {
!derive.to_string().contains("erialize")
&& !derive.to_string().contains("Deserialize")
}).collect();
})
.collect();

if !enum_derives.is_empty() {
quote! {
Expand Down Expand Up @@ -173,11 +174,9 @@ mod tests {
fn response_derives_fails_when_called_twice() {
let mut context = QueryContext::new_empty();

assert!(
context
.ingest_additional_derives("PartialEq, PartialOrd")
.is_ok()
);
assert!(context
.ingest_additional_derives("PartialEq, PartialOrd")
.is_ok());
assert!(context.ingest_additional_derives("Serialize").is_err());
}
}
15 changes: 10 additions & 5 deletions graphql_client_codegen/src/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@ impl Schema {
.ok_or_else(|| format_err!("interface not found: {}", iface_name))?;
iface.implemented_by = implementors.into_iter().collect();
Ok(())
}).collect()
})
.collect()
}

pub(crate) fn require(&self, typename_: &str) {
Expand All @@ -66,12 +67,14 @@ impl Schema {
self.enums
.get(typename_)
.map(|enm| enm.is_required.set(true))
}).or_else(|| self.inputs.get(typename_).map(|input| input.require(self)))
})
.or_else(|| self.inputs.get(typename_).map(|input| input.require(self)))
.or_else(|| {
self.objects
.get(typename_)
.map(|object| object.require(self))
}).or_else(|| {
})
.or_else(|| {
self.scalars
.get(typename_)
.map(|scalar| scalar.is_required.set(true))
Expand Down Expand Up @@ -115,7 +118,8 @@ impl ::std::convert::From<graphql_parser::schema::Document> for Schema {
.map(|v| EnumVariant {
description: v.description.clone(),
name: v.name.clone(),
}).collect(),
})
.collect(),
is_required: false.into(),
},
);
Expand Down Expand Up @@ -215,7 +219,8 @@ impl ::std::convert::From<::introspection_response::IntrospectionResponse> for S
description: t.description,
name: t.name.expect("enum variant name"),
})
}).filter_map(|t| t)
})
.filter_map(|t| t)
.collect();
let mut enm = GqlEnum {
name: name.clone(),
Expand Down
3 changes: 2 additions & 1 deletion graphql_client_codegen/src/selection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,8 @@ mod tests {
} else {
None
}
}).next()
})
.next()
.unwrap();

let selection: Selection = selection_set.into();
Expand Down
11 changes: 7 additions & 4 deletions graphql_client_codegen/src/shared.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,8 @@ pub(crate) fn field_impls_for_selection(
} else {
Ok(quote!())
}
}).collect()
})
.collect()
}

pub(crate) fn response_fields_for_selection(
Expand Down Expand Up @@ -137,20 +138,22 @@ pub(crate) fn response_fields_for_selection(
Ident::new(&fragment.fragment_name.to_snake_case(), Span::call_site());
context.require(&fragment.fragment_name);
let type_name = Ident::new(&fragment.fragment_name, Span::call_site());
Ok(quote!{
Ok(quote! {
#[serde(flatten)]
pub #field_name: #type_name
})
}
SelectionItem::InlineFragment(_) => Err(format_err!(
"unimplemented: inline fragment on object field"
))?,
}).filter(|x| match x {
})
.filter(|x| match x {
// Remove empty fields so callers always know a field has some
// tokens.
Ok(f) => !f.is_empty(),
Err(_) => true,
}).collect()
})
.collect()
}

/// Given the GraphQL schema name for an object/interface/input object field and
Expand Down
Loading