|
1 | 1 | use anyhow::Result; |
2 | | -use async_graphql::{Context, EmptyMutation, Object, Schema, Subscription}; |
3 | 2 | use async_graphql::futures_util::Stream; |
4 | 3 | use async_graphql::futures_util::stream; |
5 | | -use tako::{Method, router::Router}; |
| 4 | +use async_graphql::{Context, EmptyMutation, Object, Schema, Subscription}; |
| 5 | +use std::time::Duration; |
6 | 6 | use tako::extractors::FromRequest; |
7 | 7 | use tako::graphql::{GraphQLRequest, GraphQLResponse, GraphQLSubscription}; |
8 | 8 | use tako::types::Request as TakoRequest; |
| 9 | +use tako::{Method, router::Router}; |
9 | 10 | use tokio::net::TcpListener; |
10 | | -use std::time::Duration; |
11 | 11 |
|
12 | 12 | struct QueryRoot; |
13 | 13 |
|
14 | 14 | #[Object] |
15 | 15 | impl QueryRoot { |
16 | | - async fn hello(&self) -> &str { "Hello, GraphQL!" } |
| 16 | + async fn hello(&self) -> &str { |
| 17 | + "Hello, GraphQL!" |
| 18 | + } |
17 | 19 | } |
18 | 20 |
|
19 | 21 | struct SubscriptionRoot; |
20 | 22 |
|
21 | 23 | #[Subscription] |
22 | 24 | impl SubscriptionRoot { |
23 | | - async fn tick(&self, _ctx: &Context<'_>) -> impl Stream<Item = i32> { |
24 | | - stream::unfold(0, |i| async move { |
25 | | - tokio::time::sleep(Duration::from_millis(200)).await; |
26 | | - Some((i, i + 1)) |
27 | | - }) |
28 | | - } |
| 25 | + async fn tick(&self, _ctx: &Context<'_>) -> impl Stream<Item = i32> { |
| 26 | + stream::unfold(0, |i| async move { |
| 27 | + tokio::time::sleep(Duration::from_millis(200)).await; |
| 28 | + Some((i, i + 1)) |
| 29 | + }) |
| 30 | + } |
29 | 31 | } |
30 | 32 |
|
31 | 33 | type AppSchema = Schema<QueryRoot, EmptyMutation, SubscriptionRoot>; |
32 | 34 |
|
33 | 35 | #[tokio::main] |
34 | 36 | async fn main() -> Result<()> { |
35 | | - let listener = TcpListener::bind("127.0.0.1:8080").await?; |
| 37 | + let listener = TcpListener::bind("127.0.0.1:8080").await?; |
36 | 38 |
|
37 | | - let schema = Schema::build(QueryRoot, EmptyMutation, SubscriptionRoot) |
38 | | - .finish(); |
| 39 | + let schema = Schema::build(QueryRoot, EmptyMutation, SubscriptionRoot).finish(); |
39 | 40 |
|
40 | | - let mut router = Router::new(); |
| 41 | + let mut router = Router::new(); |
41 | 42 |
|
42 | | - // POST /graphql |
43 | | - router.route(Method::POST, "/graphql", { |
44 | | - let schema = schema.clone(); |
45 | | - move |mut req: TakoRequest| { |
46 | | - let schema = schema.clone(); |
47 | | - async move { |
48 | | - let gql_req: GraphQLRequest = GraphQLRequest::from_request(&mut req).await.unwrap(); |
49 | | - let resp = schema.execute(gql_req.0).await; |
50 | | - GraphQLResponse(resp) |
51 | | - } |
52 | | - } |
53 | | - }); |
| 43 | + // POST /graphql |
| 44 | + router.route(Method::POST, "/graphql", { |
| 45 | + let schema = schema.clone(); |
| 46 | + move |mut req: TakoRequest| { |
| 47 | + let schema = schema.clone(); |
| 48 | + async move { |
| 49 | + let gql_req: GraphQLRequest = GraphQLRequest::from_request(&mut req).await.unwrap(); |
| 50 | + let resp = schema.execute(gql_req.0).await; |
| 51 | + GraphQLResponse(resp) |
| 52 | + } |
| 53 | + } |
| 54 | + }); |
54 | 55 |
|
55 | | - // GET /ws for subscriptions (graphql-transport-ws or graphql-ws) |
56 | | - router.route(Method::GET, "/ws", { |
57 | | - let schema = schema.clone(); |
58 | | - move |req: TakoRequest| { |
59 | | - let schema = schema.clone(); |
60 | | - async move { GraphQLSubscription::new(req, schema) } |
61 | | - } |
62 | | - }); |
| 56 | + // GET /ws for subscriptions (graphql-transport-ws or graphql-ws) |
| 57 | + router.route(Method::GET, "/ws", { |
| 58 | + let schema = schema.clone(); |
| 59 | + move |req: TakoRequest| { |
| 60 | + let schema = schema.clone(); |
| 61 | + async move { GraphQLSubscription::new(req, schema) } |
| 62 | + } |
| 63 | + }); |
63 | 64 |
|
64 | | - println!("GraphQL: POST http://127.0.0.1:8080/graphql"); |
65 | | - println!("Subscriptions (WS): ws://127.0.0.1:8080/ws"); |
| 65 | + println!("GraphQL: POST http://127.0.0.1:8080/graphql"); |
| 66 | + println!("Subscriptions (WS): ws://127.0.0.1:8080/ws"); |
66 | 67 |
|
67 | | - tako::serve(listener, router).await; |
68 | | - Ok(()) |
| 68 | + tako::serve(listener, router).await; |
| 69 | + Ok(()) |
69 | 70 | } |
0 commit comments