Skip to content

Commit 6327fae

Browse files
authored
Merge branch 'master' into add-prettier
2 parents 9ec9da1 + a0b2f1e commit 6327fae

File tree

13 files changed

+59
-31
lines changed

13 files changed

+59
-31
lines changed

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ before_script:
1111
- if [ "$TRAVIS_RUST_VERSION" = "stable" ]; then (rustup component add rustfmt-preview clippy-preview) fi
1212
script:
1313
- if [ "$TRAVIS_RUST_VERSION" = "stable" ]; then (cargo fmt --all -- --check) fi
14-
- if [ "$TRAVIS_RUST_VERSION" = "stable" ]; then (cargo clippy) fi
14+
- if [ "$TRAVIS_RUST_VERSION" = "stable" ]; then (cargo clippy -- -D warnings) fi
1515
- prettier --debug-check -l './**/*.json' './**/*.graphql'
1616
- cargo test --all
1717
- cargo build --manifest-path=./graphql_client/examples/github/Cargo.toml

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
1515

1616
- (BREAKING) GraphQLQuery does not take a lifetime parameter anymore. This makes it easier to work with futures in async client, since futures expect everything they capture to have the 'static lifetime.
1717

18+
### Fixed
19+
20+
- When using edition 2018, you no longer need to add `#[macro_use] extern crate serde_derive` to your crate for the generated modules to compile (thanks @aergonaut!)
21+
1822
## 0.5.1 (2018-10-07)
1923

2024
### Added

graphql_client/examples/github/src/main.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ extern crate dotenv;
22
extern crate envy;
33
#[macro_use]
44
extern crate failure;
5-
#[macro_use]
65
extern crate graphql_client;
76
#[macro_use]
87
extern crate log;
@@ -12,7 +11,6 @@ extern crate serde;
1211
extern crate serde_json;
1312
#[macro_use]
1413
extern crate serde_derive;
15-
#[macro_use]
1614
extern crate structopt;
1715
#[macro_use]
1816
extern crate prettytable;
@@ -42,7 +40,7 @@ struct Env {
4240
}
4341

4442
fn parse_repo_name(repo_name: &str) -> Result<(&str, &str), failure::Error> {
45-
let mut parts = repo_name.split("/");
43+
let mut parts = repo_name.split('/');
4644
match (parts.next(), parts.next()) {
4745
(Some(owner), Some(name)) => Ok((owner, name)),
4846
_ => Err(format_err!("wrong format for the repository name param (we expect something like facebook/graphql)"))

graphql_client_cli/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ structopt = "0.2"
1818
serde = "1.0"
1919
serde_derive = "1.0"
2020
serde_json = "1.0"
21+
syn = "0.15"
2122

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

graphql_client_cli/src/generate.rs

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,18 @@ use graphql_client_codegen::*;
33
use std::fs::File;
44
use std::io::Write as IoWrite;
55
use std::path::PathBuf;
6+
use syn;
67

8+
#[allow(too_many_arguments)]
79
pub fn generate_code(
810
query_path: PathBuf,
911
schema_path: PathBuf,
1012
selected_operation: String,
1113
additional_derives: Option<String>,
12-
deprecation_strategy: Option<String>,
14+
deprecation_strategy: &Option<String>,
1315
no_formatting: bool,
14-
output: PathBuf,
16+
module_visibility: &Option<String>,
17+
output: &PathBuf,
1518
) -> Result<(), failure::Error> {
1619
let deprecation_strategy = deprecation_strategy.as_ref().map(|s| s.as_str());
1720
let deprecation_strategy = match deprecation_strategy {
@@ -21,10 +24,22 @@ pub fn generate_code(
2124
_ => None,
2225
};
2326

27+
let module_visibility = module_visibility.as_ref().map(|s| s.as_str());
28+
let module_visibility = match module_visibility {
29+
Some("pub") => syn::VisPublic {
30+
pub_token: <Token![pub]>::default(),
31+
}.into(),
32+
Some("private") => syn::Visibility::Inherited {},
33+
_ => syn::VisPublic {
34+
pub_token: <Token![pub]>::default(),
35+
}.into(),
36+
};
37+
2438
let options = GraphQLClientDeriveOptions {
2539
struct_name: selected_operation,
2640
additional_derives,
2741
deprecation_strategy,
42+
module_visibility,
2843
};
2944

3045
let gen = generate_module_token_stream(query_path, schema_path, Some(options))?;

graphql_client_cli/src/introspect_schema.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,8 @@ pub fn introspect_schema(
4949
}
5050

5151
let json: serde_json::Value = res.json()?;
52-
Ok(serde_json::to_writer_pretty(out, &json)?)
52+
serde_json::to_writer_pretty(out, &json)?;
53+
Ok(())
5354
}
5455

5556
fn construct_headers() -> HeaderMap {

graphql_client_cli/src/main.rs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
extern crate failure;
22
extern crate reqwest;
3-
4-
#[macro_use]
53
extern crate structopt;
64
#[macro_use]
75
extern crate graphql_client;
@@ -10,6 +8,8 @@ extern crate graphql_client_codegen;
108
extern crate serde_derive;
119
extern crate serde;
1210
extern crate serde_json;
11+
#[macro_use]
12+
extern crate syn;
1313

1414
#[cfg(feature = "rustfmt")]
1515
extern crate rustfmt_nightly as rustfmt;
@@ -57,6 +57,10 @@ enum Cli {
5757
/// Formating feature is disabled as default installation.
5858
#[structopt(long = "no-formatting")]
5959
no_formatting: bool,
60+
/// You can choose module and target struct visibility from pub and private.
61+
/// Default value is pub.
62+
#[structopt(short = "m", long = "module_visibility")]
63+
module_visibility: Option<String>,
6064
#[structopt(parse(from_os_str))]
6165
output: PathBuf,
6266
},
@@ -77,15 +81,17 @@ fn main() -> Result<(), failure::Error> {
7781
additional_derives,
7882
deprecation_strategy,
7983
no_formatting,
84+
module_visibility,
8085
output,
8186
} => generate::generate_code(
8287
query_path,
8388
schema_path,
8489
selected_operation,
8590
additional_derives,
86-
deprecation_strategy,
91+
&deprecation_strategy,
8792
no_formatting,
88-
output,
93+
&module_visibility,
94+
&output,
8995
),
9096
}
9197
}

graphql_client_codegen/src/codegen.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,9 @@ use selection::Selection;
1212
pub(crate) fn select_operation(query: &query::Document, struct_name: &str) -> Option<Operation> {
1313
let mut operations: Vec<Operation> = Vec::new();
1414

15-
for definition in query.definitions.iter() {
16-
match definition {
17-
query::Definition::Operation(op) => {
18-
operations.push(op.into());
19-
}
20-
_ => (),
15+
for definition in &query.definitions {
16+
if let query::Definition::Operation(op) = definition {
17+
operations.push(op.into());
2118
}
2219
}
2320

@@ -145,6 +142,8 @@ pub fn response_for_query(
145142
let response_derives = context.response_derives();
146143

147144
Ok(quote! {
145+
use serde_derive::*;
146+
148147
#[allow(dead_code)]
149148
type Boolean = bool;
150149
#[allow(dead_code)]

graphql_client_codegen/src/lib.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ extern crate syn;
2323
extern crate quote;
2424

2525
use proc_macro2::TokenStream;
26+
use syn::Visibility;
2627

2728
/// Derive-related code. This will be moved into graphql_query_derive.
2829
pub mod attributes;
@@ -71,6 +72,8 @@ pub struct GraphQLClientDeriveOptions {
7172
pub additional_derives: Option<String>,
7273
/// The deprecation strategy to adopt.
7374
pub deprecation_strategy: Option<deprecation::DeprecationStrategy>,
75+
/// target struct visibility.
76+
pub module_visibility: Visibility,
7477
}
7578

7679
/// Generates the code for a Rust module given a query, a schema and options.
@@ -81,12 +84,11 @@ pub fn generate_module_token_stream(
8184
) -> Result<TokenStream, failure::Error> {
8285
let options = options.unwrap();
8386

87+
let module_visibility = options.module_visibility;
8488
let response_derives = options.additional_derives;
8589

8690
// The user can determine what to do about deprecations.
87-
let deprecation_strategy = options
88-
.deprecation_strategy
89-
.unwrap_or(deprecation::DeprecationStrategy::default());
91+
let deprecation_strategy = options.deprecation_strategy.unwrap_or_default();
9092

9193
// We need to qualify the query with the path to the crate it is part of
9294
let (query_string, query) = {
@@ -157,7 +159,7 @@ pub fn generate_module_token_stream(
157159
)?;
158160

159161
let result = quote!(
160-
pub mod #module_name {
162+
#module_visibility mod #module_name {
161163
#![allow(non_camel_case_types)]
162164
#![allow(non_snake_case)]
163165
#![allow(dead_code)]

graphql_client_codegen/src/query.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,9 @@ impl QueryContext {
3131
}
3232

3333
pub(crate) fn require(&self, typename_: &str) {
34-
self.fragments
35-
.get(typename_)
36-
.map(|fragment| fragment.is_required.set(true));
34+
if let Some(fragment) = self.fragments.get(typename_) {
35+
fragment.is_required.set(true)
36+
}
3737
}
3838

3939
/// For testing only. creates an empty QueryContext with an empty Schema.
@@ -121,7 +121,7 @@ impl QueryContext {
121121
&& !derive.to_string().contains("Deserialize")
122122
}).collect();
123123

124-
if enum_derives.len() > 0 {
124+
if !enum_derives.is_empty() {
125125
quote! {
126126
#[derive( #(#enum_derives),* )]
127127
}

0 commit comments

Comments
 (0)