Added docs for Interface field method argument (#124)

* Added docs for `Interface` field `method` argument
This commit is contained in:
Иван Плесских 2020-05-28 20:50:01 +05:00 committed by GitHub
parent 313f3b7b2d
commit 6d56c0157e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 28 additions and 2 deletions

View File

@ -7,6 +7,12 @@ Therefore, the `Object`'s fields' type, arguments must match with the `Interface
`Async-graphql` implemented auto conversion from `Object` to `Interface`, you only need to call `Into::into`.
Interface fields names transforms to camelCase in schema definition.
If you need e.g. snake_cased field name, there is `method` argument in field.
- When the `name` and `method` exist together, the `name` is the graphql name and the `method` is the rust method name.
- When only `name` exists, `name.to_camel_case()` is the graphql name and the `name` is the rust method name.
```rust
use async_graphql::*;
@ -23,6 +29,11 @@ impl Circle {
async fn scale(&self, s: f32) -> Shape {
Circle { radius: self.radius * s }.into()
}
#[field(name = "short_description")]
async fn short_description(&self) -> String {
"Circle".to_string()
}
}
struct Square {
@ -38,11 +49,17 @@ impl Square {
async fn scale(&self, s: f32) -> Shape {
Square { width: self.width * s }.into()
}
#[field(name = "short_description")]
async fn short_description(&self) -> String {
"Square".to_string()
}
}
#[Interface(
field(name = "area", type = "f32"),
field(name = "scale", type = "Shape", arg(name = "s", type = "f32"))
field(name = "short_description", method = "short_description", type = "String")
)]
enum Shape {
Circle(Circle),

View File

@ -451,7 +451,7 @@ pub use async_graphql_derive::InputObject;
/// | Attribute | description | Type | Optional |
/// |-------------|---------------------------|----------|----------|
/// | name | Field name | string | N |
/// | method | Method name | string | Y |
/// | method | Rust resolver method name. If specified, `name` will not be camelCased in schema definition | string | Y |
/// | type | Field type | string | N |
/// | desc | Field description | string | Y |
/// | deprecation | Field deprecation reason | string | Y |
@ -508,6 +508,12 @@ pub use async_graphql_derive::InputObject;
/// async fn value_c(&self, a: i32, b: i32) -> i32 {
/// a + b
/// }
///
/// /// Disabled name transformation, don't forget "method" argument in interface!
/// #[field(name = "value_d")]
/// async fn value_d(&self) -> i32 {
/// &self.value + 1
/// }
/// }
///
/// #[Interface(
@ -516,6 +522,7 @@ pub use async_graphql_derive::InputObject;
/// field(name = "value_c", type = "i32",
/// arg(name = "a", type = "i32"),
/// arg(name = "b", type = "i32")),
/// field(name = "value_d", method = "value_d", type = "i32"),
/// )]
/// enum MyInterface {
/// TypeA(TypeA)
@ -539,13 +546,15 @@ pub use async_graphql_derive::InputObject;
/// valueA
/// valueB
/// valueC(a: 3, b: 2)
/// value_d
/// }
/// }"#).await.unwrap().data;
/// assert_eq!(res, serde_json::json!({
/// "typeA": {
/// "valueA": "hello",
/// "valueB": 10,
/// "valueC": 5
/// "valueC": 5,
/// "value_d": 11
/// }
/// }));
/// }