2020-08-11 03:42:19 +00:00
|
|
|
|
# 合并对象(MergedObject)
|
|
|
|
|
|
|
|
|
|
## 为同一类型实现多次Object
|
|
|
|
|
|
|
|
|
|
通常我们在Rust中可以为同一类型创建多个实现,但由于过程宏的限制,无法为同一个类型创建多个Object实现。例如,下面的代码将无法通过编译。
|
|
|
|
|
|
|
|
|
|
```rust
|
2020-09-18 00:52:13 +00:00
|
|
|
|
#[Object]
|
2020-08-11 03:42:19 +00:00
|
|
|
|
impl MyObject {
|
|
|
|
|
async fn field1(&self) -> i32 {
|
|
|
|
|
todo!()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-18 00:52:13 +00:00
|
|
|
|
#[Object]
|
2020-08-11 03:42:19 +00:00
|
|
|
|
impl MyObject {
|
|
|
|
|
async fn field2(&self) -> i32 {
|
|
|
|
|
todo!()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
2020-09-18 00:52:13 +00:00
|
|
|
|
用 `#[derive(MergedObject)]` 宏允许你合并多个独立的Object为一个.
|
2020-08-11 03:42:19 +00:00
|
|
|
|
|
2020-09-18 00:52:13 +00:00
|
|
|
|
**提示:** 每个`#[Object]`需要一个唯一的名称,即使在一个`MergedObject`内,所以确保每个对象有单独的名称。
|
2020-08-11 03:42:19 +00:00
|
|
|
|
|
|
|
|
|
```rust
|
2021-10-03 14:58:13 +00:00
|
|
|
|
#[derive(Default)]
|
|
|
|
|
struct UserQuery;
|
|
|
|
|
|
2020-09-18 00:52:13 +00:00
|
|
|
|
#[Object]
|
2020-08-11 03:42:19 +00:00
|
|
|
|
impl UserQuery {
|
|
|
|
|
async fn users(&self) -> Vec<User> {
|
|
|
|
|
todo!()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-03 14:58:13 +00:00
|
|
|
|
#[derive(Default)]
|
|
|
|
|
struct MovieQuery;
|
|
|
|
|
|
2020-09-18 00:52:13 +00:00
|
|
|
|
#[Object]
|
2020-08-11 03:42:19 +00:00
|
|
|
|
impl MovieQuery {
|
|
|
|
|
async fn movies(&self) -> Vec<Movie> {
|
|
|
|
|
todo!()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-18 00:52:13 +00:00
|
|
|
|
#[derive(MergedObject, Default)]
|
2020-08-11 03:42:19 +00:00
|
|
|
|
struct Query(UserQuery, MovieQuery);
|
|
|
|
|
|
|
|
|
|
let schema = Schema::new(
|
|
|
|
|
Query::default(),
|
|
|
|
|
EmptyMutation,
|
|
|
|
|
EmptySubscription
|
|
|
|
|
);
|
|
|
|
|
```
|
2020-08-28 06:04:59 +00:00
|
|
|
|
|
2020-10-14 22:27:31 +00:00
|
|
|
|
> ⚠️ **合并的对象无法在Interface中使用。**
|
|
|
|
|
|
2020-08-28 06:04:59 +00:00
|
|
|
|
# 合并订阅
|
|
|
|
|
|
2020-09-18 00:52:13 +00:00
|
|
|
|
和`MergedObject`一样,你可以派生`MergedSubscription`来合并单独的`#[Subscription]`块。
|
2020-08-28 06:04:59 +00:00
|
|
|
|
|
|
|
|
|
像合并对象一样,每个订阅块都需要一个唯一的名称。
|
|
|
|
|
|
|
|
|
|
```rust
|
|
|
|
|
#[derive(Default)]
|
|
|
|
|
struct Subscription1;
|
|
|
|
|
|
2020-09-18 00:52:13 +00:00
|
|
|
|
#[Subscription]
|
2020-08-28 06:04:59 +00:00
|
|
|
|
impl Subscription1 {
|
|
|
|
|
async fn events1(&self) -> impl Stream<Item = i32> {
|
|
|
|
|
futures::stream::iter(0..10)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[derive(Default)]
|
|
|
|
|
struct Subscription2;
|
|
|
|
|
|
2020-09-18 00:52:13 +00:00
|
|
|
|
#[Subscription]
|
2020-08-28 06:04:59 +00:00
|
|
|
|
impl Subscription2 {
|
|
|
|
|
async fn events2(&self) -> impl Stream<Item = i32> {
|
|
|
|
|
futures::stream::iter(10..20)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-18 00:52:13 +00:00
|
|
|
|
#[derive(MergedSubscription, Default)]
|
2020-08-28 06:04:59 +00:00
|
|
|
|
struct Subscription(Subscription1, Subscription2);
|
|
|
|
|
|
|
|
|
|
let schema = Schema::new(
|
|
|
|
|
Query::default(),
|
|
|
|
|
EmptyMutation,
|
|
|
|
|
Subscription::default()
|
|
|
|
|
);
|
|
|
|
|
```
|