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
2 changes: 2 additions & 0 deletions graphql_client/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ pub use graphql_query_derive::*;
))]
pub mod reqwest;

pub mod serde_with;

use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::fmt::{self, Display, Write};
Expand Down
27 changes: 27 additions & 0 deletions graphql_client/src/serde_with.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
//! Helpers for overriding default serde implementations.

use serde::{Deserialize, Deserializer};

/// Deserialize an optional ID type from either a String or an Integer representation.
///
/// This is used by the codegen to enable String IDs to be deserialized from
/// either Strings or Integers.
pub fn deserialize_option_id<'de, D>(deserializer: D) -> Result<Option<String>, D::Error>
where
D: Deserializer<'de>,
{
#[derive(Deserialize)]
#[serde(untagged)]
enum IntOrString {
Int(i64),
Str(String),
}

let res = Option::<IntOrString>::deserialize(deserializer)?;

Ok(match res {
None => None,
Some(IntOrString::Int(n)) => Some(n.to_string()),
Some(IntOrString::Str(s)) => Some(s),
})
}
41 changes: 41 additions & 0 deletions graphql_client/tests/int_id.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
use graphql_client::*;
use serde_json::json;

#[derive(GraphQLQuery)]
#[graphql(
schema_path = "tests/more_derives/schema.graphql",
query_path = "tests/more_derives/query.graphql",
response_derives = "Debug, PartialEq, Eq, std::cmp::PartialOrd"
)]
pub struct MoreDerives;

#[test]
fn int_id() {
let response1 = json!({
"currentUser": {
"id": 1,
"name": "Don Draper",
}
});

let response2 = json!({
"currentUser": {
"id": "2",
"name": "Peggy Olson",
}
});

let res1 = serde_json::from_value::<more_derives::ResponseData>(response1)
.expect("should deserialize");
assert_eq!(
res1.current_user.expect("res1 current user").id,
Some("1".into())
);

let res2 = serde_json::from_value::<more_derives::ResponseData>(response2)
.expect("should deserialize");
assert_eq!(
res2.current_user.expect("res2 current user").id,
Some("2".into())
);
}
6 changes: 6 additions & 0 deletions graphql_client/tests/int_id/query.graphql
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
query MoreDerives {
currentUser {
name
id
}
}
12 changes: 12 additions & 0 deletions graphql_client/tests/int_id/schema.graphql
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
schema {
query: TestQuery
}

type TestQuery {
currentUser: TestUser
}

type TestUser {
name: String
id: ID
}
9 changes: 9 additions & 0 deletions graphql_client_codegen/src/codegen/selection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -405,6 +405,14 @@ impl<'a> ExpandedField<'a> {
qualified_type
};

let id_deserialize_with = if self.field_type == "ID" {
Some(
quote!(#[serde(deserialize_with = "graphql_client::serde_with::deserialize_option_id")]),
)
} else {
None
};

let optional_skip_serializing_none = if *options.skip_serializing_none()
&& self
.field_type_qualifiers
Expand Down Expand Up @@ -443,6 +451,7 @@ impl<'a> ExpandedField<'a> {
#optional_flatten
#optional_rename
#optional_deprecation_annotation
#id_deserialize_with
pub #ident: #qualified_type
};

Expand Down