async-graphql/docs/zh-CN/src/define_interface.md

107 lines
2.9 KiB
Markdown
Raw Normal View History

2020-04-15 03:15:30 +00:00
# 接口(Interface)
2020-04-16 07:09:09 +00:00
2020-05-13 04:49:43 +00:00
接口用于抽象具有特定字段集合的对象,`Async-graphql`内部实现实际上是一个包装器包装器转发接口上定义的Resolver函数到实现该接口的对象所以接口类型所包含的字段类型参数都必须和实现该接口的对象完全匹配。
2020-04-16 07:09:09 +00:00
`Async-graphql`自动实现了对象到接口的转换,把一个对象类型转换为接口类型只需要调用`Into::into`。
2020-05-28 16:01:04 +00:00
接口字段的`name`属性表示转发的Resolver函数并且它将被转换为驼峰命名作为字段名称。
如果你需要自定义GraphQL接口字段名称可以同时使用`name`和`method`属性。
- 当`name`和`method`属性同时存在时,`name`是GraphQL接口字段名而`method`是Resolver函数名。
- 当只有`name`存在时, 转换为驼峰命名后的`name`是GraphQL接口字段名而`name`是Resolver函数名。
2020-04-16 07:09:09 +00:00
```rust
use async_graphql::*;
struct Circle {
radius: f32,
}
#[Object]
2020-04-16 07:09:09 +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 16:01:04 +00:00
2020-09-28 09:44:00 +00:00
#[graphql(name = "short_description")]
2020-05-28 16:01:04 +00:00
async fn short_description(&self) -> String {
"Circle".to_string()
}
2020-04-16 07:09:09 +00:00
}
struct Square {
width: f32,
}
#[Object]
2020-04-16 07:09:09 +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 16:01:04 +00:00
2020-09-28 09:44:00 +00:00
#[graphql(name = "short_description")]
2020-05-28 16:01:04 +00:00
async fn short_description(&self) -> String {
"Square".to_string()
}
2020-04-16 07:09:09 +00:00
}
#[derive(Interface)]
2020-09-13 04:12:32 +00:00
#[graphql(
2020-04-16 07:09:09 +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 16:01:04 +00:00
field(name = "short_description", method = "short_description", type = "String")
2020-04-16 07:09:09 +00:00
)]
2020-05-11 04:39:43 +00:00
enum Shape {
Circle(Circle),
Square(Square),
}
```
2021-08-04 14:59:46 +00:00
## 手工注册接口类型
`Async-graphql`在初始化阶段从`Schema`开始遍历并注册所有被直接或者间接引用的类型,如果某个接口没有被引用到,那么它将不会存在于注册表中,就像下面的例子,
即使`MyObject`实现了`MyInterface`,但由于`Schema`中并没有引用`MyInterface`,类型注册表中将不会存在`MyInterface`类型的信息。
```rust
#[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>;
```
你需要在构造Schema时手工注册`MyInterface`类型:
```rust
Schema::build(Query, EmptyMutation, EmptySubscription)
.register_type::<MyInterface>()
2021-08-09 15:44:27 +00:00
.finish();
2021-08-04 14:59:46 +00:00
```