-
Notifications
You must be signed in to change notification settings - Fork 145
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add hello-world graphql server implementation
- Loading branch information
Showing
5 changed files
with
166 additions
and
3 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,6 +1,13 @@ | ||
rust_binary( | ||
name = "main", | ||
name = "api-keys", | ||
srcs = glob(["src/**/*.rs"]), | ||
crate_root = "src/main.rs", | ||
edition = "2021", | ||
) | ||
|
||
rust_binary( | ||
name = "write-sdl", | ||
srcs = glob(["src/**/*.rs"]), | ||
crate_root = "src/bin/write_sdl.rs", | ||
edition = "2021", | ||
) |
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,3 +1,49 @@ | ||
fn main() { | ||
println!("Hello, world!"); | ||
use async_graphql::{EmptyMutation, EmptySubscription, Object, Schema}; | ||
use async_graphql_axum::{GraphQLRequest, GraphQLResponse}; | ||
use axum::{routing::get, Extension, Router}; | ||
|
||
#[tokio::main] | ||
async fn main() { | ||
// Create a GraphQL schema with a simple query | ||
let schema = Schema::build(QueryRoot, EmptyMutation, EmptySubscription).finish(); | ||
|
||
// Create an axum router | ||
let app = Router::new() | ||
.route( | ||
"/graphql", | ||
get(playground).post(axum::routing::post(graphql_handler)), | ||
) | ||
.layer(Extension(schema)); | ||
|
||
// Run the server | ||
axum::Server::bind(&"0.0.0.0:8000".parse().unwrap()) | ||
.serve(app.into_make_service()) | ||
.await | ||
.unwrap(); | ||
} | ||
|
||
// GraphQL handler | ||
async fn graphql_handler( | ||
schema: Extension<Schema<QueryRoot, EmptyMutation, EmptySubscription>>, | ||
req: GraphQLRequest, | ||
) -> GraphQLResponse { | ||
schema.execute(req.into_inner()).await.into() | ||
} | ||
|
||
// Playground handler | ||
async fn playground() -> impl axum::response::IntoResponse { | ||
axum::response::Html(async_graphql::http::playground_source( | ||
async_graphql::http::GraphQLPlaygroundConfig::new("/graphql"), | ||
)) | ||
} | ||
|
||
// Define a simple query object | ||
#[derive(Default)] | ||
struct QueryRoot; | ||
|
||
#[Object] | ||
impl QueryRoot { | ||
async fn hello_world(&self) -> &str { | ||
"Hello, world!" | ||
} | ||
} |
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