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

fix: fix incorrect sdl optionality with schema defaults #1325

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

Adam-Alj
Copy link
Contributor

@Adam-Alj Adam-Alj commented Jul 22, 2023

Fixes: #1321

We need to know whether or not each type has a schema-default value in order to generate the SDL with correct optionality of fields.

Say you have the following InputObject:

#[derive(InputObject)]
struct SampleInput {
    #[graphql(default = 1)]
    optional_input_defaulted: i32
}

The SDL currently generated by this is:

input SampleInput {
    optionalInputDefaulted: Int! = 1
}

Which means that callers are required to pass in a value optionalInputDefaulted as input, even though we are specifying that we are defaulting this value in the schema.

Then, in order to solve this issue, you would need to change your input object to the following:

#[derive(InputObject)]
struct SampleInput {
    #[graphql(default = 1)]
    optional_input_defaulted: Option<i32>
}

This generates the correct SDL of:

input SampleInput {
    optionalInputDefaulted: Int = 1
}

Which lets the caller make use of the defaulting behavior by not passing in a value.


By requiring types to be wrapped as Options even though they are being defaulted, we need to treat them as Options throughout the project, which is silly since we know that they are being defaulted and therefore are always Some.

This change fixes that, by marking input fields with defaults as optional in the SDL.

@Adam-Alj
Copy link
Contributor Author

Looks like CI checks are failing. I'll try and fix those up sometime tomorrow. The tests/subscription_websocket_subscriptions_transport_ws.rs test fails locally for me even off of master. Must have overlooked the failures due to it.

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

Successfully merging this pull request may close these issues.

Incorrect Optionality in Generated SDL for InputObject Fields With Defined Defaults
1 participant