async-graphql/docs/zh-CN/src/define_interface.md
2020-05-10 21:39:43 -07:00

1.1 KiB
Raw Blame History

接口(Interface)

接口用于抽象具有特定字段集合的对象,Async-graphql内部实现实际上是一个包装器包装器转发接口上定义的Resolve函数到实现该接口的对象所以接口类型所包含的字段类型参数都必须和实现该接口的对象完全匹配。

Async-graphql自动实现了对象到接口的转换,把一个对象类型转换为接口类型只需要调用Into::into

use async_graphql::*;

struct Circle {
    radius: f32,
}

#[Object]
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()
    }
}

struct Square {
    width: f32,
}

#[Object]
impl Square {
    async fn area(&self) -> f32 {
        self.width * self.width
    }

    async fn scale(&self, s: f32) -> Shape {
        Square { width: self.width * s }.into()
    }
}

#[Interface(
    field(name = "area", type = "f32"),
    field(name = "scale", type = "Shape", arg(name = "s", type = "f32"))
)]
enum Shape {
    Circle(Circle),
    Square(Square),
}