Rust的Graphql支持
程序员文章站
2022-06-06 10:12:07
...
#![feature(decl_macro, proc_macro_hygiene)] use rocket::response::content; use rocket::State; use juniper::tests::model::Database; use juniper::tests::schema::Query; use juniper::{EmptyMutation, RootNode}; type Schema = RootNode<'static, Query, EmptyMutation<Database>>; #[rocket::get("/")] fn graphiql() -> content::Html<String> { juniper_rocket::playground_source("/graphql") } #[rocket::get("/graphql?<request>")] fn get_graphql_handler( context: State<Database>, request: juniper_rocket::GraphQLRequest, schema: State<Schema>, ) -> juniper_rocket::GraphQLResponse { request.execute(&schema, &context) } #[rocket::post("/graphql", data = "<request>")] fn post_graphql_handler( context: State<Database>, request: juniper_rocket::GraphQLRequest, schema: State<Schema>, ) -> juniper_rocket::GraphQLResponse { request.execute(&schema, &context) } fn main() { rocket::ignite() .manage(Database::new()) .manage(Schema::new(Query, EmptyMutation::<Database>::new())) .mount( "/", rocket::routes![graphiql, get_graphql_handler, post_graphql_handler], ) .launch(); }
结论:
基于Juniper和Rocket支持的Graphql,性能确实很突出,写一些简单的服务,可以尝试下。
上一篇: 冬季减肥必吃八食谱 享受美味更享瘦
下一篇: 冬季喝粥养生效果佳 药膳粥益智增神强身