Added docs for Interface
field method
argument (#124)
* Added docs for `Interface` field `method` argument
This commit is contained in:
parent
313f3b7b2d
commit
6d56c0157e
|
@ -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`.
|
`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
|
```rust
|
||||||
use async_graphql::*;
|
use async_graphql::*;
|
||||||
|
|
||||||
|
@ -23,6 +29,11 @@ impl Circle {
|
||||||
async fn scale(&self, s: f32) -> Shape {
|
async fn scale(&self, s: f32) -> Shape {
|
||||||
Circle { radius: self.radius * s }.into()
|
Circle { radius: self.radius * s }.into()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[field(name = "short_description")]
|
||||||
|
async fn short_description(&self) -> String {
|
||||||
|
"Circle".to_string()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
struct Square {
|
struct Square {
|
||||||
|
@ -38,11 +49,17 @@ impl Square {
|
||||||
async fn scale(&self, s: f32) -> Shape {
|
async fn scale(&self, s: f32) -> Shape {
|
||||||
Square { width: self.width * s }.into()
|
Square { width: self.width * s }.into()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[field(name = "short_description")]
|
||||||
|
async fn short_description(&self) -> String {
|
||||||
|
"Square".to_string()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[Interface(
|
#[Interface(
|
||||||
field(name = "area", type = "f32"),
|
field(name = "area", type = "f32"),
|
||||||
field(name = "scale", type = "Shape", arg(name = "s", type = "f32"))
|
field(name = "scale", type = "Shape", arg(name = "s", type = "f32"))
|
||||||
|
field(name = "short_description", method = "short_description", type = "String")
|
||||||
)]
|
)]
|
||||||
enum Shape {
|
enum Shape {
|
||||||
Circle(Circle),
|
Circle(Circle),
|
||||||
|
|
13
src/lib.rs
13
src/lib.rs
|
@ -451,7 +451,7 @@ pub use async_graphql_derive::InputObject;
|
||||||
/// | Attribute | description | Type | Optional |
|
/// | Attribute | description | Type | Optional |
|
||||||
/// |-------------|---------------------------|----------|----------|
|
/// |-------------|---------------------------|----------|----------|
|
||||||
/// | name | Field name | string | N |
|
/// | 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 |
|
/// | type | Field type | string | N |
|
||||||
/// | desc | Field description | string | Y |
|
/// | desc | Field description | string | Y |
|
||||||
/// | deprecation | Field deprecation reason | 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 {
|
/// async fn value_c(&self, a: i32, b: i32) -> i32 {
|
||||||
/// a + b
|
/// 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(
|
/// #[Interface(
|
||||||
|
@ -516,6 +522,7 @@ pub use async_graphql_derive::InputObject;
|
||||||
/// field(name = "value_c", type = "i32",
|
/// field(name = "value_c", type = "i32",
|
||||||
/// arg(name = "a", type = "i32"),
|
/// arg(name = "a", type = "i32"),
|
||||||
/// arg(name = "b", type = "i32")),
|
/// arg(name = "b", type = "i32")),
|
||||||
|
/// field(name = "value_d", method = "value_d", type = "i32"),
|
||||||
/// )]
|
/// )]
|
||||||
/// enum MyInterface {
|
/// enum MyInterface {
|
||||||
/// TypeA(TypeA)
|
/// TypeA(TypeA)
|
||||||
|
@ -539,13 +546,15 @@ pub use async_graphql_derive::InputObject;
|
||||||
/// valueA
|
/// valueA
|
||||||
/// valueB
|
/// valueB
|
||||||
/// valueC(a: 3, b: 2)
|
/// valueC(a: 3, b: 2)
|
||||||
|
/// value_d
|
||||||
/// }
|
/// }
|
||||||
/// }"#).await.unwrap().data;
|
/// }"#).await.unwrap().data;
|
||||||
/// assert_eq!(res, serde_json::json!({
|
/// assert_eq!(res, serde_json::json!({
|
||||||
/// "typeA": {
|
/// "typeA": {
|
||||||
/// "valueA": "hello",
|
/// "valueA": "hello",
|
||||||
/// "valueB": 10,
|
/// "valueB": 10,
|
||||||
/// "valueC": 5
|
/// "valueC": 5,
|
||||||
|
/// "value_d": 11
|
||||||
/// }
|
/// }
|
||||||
/// }));
|
/// }));
|
||||||
/// }
|
/// }
|
||||||
|
|
Loading…
Reference in New Issue
Block a user