2020-04-15 03:15:30 +00:00
|
|
|
# Interface
|
2020-05-09 17:53:41 +00:00
|
|
|
|
2020-05-11 04:39:43 +00:00
|
|
|
`Interface` is used to abstract `Object`s with common fields.
|
2020-09-01 05:47:22 +00:00
|
|
|
`Async-graphql` implements it as a wrapper.
|
|
|
|
The wrapper will forward field resolution to the `Object` that implements this `Interface`.
|
|
|
|
Therefore, the `Object`'s fields' type and arguments must match with the `Interface`'s.
|
2020-05-09 17:53:41 +00:00
|
|
|
|
2020-09-01 05:47:22 +00:00
|
|
|
`Async-graphql` implements auto conversion from `Object` to `Interface`, you only need to call `Into::into`.
|
2020-05-09 17:53:41 +00:00
|
|
|
|
2020-09-01 05:47:22 +00:00
|
|
|
Interface field names are transformed to camelCase for the schema definition.
|
|
|
|
If you need e.g. a snake_cased GraphQL field name, you can use both the `name` and `method` attributes.
|
2020-05-28 15:50:01 +00:00
|
|
|
|
2020-09-01 05:47:22 +00:00
|
|
|
- When `name` and `method` exist together, `name` is the GraphQL field name and the `method` is the resolver function name.
|
2020-05-28 16:01:04 +00:00
|
|
|
- When only `name` exists, `name.to_camel_case()` is the GraphQL field name and the `name` is the resolver function name.
|
2020-05-28 15:50:01 +00:00
|
|
|
|
2020-05-09 17:53:41 +00:00
|
|
|
```rust
|
2022-06-02 21:32:12 +00:00
|
|
|
# extern crate async_graphql;
|
2020-05-09 17:53:41 +00:00
|
|
|
use async_graphql::*;
|
|
|
|
|
|
|
|
struct Circle {
|
|
|
|
radius: f32,
|
|
|
|
}
|
|
|
|
|
2020-09-18 00:52:13 +00:00
|
|
|
#[Object]
|
2020-05-09 17:53:41 +00:00
|
|
|
impl Circle {
|
|
|
|
async fn area(&self) -> f32 {
|
|
|
|
std::f32::consts::PI * self.radius * self.radius
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn scale(&self, s: f32) -> Shape {
|
|
|
|
Circle { radius: self.radius * s }.into()
|
|
|
|
}
|
2020-05-28 15:50:01 +00:00
|
|
|
|
2020-09-28 09:44:00 +00:00
|
|
|
#[graphql(name = "short_description")]
|
2020-05-28 15:50:01 +00:00
|
|
|
async fn short_description(&self) -> String {
|
|
|
|
"Circle".to_string()
|
|
|
|
}
|
2020-05-09 17:53:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
struct Square {
|
|
|
|
width: f32,
|
|
|
|
}
|
|
|
|
|
2020-09-18 00:52:13 +00:00
|
|
|
#[Object]
|
2020-05-09 17:53:41 +00:00
|
|
|
impl Square {
|
|
|
|
async fn area(&self) -> f32 {
|
|
|
|
self.width * self.width
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn scale(&self, s: f32) -> Shape {
|
|
|
|
Square { width: self.width * s }.into()
|
|
|
|
}
|
2020-05-28 15:50:01 +00:00
|
|
|
|
2020-09-28 09:44:00 +00:00
|
|
|
#[graphql(name = "short_description")]
|
2020-05-28 15:50:01 +00:00
|
|
|
async fn short_description(&self) -> String {
|
|
|
|
"Square".to_string()
|
|
|
|
}
|
2020-05-09 17:53:41 +00:00
|
|
|
}
|
|
|
|
|
2020-09-18 00:52:13 +00:00
|
|
|
#[derive(Interface)]
|
2020-09-13 04:12:32 +00:00
|
|
|
#[graphql(
|
2020-05-09 17:53:41 +00:00
|
|
|
field(name = "area", type = "f32"),
|
2021-05-16 05:50:47 +00:00
|
|
|
field(name = "scale", type = "Shape", arg(name = "s", type = "f32")),
|
2020-05-28 15:50:01 +00:00
|
|
|
field(name = "short_description", method = "short_description", type = "String")
|
2020-05-09 17:53:41 +00:00
|
|
|
)]
|
2020-05-11 04:39:43 +00:00
|
|
|
enum Shape {
|
|
|
|
Circle(Circle),
|
|
|
|
Square(Square),
|
|
|
|
}
|
|
|
|
```
|
2021-08-04 14:59:46 +00:00
|
|
|
|
|
|
|
## Register the interface manually
|
|
|
|
|
|
|
|
`Async-graphql` traverses and registers all directly or indirectly referenced types from `Schema` in the initialization phase.
|
|
|
|
If an interface is not referenced, it will not exist in the registry, as in the following example , even if `MyObject` implements `MyInterface`,
|
|
|
|
because `MyInterface` is not referenced in `Schema`, the `MyInterface` type will not exist in the registry.
|
|
|
|
|
|
|
|
```rust
|
2022-06-02 21:32:12 +00:00
|
|
|
# extern crate async_graphql;
|
|
|
|
# use async_graphql::*;
|
2021-08-04 14:59:46 +00:00
|
|
|
#[derive(Interface)]
|
|
|
|
#[graphql(
|
|
|
|
field(name = "name", type = "String"),
|
|
|
|
)]
|
|
|
|
enum MyInterface {
|
|
|
|
MyObject(MyObject),
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(SimpleObject)]
|
|
|
|
struct MyObject {
|
|
|
|
name: String,
|
|
|
|
}
|
|
|
|
|
|
|
|
struct Query;
|
|
|
|
|
|
|
|
#[Object]
|
|
|
|
impl Query {
|
|
|
|
async fn obj(&self) -> MyObject {
|
|
|
|
todo!()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type MySchema = Schema<Query, EmptyMutation, EmptySubscription>;
|
|
|
|
```
|
|
|
|
|
|
|
|
You need to manually register the `MyInterface` type when constructing the `Schema`:
|
|
|
|
|
|
|
|
```rust
|
2022-06-02 21:32:12 +00:00
|
|
|
# extern crate async_graphql;
|
|
|
|
# use async_graphql::*;
|
|
|
|
# #[derive(Interface)]
|
|
|
|
# #[graphql(field(name = "name", type = "String"))]
|
|
|
|
# enum MyInterface { MyObject(MyObject) }
|
|
|
|
# #[derive(SimpleObject)]
|
|
|
|
# struct MyObject { name: String, }
|
|
|
|
# struct Query;
|
|
|
|
# #[Object]
|
|
|
|
# impl Query { async fn version(&self) -> &str { "1.0" } }
|
|
|
|
|
2021-08-04 14:59:46 +00:00
|
|
|
Schema::build(Query, EmptyMutation, EmptySubscription)
|
2022-06-02 21:32:12 +00:00
|
|
|
.register_output_type::<MyInterface>()
|
2021-08-09 15:43:26 +00:00
|
|
|
.finish();
|
2021-08-04 14:59:46 +00:00
|
|
|
```
|