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

Default trait for variables #477

Open
danielboros1990 opened this issue Mar 12, 2024 · 1 comment
Open

Default trait for variables #477

danielboros1990 opened this issue Mar 12, 2024 · 1 comment

Comments

@danielboros1990
Copy link

Hi guys,

is it possible to use Default trait instead of writing None for nullable values.

// current solutions
set: StreamsSetInput {
                        id: None,
                        user_id: None,
                        stream_id: None,
                        is_online: None,
                        live_ms: Some(stream.live_ms.unwrap().to_string()),
                        frames: Some(stream.frames.unwrap().to_string()),
                        send_bytes: Some(stream.send_bytes.unwrap().to_string()),
                        recv_bytes: Some(stream.recv_bytes.unwrap().to_string()),
                        video_codec: Some(stream.video.as_ref().unwrap().codec.clone().unwrap()),
                        video_profile: Some(
                            stream.video.as_ref().unwrap().profile.clone().unwrap(),
                        ),
                        video_level: Some(stream.video.as_ref().unwrap().level.clone().unwrap()),
                        video_width: Some(stream.video.as_ref().unwrap().width.unwrap().into()),
                        video_height: Some(stream.video.as_ref().unwrap().height.unwrap().into()),
                        audio_codec: Some(stream.audio.as_ref().unwrap().codec.clone()),
                        audio_sample_rate: Some(stream.audio.as_ref().unwrap().sample_rate.into()),
                        audio_channel: Some(stream.audio.as_ref().unwrap().channel.into()),
                        audio_profile: Some(stream.audio.as_ref().unwrap().profile.clone()),
                        created_at: None,
                        updated_at: None,
                    },

// expected solutions
set: StreamsSetInput {
                        live_ms: Some(stream.live_ms.unwrap().to_string()),
                        frames: Some(stream.frames.unwrap().to_string()),
                        send_bytes: Some(stream.send_bytes.unwrap().to_string()),
                        recv_bytes: Some(stream.recv_bytes.unwrap().to_string()),
                        video_codec: Some(stream.video.as_ref().unwrap().codec.clone().unwrap()),
                        video_profile: Some(
                            stream.video.as_ref().unwrap().profile.clone().unwrap(),
                        ),
                        video_level: Some(stream.video.as_ref().unwrap().level.clone().unwrap()),
                        video_width: Some(stream.video.as_ref().unwrap().width.unwrap().into()),
                        video_height: Some(stream.video.as_ref().unwrap().height.unwrap().into()),
                        audio_codec: Some(stream.audio.as_ref().unwrap().codec.clone()),
                        audio_sample_rate: Some(stream.audio.as_ref().unwrap().sample_rate.into()),
                        audio_channel: Some(stream.audio.as_ref().unwrap().channel.into()),
                        audio_profile: Some(stream.audio.as_ref().unwrap().profile.clone()),
                        ..Default::default()
                    },
@oatmealdealer
Copy link

This might do it, depending on your query:

#[derive(GraphQLQuery)]
#[graphql(
    schema_path = "schema.json",
    query_path = "query.graphql",
    variables_derives = "Default",
)]
pub struct Query;

This works for me in cases where the input variables don't include any generated Rust enums, for example, since those require #[default] to derive the trait properly. But I think if you don't have any enums in your input variables then variables_derives will work.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants