async-graphql/docs/en/src/default_value.md
Edward Rudd 3b7ed74d11 correct doc examples so they compile
- examples to fix still
  - error_extensions.md ResultExt example does not compile!
     - trait ErrorExtensions is not implemented for ParseIntError
  - dataloader
     - requires sqlx to work. So we either "stub" it OR we rewrite them simpler to use a  simple "faux" db library
2022-06-02 17:32:12 -04:00

1.7 KiB

Default value

You can define default values for input value types. Below are some examples.

Object field

# extern crate async_graphql;
use async_graphql::*;

struct Query;

fn my_default() -> i32 {
    30
}

#[Object]
impl Query {
    // The default value of the value parameter is 0, it will call i32::default()
    async fn test1(&self, #[graphql(default)] value: i32) -> i32 { todo!() }

    // The default value of the value parameter is 10
    async fn test2(&self, #[graphql(default = 10)] value: i32) -> i32 { todo!() }
    
    // The default value of the value parameter uses the return result of the my_default function, the value is 30.
    async fn test3(&self, #[graphql(default_with = "my_default()")] value: i32) -> i32 { todo!() }
}

Interface field

# extern crate async_graphql;
# fn my_default() -> i32 { 5 }
# struct MyObj;
# #[Object]
# impl MyObj {
#    async fn test1(&self, value: i32) -> i32 { todo!() }
#    async fn test2(&self, value: i32) -> i32 { todo!() }
#    async fn test3(&self, value: i32) -> i32 { todo!() }
# }
use async_graphql::*;

#[derive(Interface)]
#[graphql(
    field(name = "test1", type = "i32", arg(name = "value", type = "i32", default)),
    field(name = "test2", type = "i32", arg(name = "value", type = "i32", default = 10)),
    field(name = "test3", type = "i32", arg(name = "value", type = "i32", default_with = "my_default()")),
)]
enum MyInterface {
    MyObj(MyObj),
}

Input object field

# extern crate async_graphql;
# fn my_default() -> i32 { 5 }
use async_graphql::*;

#[derive(InputObject)]
struct MyInputObject {
    #[graphql(default)]
    value1: i32,

    #[graphql(default = 10)]
    value2: i32,

    #[graphql(default_with = "my_default()")]
    value3: i32,
}