Skip to content
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

As using proxy type #1490

Open
carlocorradini opened this issue Mar 13, 2024 · 0 comments
Open

As using proxy type #1490

carlocorradini opened this issue Mar 13, 2024 · 0 comments
Labels
enhancement New feature or request

Comments

@carlocorradini
Copy link

carlocorradini commented Mar 13, 2024

Description of the feature

I would like to include a feature similar to serde_with::FromInto:

Serialize value by converting to/from a proxy type with serde support.

This adapter serializes a type O by converting it into a second type T and serializing T. Deserializing works analogue, by deserializing a > T and then converting into O.

struct S {
    #[serde_as(as = "FromInto<T>")]
    #[graphql(as = "T")]
    value: O,
}

The latter eliminates the requirement for wrapper types and/or additional logic because it uses something that GraphQL already understands (i.e. u8, u16, String, ...).

Additionally, it can be used in conjunction with skip_input to eliminate it from an InputObject.

Code example

#[serde_as]
#[derive(Serialize, Deserialize, SimpleObject, InputObject, Copy, Clone, Debug, PartialEq, Eq, Hash)]
pub struct MyObject {
    #[serde_as(as = "FromInto<u8>")]
    #[graphql(as = "u8")]
    pub type: external::enum::Type, // Assume u8 via impl From<u8> and impl From<external::enum::Type>
    #[serde_as(as = "FromInto<u16>")]
    #[serde_as(skip_input, as = "u16")]
    pub only_object: external::Obj, // Assume u16 (only Object) via impl From<external::Obj> for u16
    pub name: String,
}
  • type is external::enum::Type, however it is seen as an u8 in both SimpleObject and InputObject thanks to impl From<u8> for external::enum::Type and impl From<external::enum::Type> for u8
  • only_object is external::Obj, however it is seen as an u16 in SimpleObject thanks to impl From<external::enum::Type> for u16
  • only_object is skipped in InputObject thanks to skip_input
query {
  myObj {
    type, # 100
    onlyObject, # 65535
    name # "Hello World!"
  }
}
@carlocorradini carlocorradini added the enhancement New feature or request label Mar 13, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant