Skip to content

Commit db93e2c

Browse files
committed
adding loco Starwars + subscription examples
1 parent 3d30c93 commit db93e2c

File tree

19 files changed

+375
-0
lines changed

19 files changed

+375
-0
lines changed

Diff for: README.md

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ A git submodule that shows example async-graphql projects.
1111
- [tide] Examples for `tide`
1212
- [rocket] Examples for `rocket`
1313
- [axum] Examples for `axum`
14+
- [loco] Examples for `loco`
1415
- [federation] Examples for [Apollo Federation](https://www.apollographql.com/docs/federation/)
1516

1617

Diff for: loco/starwars/.cargo/config.toml

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[alias]
2+
loco = "run --"

Diff for: loco/starwars/Cargo.toml

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
[workspace]
2+
3+
[package]
4+
name = "loco-starwars"
5+
version = "0.1.0"
6+
edition = "2021"
7+
8+
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
9+
10+
[dependencies]
11+
loco-rs = { version = "0.6.0", default-features = false, features = ["cli"] }
12+
eyre = "*"
13+
tokio = { version = "1.33.0", default-features = false }
14+
async-trait = "0.1.74"
15+
16+
axum = "0.7.1"
17+
18+
# async-graphql dependencies
19+
async-graphql = { path = "../../.." }
20+
async-graphql-axum = { path = "../../../integrations/axum" }
21+
starwars = { path = "../../models/starwars" }
22+
23+
[[bin]]
24+
name = "starwars-cli"
25+
path = "src/bin/main.rs"
26+
required-features = []

Diff for: loco/starwars/README.md

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# async-graphql with Loco :train:
2+
3+
Example async-graphql project with [Loco](https://github.com/loco-rs/loco).
4+
5+
## Quick Start
6+
7+
Start your app:
8+
9+
```
10+
$ cargo loco start
11+
Finished dev [unoptimized + debuginfo] target(s) in 21.63s
12+
Running `target/debug/myapp start`
13+
14+
:
15+
:
16+
:
17+
18+
controller/app_routes.rs:203: [Middleware] Adding log trace id
19+
20+
▄ ▀
21+
▀ ▄
22+
▄ ▀ ▄ ▄ ▄▀
23+
▄ ▀▄▄
24+
▄ ▀ ▀ ▀▄▀█▄
25+
▀█▄
26+
▄▄▄▄▄▄▄ ▄▄▄▄▄▄▄▄▄ ▄▄▄▄▄▄▄▄▄▄▄ ▄▄▄▄▄▄▄▄▄ ▀▀█
27+
██████ █████ ███ █████ ███ █████ ███ ▀█
28+
██████ █████ ███ █████ ▀▀▀ █████ ███ ▄█▄
29+
██████ █████ ███ █████ █████ ███ ████▄
30+
██████ █████ ███ █████ ▄▄▄ █████ ███ █████
31+
██████ █████ ███ ████ ███ █████ ███ ████▀
32+
▀▀▀██▄ ▀▀▀▀▀▀▀▀▀▀ ▀▀▀▀▀▀▀▀▀▀ ▀▀▀▀▀▀▀▀▀▀ ██▀
33+
▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀
34+
35+
started on port 5150
36+
```

Diff for: loco/starwars/config/development.yaml

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# Loco configuration file documentation
2+
3+
# Application logging configuration
4+
logger:
5+
# Enable or disable logging.
6+
enable: true
7+
# Log level, options: trace, debug, info, warn or error.
8+
level: debug
9+
# Define the logging format. options: compact, pretty or json
10+
format: compact
11+
# By default the logger has filtering only logs that came from your code or logs that came from `loco` framework. to see all third party libraries
12+
# Uncomment the line below to override to see all third party libraries you can enable this config and override the logger filters.
13+
# override_filter: trace
14+
15+
# Web server configuration
16+
server:
17+
# Port on which the server will listen. the server binding is 0.0.0.0:{PORT}
18+
port: 5150
19+
# The UI hostname or IP address that mailers will point to.
20+
host: http://localhost
21+
# Out of the box middleware configuration. to disable middleware you can changed the `enable` field to `false` of comment the middleware block
22+
middlewares:
23+
# Enable Etag cache header middleware
24+
etag:
25+
enable: true
26+
# Allows to limit the payload size request. payload that bigger than this file will blocked the request.
27+
limit_payload:
28+
# Enable/Disable the middleware.
29+
enable: true
30+
# the limit size. can be b,kb,kib,mb,mib,gb,gib
31+
body_limit: 5mb
32+
# Generating a unique request ID and enhancing logging with additional information such as the start and completion of request processing, latency, status code, and other request details.
33+
logger:
34+
# Enable/Disable the middleware.
35+
enable: true
36+
# when your code is panicked, the request still returns 500 status code.
37+
catch_panic:
38+
# Enable/Disable the middleware.
39+
enable: true
40+
# Timeout for incoming requests middleware. requests that take more time from the configuration will cute and 408 status code will returned.
41+
timeout_request:
42+
# Enable/Disable the middleware.
43+
enable: false
44+
# Duration time in milliseconds.
45+
timeout: 5000

Diff for: loco/starwars/src/app.rs

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
use async_trait::async_trait;
2+
use loco_rs::{
3+
app::{AppContext, Hooks},
4+
boot::{create_app, BootResult, StartMode},
5+
controller::AppRoutes,
6+
environment::Environment,
7+
task::Tasks,
8+
worker::Processor,
9+
Result,
10+
};
11+
12+
use crate::controllers;
13+
14+
pub struct App;
15+
#[async_trait]
16+
impl Hooks for App {
17+
fn app_name() -> &'static str {
18+
env!("CARGO_CRATE_NAME")
19+
}
20+
21+
fn app_version() -> String {
22+
format!(
23+
"{} ({})",
24+
env!("CARGO_PKG_VERSION"),
25+
option_env!("BUILD_SHA")
26+
.or(option_env!("GITHUB_SHA"))
27+
.unwrap_or("dev")
28+
)
29+
}
30+
31+
async fn boot(mode: StartMode, environment: &Environment) -> Result<BootResult> {
32+
create_app::<Self>(mode, environment).await
33+
}
34+
35+
fn routes(_ctx: &AppContext) -> AppRoutes {
36+
AppRoutes::empty().add_route(controllers::graphiql::routes())
37+
}
38+
39+
fn connect_workers<'a>(_p: &'a mut Processor, _ctx: &'a AppContext) {}
40+
41+
fn register_tasks(_tasks: &mut Tasks) {}
42+
}

Diff for: loco/starwars/src/bin/main.rs

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
use loco_rs::cli;
2+
use loco_starwars::app::App;
3+
4+
#[tokio::main]
5+
async fn main() -> eyre::Result<()> {
6+
cli::main::<App>().await
7+
}

Diff for: loco/starwars/src/controllers/graphiql.rs

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
use async_graphql::{http::GraphiQLSource, EmptyMutation, EmptySubscription, Schema};
2+
use async_graphql_axum::GraphQL;
3+
use axum::debug_handler;
4+
use loco_rs::prelude::*;
5+
use starwars::{QueryRoot, StarWars};
6+
7+
#[debug_handler]
8+
async fn graphiql() -> Result<Response> {
9+
format::html(&GraphiQLSource::build().endpoint("/").finish())
10+
}
11+
12+
pub fn routes() -> Routes {
13+
let schema = Schema::build(QueryRoot, EmptyMutation, EmptySubscription)
14+
.data(StarWars::new())
15+
.finish();
16+
17+
Routes::new().add("/", get(graphiql).post_service(GraphQL::new(schema)))
18+
}

Diff for: loco/starwars/src/controllers/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
pub mod graphiql;

Diff for: loco/starwars/src/lib.rs

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
pub mod app;
2+
pub mod controllers;

Diff for: loco/subscription/.cargo/config.toml

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[alias]
2+
loco = "run --"

Diff for: loco/subscription/Cargo.toml

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
[workspace]
2+
3+
[package]
4+
name = "loco-subscription"
5+
version = "0.1.0"
6+
edition = "2021"
7+
8+
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
9+
10+
[dependencies]
11+
loco-rs = { version = "0.6.0", default-features = false, features = ["cli"] }
12+
eyre = "*"
13+
tokio = { version = "1.33.0", default-features = false }
14+
async-trait = "0.1.74"
15+
axum = "0.7.1"
16+
17+
# async-graphql dependencies
18+
async-graphql = { path = "../../.." }
19+
async-graphql-axum = { path = "../../../integrations/axum" }
20+
books = { path = "../../models/books" }
21+
22+
[[bin]]
23+
name = "starwars-cli"
24+
path = "src/bin/main.rs"
25+
required-features = []

Diff for: loco/subscription/README.md

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# async-graphql with Loco :train:
2+
3+
Example async-graphql project with [Loco](https://github.com/loco-rs/loco).
4+
5+
## Quick Start
6+
7+
Start your app:
8+
9+
```
10+
$ cargo loco start
11+
Finished dev [unoptimized + debuginfo] target(s) in 21.63s
12+
Running `target/debug/myapp start`
13+
14+
:
15+
:
16+
:
17+
18+
controller/app_routes.rs:203: [Middleware] Adding log trace id
19+
20+
▄ ▀
21+
▀ ▄
22+
▄ ▀ ▄ ▄ ▄▀
23+
▄ ▀▄▄
24+
▄ ▀ ▀ ▀▄▀█▄
25+
▀█▄
26+
▄▄▄▄▄▄▄ ▄▄▄▄▄▄▄▄▄ ▄▄▄▄▄▄▄▄▄▄▄ ▄▄▄▄▄▄▄▄▄ ▀▀█
27+
██████ █████ ███ █████ ███ █████ ███ ▀█
28+
██████ █████ ███ █████ ▀▀▀ █████ ███ ▄█▄
29+
██████ █████ ███ █████ █████ ███ ████▄
30+
██████ █████ ███ █████ ▄▄▄ █████ ███ █████
31+
██████ █████ ███ ████ ███ █████ ███ ████▀
32+
▀▀▀██▄ ▀▀▀▀▀▀▀▀▀▀ ▀▀▀▀▀▀▀▀▀▀ ▀▀▀▀▀▀▀▀▀▀ ██▀
33+
▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀
34+
35+
started on port 5150
36+
```

Diff for: loco/subscription/config/development.yaml

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# Loco configuration file documentation
2+
3+
# Application logging configuration
4+
logger:
5+
# Enable or disable logging.
6+
enable: true
7+
# Log level, options: trace, debug, info, warn or error.
8+
level: debug
9+
# Define the logging format. options: compact, pretty or json
10+
format: compact
11+
# By default the logger has filtering only logs that came from your code or logs that came from `loco` framework. to see all third party libraries
12+
# Uncomment the line below to override to see all third party libraries you can enable this config and override the logger filters.
13+
# override_filter: trace
14+
15+
# Web server configuration
16+
server:
17+
# Port on which the server will listen. the server binding is 0.0.0.0:{PORT}
18+
port: 5150
19+
# The UI hostname or IP address that mailers will point to.
20+
host: http://localhost
21+
# Out of the box middleware configuration. to disable middleware you can changed the `enable` field to `false` of comment the middleware block
22+
middlewares:
23+
# Enable Etag cache header middleware
24+
etag:
25+
enable: true
26+
# Allows to limit the payload size request. payload that bigger than this file will blocked the request.
27+
limit_payload:
28+
# Enable/Disable the middleware.
29+
enable: true
30+
# the limit size. can be b,kb,kib,mb,mib,gb,gib
31+
body_limit: 5mb
32+
# Generating a unique request ID and enhancing logging with additional information such as the start and completion of request processing, latency, status code, and other request details.
33+
logger:
34+
# Enable/Disable the middleware.
35+
enable: true
36+
# when your code is panicked, the request still returns 500 status code.
37+
catch_panic:
38+
# Enable/Disable the middleware.
39+
enable: true
40+
# Timeout for incoming requests middleware. requests that take more time from the configuration will cute and 408 status code will returned.
41+
timeout_request:
42+
# Enable/Disable the middleware.
43+
enable: false
44+
# Duration time in milliseconds.
45+
timeout: 5000

Diff for: loco/subscription/src/app.rs

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
use async_graphql::Schema;
2+
use books::{MutationRoot, QueryRoot, Storage, SubscriptionRoot};
3+
4+
use async_graphql_axum::GraphQLSubscription;
5+
use async_trait::async_trait;
6+
use axum::Router as AxumRouter;
7+
use loco_rs::{
8+
app::{AppContext, Hooks},
9+
boot::{create_app, BootResult, StartMode},
10+
controller::AppRoutes,
11+
environment::Environment,
12+
task::Tasks,
13+
worker::Processor,
14+
Result,
15+
};
16+
17+
use crate::controllers;
18+
19+
pub struct App;
20+
#[async_trait]
21+
impl Hooks for App {
22+
fn app_name() -> &'static str {
23+
env!("CARGO_CRATE_NAME")
24+
}
25+
26+
fn app_version() -> String {
27+
format!(
28+
"{} ({})",
29+
env!("CARGO_PKG_VERSION"),
30+
option_env!("BUILD_SHA")
31+
.or(option_env!("GITHUB_SHA"))
32+
.unwrap_or("dev")
33+
)
34+
}
35+
36+
async fn boot(mode: StartMode, environment: &Environment) -> Result<BootResult> {
37+
create_app::<Self>(mode, environment).await
38+
}
39+
40+
fn routes(_ctx: &AppContext) -> AppRoutes {
41+
AppRoutes::empty().add_route(controllers::graphiql::routes())
42+
}
43+
44+
async fn after_routes(router: AxumRouter, _ctx: &AppContext) -> Result<AxumRouter> {
45+
let schema = Schema::build(QueryRoot, MutationRoot, SubscriptionRoot)
46+
.data(Storage::default())
47+
.finish();
48+
Ok(router.route_service("/ws", GraphQLSubscription::new(schema)))
49+
}
50+
51+
fn connect_workers<'a>(_p: &'a mut Processor, _ctx: &'a AppContext) {}
52+
53+
fn register_tasks(_tasks: &mut Tasks) {}
54+
}

Diff for: loco/subscription/src/bin/main.rs

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
use loco_rs::cli;
2+
use loco_subscription::app::App;
3+
4+
#[tokio::main]
5+
async fn main() -> eyre::Result<()> {
6+
cli::main::<App>().await
7+
}

Diff for: loco/subscription/src/controllers/graphiql.rs

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
use async_graphql::{http::GraphiQLSource, Schema};
2+
use async_graphql_axum::GraphQL;
3+
use axum::debug_handler;
4+
use books::{MutationRoot, QueryRoot, Storage, SubscriptionRoot};
5+
use loco_rs::prelude::*;
6+
7+
#[debug_handler]
8+
async fn graphiql() -> Result<Response> {
9+
format::html(
10+
&GraphiQLSource::build()
11+
.endpoint("/")
12+
.subscription_endpoint("/ws")
13+
.finish(),
14+
)
15+
}
16+
17+
pub fn routes() -> Routes {
18+
let schema = Schema::build(QueryRoot, MutationRoot, SubscriptionRoot)
19+
.data(Storage::default())
20+
.finish();
21+
22+
Routes::new().add("/", get(graphiql).post_service(GraphQL::new(schema)))
23+
}

Diff for: loco/subscription/src/controllers/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
pub mod graphiql;

Diff for: loco/subscription/src/lib.rs

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
pub mod app;
2+
pub mod controllers;

0 commit comments

Comments
 (0)