async-graphql/docs/zh-CN/src/integrations_to_warp.md
2020-09-11 23:38:18 +08:00

1.1 KiB
Raw Blame History

Warp

Async-graphql-warp提供了两个Filtergraphqlgraphql_subscription

graphql用于执行QueryMutation请求他总是要求POST方法输出一个包含async_graphql::Schemaasync_graphql::Request的元组你可以在之后组合其它Filter或者直接调用Schema::execute执行查询。

graphql_subscription用于实现基于Web Socket的订阅它输出warp::Reply

请求例子

type MySchema = Schema<QueryRoot, EmptyMutation, EmptySubscription>;

let schema = Schema::new(QueryRoot, EmptyMutation, EmptySubscription);
let filter = async_graphql_warp::graphql(schema).and_then(|(schema, request): (MySchema, async_graphql::Request)| async move {
    // 执行查询
    let resp = schema.execute(request).await;

    // 返回结果
    Ok::<_, Infallible>(warp::reply::json(resp).into_response())
});
warp::serve(filter).run(([0, 0, 0, 0], 8000)).await;

订阅例子

let schema = Schema::new(QueryRoot, EmptyMutation, SubscriptionRoot);
let filter = async_graphql_warp::graphql_subscription(schema);
warp::serve(filter).run(([0, 0, 0, 0], 8000)).await;