-
Notifications
You must be signed in to change notification settings - Fork 434
Is there an ability to have a default value for input values? #28
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
Comments
Do you mean default values on arguments for fields? This is supported; check out the tests here: https://github.com/mhallin/juniper/blob/master/src/macros/tests/args.rs#L46-L54 and the documentation here: https://mhallin.github.io/juniper/juniper/macro.graphql_object!.html#field-arguments |
@mhallin Sorry, it is one-half of what I was looking for. The syntax currently doesn't expand to input objects. So for example,
Which I believe should be supported if I am reading the specs correctly. |
Huh, no I must have completely missed that feature. I'll try to add it. |
The rustdoc hasn't updated yet, but the just-released 0.6.3 version adds default values for input object fields: graphql_input_object!(
struct SampleObject {
foo = 123: i64 as "A sample field, defaults to 123 if omitted"
}
); |
The specs aren't really clear on whether a default can only be a basic scalar types or if it can be any type kind. So for example, enum Color {
Red,
Orange,
Green,
Blue,
Black,
Grey,
}
graphql_enum!(Color {
Color::Red => "RED" as "The color red",
Color::Orange => "ORANGE",
Color::Green => "GREEN",
Color::Blue => "BLUE",
Color::Grey => "GREY",
Color::Black => "BLACK" deprecated "Superseded by ORANGE",
});
graphql_input_object!(
pub struct Room {
room_color = Color::Grey : Color as "The color of the room defaults to grey"
}
); doesn't work. However, I am not 100% certain if it should work due to the wording of the defaultValue clause. |
I believe this issue also extends to functions which makes it difficult to set defaults for strings which require String::from(), or "test".to_owned(), etc... |
Yeah, due to some syntactical limitations in how the macros work, you need to parenthesize expressions that aren't just a single token tree, e.g.
The documentation for release 0.7.0 has been updated with some small examples. That release also contains a fix where input objects couldn't be used as default values. |
According to the specs, one should be able to set a default value as indicated both here input type spec and here. Also, this feature is supported in the js implementation as well as indicated in this issue. As far I can tell the input macro doesn't allow you specify this so this would be a great feature to have unless of course, it is already there and I am overlooking it.
The text was updated successfully, but these errors were encountered: