Skip to content

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

Closed
TheServerAsterisk opened this issue Feb 9, 2017 · 7 comments
Closed

Comments

@TheServerAsterisk
Copy link
Contributor

TheServerAsterisk commented Feb 9, 2017

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.

@mhallin
Copy link
Member

mhallin commented Feb 9, 2017

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

@TheServerAsterisk
Copy link
Contributor Author

TheServerAsterisk commented Feb 9, 2017

@mhallin Sorry, it is one-half of what I was looking for. The syntax currently doesn't expand to input objects. So for example,

graphql_input_object!(
    pub struct HoursWorked {
        daily_total = 0.0 : f64
    }
)

Which I believe should be supported if I am reading the specs correctly.

@mhallin
Copy link
Member

mhallin commented Feb 9, 2017

Huh, no I must have completely missed that feature. I'll try to add it.

@mhallin
Copy link
Member

mhallin commented Feb 19, 2017

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"
    }
);

@TheServerAsterisk
Copy link
Contributor Author

TheServerAsterisk commented Feb 22, 2017

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.

@TheServerAsterisk
Copy link
Contributor Author

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...

@mhallin
Copy link
Member

mhallin commented Feb 26, 2017

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.

room_color = (Color::Gray): Color

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.

@mhallin mhallin closed this as completed Apr 19, 2017
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