Juniper: Is there an ability to have a default value for input values?

Created on 9 Feb 2017  路  7Comments  路  Source: graphql-rust/juniper

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.

Most helpful comment

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

All 7 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,

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

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.

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.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

ysimonson picture ysimonson  路  5Comments

teymour-aldridge picture teymour-aldridge  路  5Comments

projektir picture projektir  路  4Comments

theduke picture theduke  路  4Comments

spacemeowx2 picture spacemeowx2  路  5Comments