forked from graphql-rust/graphql-client
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
codegen: enable ids to be deserialized from strings or integers
Although not strictly to spec, some upstream graphql implementations such as the start.gg API encode their ID types as integers rather than strings. Prior to this commit, deserialization would fail in such cases, since graphql_client simply type aliases the ID type to a String. This commit introduces a simple deserialization helper that enables us to handle both integers and strings while maintaining backward compatbility for downstream users. Fixes: graphql-rust#455 Signed-off-by: William Findlay <[email protected]>
- Loading branch information
1 parent
a7283cb
commit fdc983c
Showing
3 changed files
with
38 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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), | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters