graphql-rust / juniper

GraphQL server library for Rust
Other
5.72k stars 425 forks source link

Mutation argument with default value is marked as required in schema #1083

Closed deiwo closed 2 years ago

deiwo commented 2 years ago

Describe the bug The problem is when mutation argument is specified with scalar type and a default value in the macro. In the schema that is generated this argument is marked as required with ! even though it doesn't have to be required, since the default value is specified.

To Reproduce Define a simple mutation:

fn do_something(
    #[graphql(default = false)]
    should_switch: bool
) {
    ....
}

The generated schema:

mutation doSomething(
    shouldSwitch: Boolean! = false
)

Send a request to this mutation with should_switch set to undefined. This request gets a error response that the required type is Boolean! but Boolean was provided.

Expected behavior The default value is used for the argument and no error is raised.

Additional information This behavior occurs in master branch, but in the v15 it works as expected.

ilslv commented 2 years ago

As pointed out in #1073, behaviour of v15 is wrong and was fixed on master in #1080. Detailed spec explanation can be found in Coercing Field Arguments section, but I find wording here more helpful:

In other words, there is a semantic difference between the explicitly provided value null versus having not provided a value.

So providing null for shouldSwitch field is different from not providing value at all. In current scenario first option should raise an error, while second one will use default value.

tyranron commented 2 years ago

I guess, the issue with the client, that it considers undefined as null, so that's why doesn't type check.