Skip to content

Don't serialize Option::None for Inputs #262

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 7 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
20 changes: 10 additions & 10 deletions examples/github/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,16 @@ edition = "2018"
[dependencies]
failure = "*"
graphql_client = { path = "../../graphql_client" }
serde = "1.0"
serde_derive = "1.0"
serde_json = "1.0"
reqwest = "^0.9.0"
prettytable-rs = "0.7.0"
structopt = "0.2.10"
dotenv = "0.13.0"
envy = "0.3.2"
log = "0.4.3"
env_logger = "0.5.10"
serde = "^1.0"
serde_derive = "^1.0"
serde_json = "^1.0"
reqwest = "^0.9"
prettytable-rs = "^0.7"
structopt = "^0.2"
dotenv = "^0.13"
envy = "^0.3"
log = "^0.4"
env_logger = "^0.5"

[workspace]
members = ["."]
28 changes: 14 additions & 14 deletions graphql_client/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,26 +10,26 @@ categories = ["network-programming", "web-programming", "wasm"]
edition = "2018"

[dependencies]
failure = "0.1"
failure = "^0.1"
graphql_query_derive = { path = "../graphql_query_derive", version = "0.8.0" }
serde = { version = "^1.0.78", features = ["derive"] }
serde_json = "1.0"
doc-comment = "0.3.1"
serde = { version = "^1.0", features = ["derive"] }
serde_json = "^1.0"
doc-comment = "^0.3"

[dependencies.futures]
version = "0.1"
version = "^0.1"
optional = true

[dependencies.js-sys]
version = "0.3.5"
version = "^0.3"
optional = true

[dependencies.log]
version = "0.4.6"
version = "^0.4"
optional = true

[dependencies.web-sys]
version = "0.3.2"
version = "^0.3"
optional = true
features = [
"Headers",
Expand All @@ -40,18 +40,18 @@ features = [
]

[dependencies.wasm-bindgen]
version = "0.2.43"
version = "^0.2"
optional = true

[dependencies.wasm-bindgen-futures]
version = "0.3.2"
version = "^0.3"
optional = true

[target.'cfg(not(target_arch = "wasm32"))'.dev-dependencies.reqwest]
version = "0.9.16"
[target.'cfg(not(target_arch = "wasm32"))'.dev-dependencies]
reqwest = "^0.9"

[dev-dependencies.wasm-bindgen-test]
version = "0.2.43"
[dev-dependencies]
wasm-bindgen-test = "^0.2"

[features]
web = [
Expand Down
12 changes: 6 additions & 6 deletions graphql_client/tests/input_object_variables.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,14 +58,14 @@ fn recursive_input_objects_can_be_constructed() {

RecursiveInput {
head: "hello".to_string(),
tail: Box::new(None),
tail: None,
};

RecursiveInput {
head: "hi".to_string(),
tail: Box::new(Some(RecursiveInput {
tail: Some(Box::new(RecursiveInput {
head: "this is crazy".to_string(),
tail: Box::new(None),
tail: None,
})),
};
}
Expand All @@ -84,14 +84,14 @@ fn indirectly_recursive_input_objects_can_be_constructed() {

IndirectlyRecursiveInput {
head: "hello".to_string(),
tail: Box::new(None),
tail: None,
};

IndirectlyRecursiveInput {
head: "hi".to_string(),
tail: Box::new(Some(IndirectlyRecursiveInputTailPart {
tail: Some(Box::new(IndirectlyRecursiveInputTailPart {
name: "this is crazy".to_string(),
recursed_field: Box::new(None),
recursed_field: None,
})),
};
}
18 changes: 9 additions & 9 deletions graphql_client_cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,18 @@ name = "graphql-client"
path = "src/main.rs"

[dependencies]
failure = "0.1"
reqwest = "^0.9.0"
failure = "^0.1"
reqwest = "^0.9"
graphql_client = { version = "0.8.0", path = "../graphql_client" }
graphql_client_codegen = { path = "../graphql_client_codegen/", version = "0.8.0" }
structopt = "0.2"
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
syn = "0.15"
log = "0.4.0"
env_logger = "0.6.0"
structopt = "0.2.18"
serde = { version = "^1.0", features = ["derive"] }
serde_json = "^1.0"
syn = "^1.0"
log = "^0.4"
env_logger = "^0.6"

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

[features]
default = []
Expand Down
1 change: 0 additions & 1 deletion graphql_client_cli/src/generate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,6 @@ fn format(codes: &str) -> String {
#[cfg(feature = "rustfmt")]
{
use rustfmt::{Config, Input, Session};
use std::default::Default;

let mut config = Config::default();

Expand Down
2 changes: 1 addition & 1 deletion graphql_client_cli/src/introspect_schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ pub fn introspect_schema(
) -> Result<(), failure::Error> {
use std::io::Write;

let out: Box<Write> = match output {
let out: Box<dyn Write> = match output {
Some(path) => Box::new(::std::fs::File::create(path)?),
None => Box::new(::std::io::stdout()),
};
Expand Down
4 changes: 2 additions & 2 deletions graphql_client_cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ enum Cli {
/// Path to the GraphQL query file.
query_path: PathBuf,
/// Name of target query. If you don't set this parameter, cli generate all queries in query file.
#[structopt(short = "o", long = "selected-operation")]
#[structopt(long = "selected-operation")]
selected_operation: Option<String>,
/// Additional derives that will be added to the generated structs and enums for the response and the variables.
/// --additional-derives='Serialize,PartialEq'
Expand All @@ -59,7 +59,7 @@ enum Cli {
///
/// If this option is omitted, the code will be generated next to the .graphql
/// file, with the same name and the .rs extension.
#[structopt(short = "out", long = "output-directory")]
#[structopt(short = "o", long = "output-directory")]
output_directory: Option<PathBuf>,
},
}
Expand Down
19 changes: 10 additions & 9 deletions graphql_client_codegen/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,13 @@ repository = "https://github.com/graphql-rust/graphql-client"
edition = "2018"

[dependencies]
failure = "0.1"
lazy_static = "1.0"
quote = "0.6"
syn = "0.15.20"
proc-macro2 = { version = "0.4", features = [] }
serde = { version = "^1.0.78", features = ["derive"] }
serde_json = "1.0"
heck = "0.3"
graphql-parser = "0.2.2"
failure = "^0.1"
lazy_static = "^1.3"
quote = "^1.0"
syn = "^1.0"
proc-macro2 = { version = "^1.0", features = [] }
serde = { version = "^1.0", features = ["derive"] }
serde_json = "^1.0"
heck = "^0.3"
graphql-parser = "^0.2"
derivative = "1.0.2"
5 changes: 4 additions & 1 deletion graphql_client_codegen/src/codegen_options.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::deprecation::DeprecationStrategy;
use derivative::*;
use proc_macro2::Ident;
use std::path::{Path, PathBuf};
use syn::Visibility;
Expand All @@ -13,7 +14,8 @@ pub enum CodegenMode {
}

/// Used to configure code generation.
#[derive(Debug)]
#[derive(Derivative)]
#[derivative(Debug)]
pub struct GraphQLClientCodegenOptions {
/// Which context is this code generation effort taking place.
pub mode: CodegenMode,
Expand All @@ -28,6 +30,7 @@ pub struct GraphQLClientCodegenOptions {
/// The deprecation strategy to adopt.
deprecation_strategy: Option<DeprecationStrategy>,
/// Target module visibility.
#[derivative(Debug = "ignore")]
module_visibility: Option<Visibility>,
/// A path to a file to include in the module to force Cargo to take into account changes in
/// the query files when recompiling.
Expand Down
13 changes: 11 additions & 2 deletions graphql_client_codegen/src/enums.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,13 @@ pub struct GqlEnum<'schema> {
}

impl<'schema> GqlEnum<'schema> {
/**
* About rust keyword escaping: variant_names and constructors must be escaped,
* variant_str not.
* Example schema: enum AnEnum { where \n self }
* Generated "variant_names" enum: pub enum AnEnum { where_, self_, Other(String), }
* Generated serialize line: "AnEnum::where_ => "where","
*/
pub(crate) fn to_rust(
&self,
query_context: &crate::query::QueryContext<'_, '_>,
Expand All @@ -28,7 +35,8 @@ impl<'schema> GqlEnum<'schema> {
.variants
.iter()
.map(|v| {
let name = Ident::new(&v.name, Span::call_site());
let rust_safe_field_name = crate::shared::keyword_replace(&v.name);
let name = Ident::new(&rust_safe_field_name, Span::call_site());
let description = &v.description;
let description = description.as_ref().map(|d| quote!(#[doc = #d]));
quote!(#description #name)
Expand All @@ -40,7 +48,8 @@ impl<'schema> GqlEnum<'schema> {
.variants
.iter()
.map(|v| {
let v = Ident::new(&v.name, Span::call_site());
let rust_safe_field_name = crate::shared::keyword_replace(&v.name);
let v = Ident::new(&rust_safe_field_name, Span::call_site());
quote!(#name_ident::#v)
})
.collect();
Expand Down
3 changes: 2 additions & 1 deletion graphql_client_codegen/src/field_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,9 @@ impl<'a> FieldType<'a> {
}
prefix.to_string()
};
let full_name = Ident::new(&full_name, Span::call_site());

let rust_safe_field_name = crate::shared::keyword_replace(&full_name);
let full_name = Ident::new(&rust_safe_field_name, Span::call_site());
quote!(#full_name)
}
FieldType::Optional(inner) => {
Expand Down
13 changes: 11 additions & 2 deletions graphql_client_codegen/src/generated_module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ impl<'a> GeneratedModule<'a> {
let module_name = Ident::new(&self.operation.name.to_snake_case(), Span::call_site());
let module_visibility = &self.options.module_visibility();
let operation_name_literal = &self.operation.name;
let operation_name_ident = Ident::new(&self.operation.name, Span::call_site());
let operation_name_ident =
Ident::new(&self.operation.name.to_camel_case(), Span::call_site());

// Force cargo to refresh the generated code when the query file changes.
let query_include = self
Expand All @@ -51,7 +52,15 @@ impl<'a> GeneratedModule<'a> {
CodegenMode::Derive => quote!(),
};

let variables_type = match self.operation.variables.len() {
0 => quote!(()),
_ => quote!(
#module_name::Variables
),
};

Ok(quote!(
#[allow(dead_code)]
#struct_declaration

#module_visibility mod #module_name {
Expand All @@ -66,7 +75,7 @@ impl<'a> GeneratedModule<'a> {
}

impl graphql_client::GraphQLQuery for #operation_name_ident {
type Variables = #module_name::Variables;
type Variables = #variables_type;
type ResponseData = #module_name::ResponseData;

fn build_query(variables: Self::Variables) -> ::graphql_client::QueryBody<Self::Variables> {
Expand Down
Loading