问题描述
我正在尝试将一个基本的 GraphQL 服务器与 async-graphql 和 warp 组合在一起:
use async_graphql::{Object,Schema,EmptyMutation,EmptySubscription,Response,Context};
use std::convert::Infallible;
use warp::{Filter};
#[tokio::main]
async fn main() {
let schema = Schema::build(Query,EmptySubscription).finish();
// schema processor
let graphql_api = async_graphql_warp::graphql(schema).and_then(
|(schema,request): (
Schema<Query,EmptySubscription>,async_graphql::Request,)| async move { Ok::<_,Infallible>(Response::from(schema.execute(request).await)) },);
let routes = graphql_api;
warp::serve(routes).bind(([0,0],3000)).await;
}
pub struct Book {
id: String,name: String,author: String,}
#[Object]
impl Book {
async fn id(&self) -> &str {
&self.id
}
async fn name(&self) -> &str {
&self.name
}
async fn author(&self) -> &str {
&self.author
}
}
struct Query;
#[Object]
impl Query {
async fn books(&self,_ctx: &Context<'_>) -> Vec<Book> {
vec![new_book("id1","Penturian","Gibsons"),new_book("id2","Dekatos","Gibsons")]
}
}
fn new_book(id: &str,name: &str,author: &str) -> Book {
Book { id: id.to_string(),name: name.to_string(),author: author.to_string()}
}
error[E0277]: the trait bound `async_graphql::Response: Reply` is not satisfied
--> src/main.rs:22:17
|
22 | warp::serve(routes).bind(([0,3000)).await;
| ^^^^^^ the trait `Reply` is not implemented for `async_graphql::Response`
|
::: /Users/stephen.gibson/.cargo/registry/src/github.com-1ecc6299db9ec823/warp-0.3.1/src/server.rs:26:17
|
26 | F::Extract: Reply,| ----- required by this bound in `serve`
|
= note: required because of the requirements on the impl of `Reply` for `(async_graphql::Response,)`
这对我来说没有多大意义。
Cargo.toml:
[package]
name = "graphql"
version = "0.1.0"
edition = "2018"
# See more keys and their deFinitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
async-graphql = "2.0"
async-graphql-warp = "2.0"
tokio = { version = "1.0.2",features = ["macros","rt-multi-thread"] }
warp = "0.3"
serde_json = "1.0"
serde_derive = "1.0"
serde = "1.0"
futures = "0.3"
解决方法
我导入错误。我正在导入 async_graphql::Response 但不是 async_graphql_warp::Response。