-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
80 additions
and
122 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,62 +1,31 @@ | ||
use std::sync::Arc; | ||
|
||
use async_graphql::http::GraphiQLSource; | ||
use async_graphql_axum::{GraphQLRequest, GraphQLResponse}; | ||
use async_trait::async_trait; | ||
use axum::response::{Html, IntoResponse}; | ||
use nakago::{provider, Inject, Provider}; | ||
use nakago_axum::auth::Subject; | ||
use nakago_derive::Provider; | ||
use axum::response::{self, IntoResponse}; | ||
use nakago_axum::{auth::Subject, Inject}; | ||
|
||
use crate::domains::{graphql, users}; | ||
|
||
/// Events Controller | ||
#[derive(Clone)] | ||
pub struct Controller { | ||
users: Arc<Box<dyn users::Service>>, | ||
schema: Arc<graphql::Schema>, | ||
} | ||
|
||
impl Controller { | ||
/// Handle GraphiQL Requests | ||
pub async fn graphiql() -> impl IntoResponse { | ||
Html(GraphiQLSource::build().endpoint("/graphql").finish()) | ||
} | ||
|
||
/// Handle GraphQL Requests | ||
pub async fn resolve( | ||
self: Arc<Self>, | ||
sub: Subject, | ||
req: GraphQLRequest, | ||
) -> Result<GraphQLResponse, GraphQLResponse> { | ||
// Retrieve the request User, if username is present | ||
let user = if let Subject(Some(ref username)) = sub { | ||
self.users | ||
.get_by_username(username, &true) | ||
.await | ||
.unwrap_or(None) | ||
} else { | ||
None | ||
}; | ||
|
||
// Add the Subject and optional User to the context | ||
let request = req.into_inner().data(sub).data(user); | ||
|
||
Ok(self.schema.execute(request).await.into()) | ||
} | ||
/// Handle GraphQL Requests | ||
pub async fn resolve( | ||
schema: Inject<graphql::Schema>, | ||
users: Inject<Box<dyn users::Service>>, | ||
sub: Subject, | ||
req: GraphQLRequest, | ||
) -> GraphQLResponse { | ||
// Retrieve the request User, if username is present | ||
let user = if let Subject(Some(ref username)) = sub { | ||
users.get_by_username(username, &true).await.unwrap_or(None) | ||
} else { | ||
None | ||
}; | ||
|
||
// Add the Subject and optional User to the context | ||
let request = req.into_inner().data(sub).data(user); | ||
|
||
schema.execute(request).await.into() | ||
} | ||
|
||
/// Events Provider | ||
#[derive(Default)] | ||
pub struct Provide {} | ||
|
||
#[Provider] | ||
#[async_trait] | ||
impl Provider<Controller> for Provide { | ||
async fn provide(self: Arc<Self>, i: Inject) -> provider::Result<Arc<Controller>> { | ||
let users = i.get::<Box<dyn users::Service>>().await?; | ||
let schema = i.get::<graphql::Schema>().await?; | ||
|
||
Ok(Arc::new(Controller { users, schema })) | ||
} | ||
/// Handle GraphiQL UI Requests | ||
pub async fn graphiql() -> impl IntoResponse { | ||
response::Html(GraphiQLSource::build().endpoint("/").finish()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters