Initial commit, basic graphql server setup
This commit is contained in:
commit
c9f39a3d37
4 changed files with 1647 additions and 0 deletions
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
|
|
@ -0,0 +1 @@
|
||||||
|
/target
|
||||||
1572
Cargo.lock
generated
Normal file
1572
Cargo.lock
generated
Normal file
File diff suppressed because it is too large
Load diff
10
Cargo.toml
Normal file
10
Cargo.toml
Normal file
|
|
@ -0,0 +1,10 @@
|
||||||
|
[package]
|
||||||
|
name = "city_portal"
|
||||||
|
version = "0.1.0"
|
||||||
|
edition = "2024"
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
|
tokio = { version = "1", features = ["full"] }
|
||||||
|
axum = "0.8"
|
||||||
|
async-graphql = "7"
|
||||||
|
async-graphql-axum = "7"
|
||||||
64
src/main.rs
Normal file
64
src/main.rs
Normal file
|
|
@ -0,0 +1,64 @@
|
||||||
|
use async_graphql::{EmptyMutation, EmptySubscription, Object, Schema, SimpleObject};
|
||||||
|
use async_graphql_axum::{GraphQLRequest, GraphQLResponse};
|
||||||
|
use axum::{
|
||||||
|
Router,
|
||||||
|
routing::{get, post},
|
||||||
|
};
|
||||||
|
use std::net::SocketAddr;
|
||||||
|
|
||||||
|
struct QueryRoot;
|
||||||
|
|
||||||
|
#[Object]
|
||||||
|
impl QueryRoot {
|
||||||
|
async fn hello(&self) -> &str {
|
||||||
|
"Hello, world!"
|
||||||
|
}
|
||||||
|
async fn testing(&self) -> &str {
|
||||||
|
"Testing"
|
||||||
|
}
|
||||||
|
async fn user(&self, id: i32) -> User {
|
||||||
|
User {
|
||||||
|
id,
|
||||||
|
name: String::from("Zeph"),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(SimpleObject)]
|
||||||
|
struct User {
|
||||||
|
id: i32,
|
||||||
|
name: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[tokio::main]
|
||||||
|
async fn main() {
|
||||||
|
let schema = Schema::build(QueryRoot, EmptyMutation, EmptySubscription).finish();
|
||||||
|
|
||||||
|
let app = Router::new()
|
||||||
|
.route(
|
||||||
|
"/graphql",
|
||||||
|
post({
|
||||||
|
let schema = schema.clone();
|
||||||
|
move |req: GraphQLRequest| async move {
|
||||||
|
GraphQLResponse::from(schema.execute(req.into_inner()).await)
|
||||||
|
}
|
||||||
|
}),
|
||||||
|
)
|
||||||
|
.route(
|
||||||
|
"/graphql",
|
||||||
|
get({
|
||||||
|
let schema = schema.clone();
|
||||||
|
move || async move { axum::Json(schema.sdl()) }
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
|
||||||
|
let addr = SocketAddr::from(([0, 0, 0, 0], 3000));
|
||||||
|
println!("listening on http://{}", addr);
|
||||||
|
|
||||||
|
axum::serve(
|
||||||
|
tokio::net::TcpListener::bind(addr).await.unwrap(),
|
||||||
|
app.into_make_service(),
|
||||||
|
)
|
||||||
|
.await
|
||||||
|
.unwrap();
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue