Skip to content

Commit

Permalink
fix: sync handler returns (#246)
Browse files Browse the repository at this point in the history
  • Loading branch information
elcharitas authored Jan 12, 2025
1 parent 5ce6519 commit 63b47ed
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 23 deletions.
4 changes: 2 additions & 2 deletions crates/macros/src/common/handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,14 +116,14 @@ pub fn handler_macro(args: TokenStream, raw_input: TokenStream) -> TokenStream {
},
false => quote! {
let output = (|#inputs| #block)(#args);
*cx.response_mut().body_mut() = output.to_bytes().into();
Box::new(output) as Box<dyn ngyn::prelude::ToBytes>
},
};

let output = asyncness.map(|_| {
let r_arrow = RArrow::default();
quote! { #r_arrow std::pin::Pin<Box<dyn std::future::Future<Output = Box<dyn ngyn::prelude::ToBytes>> + Send + '_cx_lifetime>> }
});
}).unwrap_or_else(|| quote! { -> Box<dyn ngyn::prelude::ToBytes> });

quote! {
#vis #constness #unsafety #fn_token #ident <#generics_stream>(cx: &'_cx_lifetime mut ngyn::prelude::NgynContext) #output {
Expand Down
8 changes: 2 additions & 6 deletions crates/shared/src/core/handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use crate::server::{NgynContext, ToBytes};
/// Represents a handler function that takes in a mutable reference to `NgynContext` and `NgynResponse`.
pub(crate) type Handler = dyn Fn(&mut NgynContext) -> Box<dyn ToBytes> + Send + Sync + 'static;

pub type AsyncHandler = dyn for<'a, 'b> Fn(
pub(crate) type AsyncHandler = dyn for<'a, 'b> Fn(
&'a mut NgynContext,
) -> Pin<Box<dyn Future<Output = Box<dyn ToBytes>> + Send + 'a>>
+ Send
Expand Down Expand Up @@ -66,11 +66,7 @@ pub fn async_handler<S: ToBytes + 'static, Fut: Future<Output = S> + Send + 'sta
) -> Box<AsyncHandler> {
Box::new(move |ctx: &mut NgynContext| {
let fut = f(ctx);
Box::pin(async move {
let body = fut.await.to_bytes();
*ctx.response_mut().body_mut() = body.into();
Box::new(()) as Box<dyn ToBytes>
})
Box::pin(async move { Box::new(fut.await) as Box<dyn ToBytes> })
})
}

Expand Down
36 changes: 21 additions & 15 deletions examples/graphql_app/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ use ngyn::prelude::*;
use ngyn_hyper::HyperApplication;
use std::sync::Arc;

#[handler]
fn home() -> &'static str {
"<html><body>You can access the GraphQL playground at <a href='/playground'>/playground</a> or the GraphiQL interface at <a href='/graphiql'>/graphiql</a>.</body></html>"
}

#[handler]
async fn handle_graphql(req: NgynRequest) -> String {
let db = Arc::new(Database::new());
Expand All @@ -19,30 +24,31 @@ async fn handle_graphql(req: NgynRequest) -> String {
graphql_res.body().as_str().to_owned()
}

#[handler]
async fn handle_graphiql() -> String {
let graphiql_res = graphiql("/graphql", None).await;
graphiql_res.body().as_str().to_owned()
}

#[handler]
async fn handle_playground() -> String {
let playground_res = playground("/graphql", None).await;
playground_res.body().as_str().to_owned()
}

#[tokio::main]
async fn main() {
let mut app = HyperApplication::default();

app.get("/", handler(|_c| "<html><body>You can access the GraphQL playground at <a href='/playground'>/playground</a> or the GraphiQL interface at <a href='/graphiql'>/graphiql</a>.</body></html>"));
app.get("/", home);

app.get(
"/playground",
async_handler(|_c| async {
let playground_res = playground("/graphql", None).await;
playground_res.body().as_str().to_owned()
}),
);
app.get("/playground", async_wrap(handle_playground));

app.get(
"/graphiql",
async_handler(|_c| async {
let graphiql_res = graphiql("/graphql", None).await;
graphiql_res.body().as_str().to_owned()
}),
);
app.get("/graphiql", async_wrap(handle_graphiql));

app.any("/graphql", async_wrap(handle_graphql));

println!("Listening on http://127.0.0.1:8080");

let _ = app.listen("127.0.0.1:8080").await;
}

0 comments on commit 63b47ed

Please sign in to comment.