Skip to content

Adding loco Starwars + subscription examples #87

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ A git submodule that shows example async-graphql projects.
- [tide] Examples for `tide`
- [rocket] Examples for `rocket`
- [axum] Examples for `axum`
- [loco] Examples for `loco`
- [federation] Examples for [Apollo Federation](https://www.apollographql.com/docs/federation/)


Expand Down
2 changes: 2 additions & 0 deletions loco/starwars/.cargo/config.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[alias]
loco = "run --"
26 changes: 26 additions & 0 deletions loco/starwars/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
[workspace]

[package]
name = "loco-starwars"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
loco-rs = { version = "0.6.0", default-features = false, features = ["cli"] }
eyre = "*"
tokio = { version = "1.33.0", default-features = false }
async-trait = "0.1.74"

axum = "0.7.1"

# async-graphql dependencies
async-graphql = { path = "../../.." }
async-graphql-axum = { path = "../../../integrations/axum" }
starwars = { path = "../../models/starwars" }

[[bin]]
name = "starwars-cli"
path = "src/bin/main.rs"
required-features = []
36 changes: 36 additions & 0 deletions loco/starwars/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# async-graphql with Loco :train:

Example async-graphql project with [Loco](https://github.com/loco-rs/loco).

## Quick Start

Start your app:

```
$ cargo loco start
Finished dev [unoptimized + debuginfo] target(s) in 21.63s
Running `target/debug/myapp start`

:
:
:

controller/app_routes.rs:203: [Middleware] Adding log trace id

▄ ▀
▀ ▄
▄ ▀ ▄ ▄ ▄▀
▄ ▀▄▄
▄ ▀ ▀ ▀▄▀█▄
▀█▄
▄▄▄▄▄▄▄ ▄▄▄▄▄▄▄▄▄ ▄▄▄▄▄▄▄▄▄▄▄ ▄▄▄▄▄▄▄▄▄ ▀▀█
██████ █████ ███ █████ ███ █████ ███ ▀█
██████ █████ ███ █████ ▀▀▀ █████ ███ ▄█▄
██████ █████ ███ █████ █████ ███ ████▄
██████ █████ ███ █████ ▄▄▄ █████ ███ █████
██████ █████ ███ ████ ███ █████ ███ ████▀
▀▀▀██▄ ▀▀▀▀▀▀▀▀▀▀ ▀▀▀▀▀▀▀▀▀▀ ▀▀▀▀▀▀▀▀▀▀ ██▀
▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀

started on port 5150
```
45 changes: 45 additions & 0 deletions loco/starwars/config/development.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# Loco configuration file documentation

# Application logging configuration
logger:
# Enable or disable logging.
enable: true
# Log level, options: trace, debug, info, warn or error.
level: debug
# Define the logging format. options: compact, pretty or json
format: compact
# 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
# Uncomment the line below to override to see all third party libraries you can enable this config and override the logger filters.
# override_filter: trace

# Web server configuration
server:
# Port on which the server will listen. the server binding is 0.0.0.0:{PORT}
port: 5150
# The UI hostname or IP address that mailers will point to.
host: http://localhost
# Out of the box middleware configuration. to disable middleware you can changed the `enable` field to `false` of comment the middleware block
middlewares:
# Enable Etag cache header middleware
etag:
enable: true
# Allows to limit the payload size request. payload that bigger than this file will blocked the request.
limit_payload:
# Enable/Disable the middleware.
enable: true
# the limit size. can be b,kb,kib,mb,mib,gb,gib
body_limit: 5mb
# 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.
logger:
# Enable/Disable the middleware.
enable: true
# when your code is panicked, the request still returns 500 status code.
catch_panic:
# Enable/Disable the middleware.
enable: true
# Timeout for incoming requests middleware. requests that take more time from the configuration will cute and 408 status code will returned.
timeout_request:
# Enable/Disable the middleware.
enable: false
# Duration time in milliseconds.
timeout: 5000
42 changes: 42 additions & 0 deletions loco/starwars/src/app.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
use async_trait::async_trait;
use loco_rs::{
app::{AppContext, Hooks},
boot::{create_app, BootResult, StartMode},
controller::AppRoutes,
environment::Environment,
task::Tasks,
worker::Processor,
Result,
};

use crate::controllers;

pub struct App;
#[async_trait]
impl Hooks for App {
fn app_name() -> &'static str {
env!("CARGO_CRATE_NAME")
}

fn app_version() -> String {
format!(
"{} ({})",
env!("CARGO_PKG_VERSION"),
option_env!("BUILD_SHA")
.or(option_env!("GITHUB_SHA"))
.unwrap_or("dev")
)
}

async fn boot(mode: StartMode, environment: &Environment) -> Result<BootResult> {
create_app::<Self>(mode, environment).await
}

fn routes(_ctx: &AppContext) -> AppRoutes {
AppRoutes::empty().add_route(controllers::graphiql::routes())
}

fn connect_workers<'a>(_p: &'a mut Processor, _ctx: &'a AppContext) {}

fn register_tasks(_tasks: &mut Tasks) {}
}
7 changes: 7 additions & 0 deletions loco/starwars/src/bin/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
use loco_rs::cli;
use loco_starwars::app::App;

#[tokio::main]
async fn main() -> eyre::Result<()> {
cli::main::<App>().await
}
18 changes: 18 additions & 0 deletions loco/starwars/src/controllers/graphiql.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
use async_graphql::{http::GraphiQLSource, EmptyMutation, EmptySubscription, Schema};
use async_graphql_axum::GraphQL;
use axum::debug_handler;
use loco_rs::prelude::*;
use starwars::{QueryRoot, StarWars};

#[debug_handler]
async fn graphiql() -> Result<Response> {
format::html(&GraphiQLSource::build().endpoint("/").finish())
}

pub fn routes() -> Routes {
let schema = Schema::build(QueryRoot, EmptyMutation, EmptySubscription)
.data(StarWars::new())
.finish();

Routes::new().add("/", get(graphiql).post_service(GraphQL::new(schema)))
}
1 change: 1 addition & 0 deletions loco/starwars/src/controllers/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub mod graphiql;
2 changes: 2 additions & 0 deletions loco/starwars/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
pub mod app;
pub mod controllers;
2 changes: 2 additions & 0 deletions loco/subscription/.cargo/config.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[alias]
loco = "run --"
25 changes: 25 additions & 0 deletions loco/subscription/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
[workspace]

[package]
name = "loco-subscription"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
loco-rs = { version = "0.6.0", default-features = false, features = ["cli"] }
eyre = "*"
tokio = { version = "1.33.0", default-features = false }
async-trait = "0.1.74"
axum = "0.7.1"

# async-graphql dependencies
async-graphql = { path = "../../.." }
async-graphql-axum = { path = "../../../integrations/axum" }
books = { path = "../../models/books" }

[[bin]]
name = "starwars-cli"
path = "src/bin/main.rs"
required-features = []
36 changes: 36 additions & 0 deletions loco/subscription/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# async-graphql with Loco :train:

Example async-graphql project with [Loco](https://github.com/loco-rs/loco).

## Quick Start

Start your app:

```
$ cargo loco start
Finished dev [unoptimized + debuginfo] target(s) in 21.63s
Running `target/debug/myapp start`

:
:
:

controller/app_routes.rs:203: [Middleware] Adding log trace id

▄ ▀
▀ ▄
▄ ▀ ▄ ▄ ▄▀
▄ ▀▄▄
▄ ▀ ▀ ▀▄▀█▄
▀█▄
▄▄▄▄▄▄▄ ▄▄▄▄▄▄▄▄▄ ▄▄▄▄▄▄▄▄▄▄▄ ▄▄▄▄▄▄▄▄▄ ▀▀█
██████ █████ ███ █████ ███ █████ ███ ▀█
██████ █████ ███ █████ ▀▀▀ █████ ███ ▄█▄
██████ █████ ███ █████ █████ ███ ████▄
██████ █████ ███ █████ ▄▄▄ █████ ███ █████
██████ █████ ███ ████ ███ █████ ███ ████▀
▀▀▀██▄ ▀▀▀▀▀▀▀▀▀▀ ▀▀▀▀▀▀▀▀▀▀ ▀▀▀▀▀▀▀▀▀▀ ██▀
▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀

started on port 5150
```
45 changes: 45 additions & 0 deletions loco/subscription/config/development.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# Loco configuration file documentation

# Application logging configuration
logger:
# Enable or disable logging.
enable: true
# Log level, options: trace, debug, info, warn or error.
level: debug
# Define the logging format. options: compact, pretty or json
format: compact
# 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
# Uncomment the line below to override to see all third party libraries you can enable this config and override the logger filters.
# override_filter: trace

# Web server configuration
server:
# Port on which the server will listen. the server binding is 0.0.0.0:{PORT}
port: 5150
# The UI hostname or IP address that mailers will point to.
host: http://localhost
# Out of the box middleware configuration. to disable middleware you can changed the `enable` field to `false` of comment the middleware block
middlewares:
# Enable Etag cache header middleware
etag:
enable: true
# Allows to limit the payload size request. payload that bigger than this file will blocked the request.
limit_payload:
# Enable/Disable the middleware.
enable: true
# the limit size. can be b,kb,kib,mb,mib,gb,gib
body_limit: 5mb
# 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.
logger:
# Enable/Disable the middleware.
enable: true
# when your code is panicked, the request still returns 500 status code.
catch_panic:
# Enable/Disable the middleware.
enable: true
# Timeout for incoming requests middleware. requests that take more time from the configuration will cute and 408 status code will returned.
timeout_request:
# Enable/Disable the middleware.
enable: false
# Duration time in milliseconds.
timeout: 5000
54 changes: 54 additions & 0 deletions loco/subscription/src/app.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
use async_graphql::Schema;
use books::{MutationRoot, QueryRoot, Storage, SubscriptionRoot};

use async_graphql_axum::GraphQLSubscription;
use async_trait::async_trait;
use axum::Router as AxumRouter;
use loco_rs::{
app::{AppContext, Hooks},
boot::{create_app, BootResult, StartMode},
controller::AppRoutes,
environment::Environment,
task::Tasks,
worker::Processor,
Result,
};

use crate::controllers;

pub struct App;
#[async_trait]
impl Hooks for App {
fn app_name() -> &'static str {
env!("CARGO_CRATE_NAME")
}

fn app_version() -> String {
format!(
"{} ({})",
env!("CARGO_PKG_VERSION"),
option_env!("BUILD_SHA")
.or(option_env!("GITHUB_SHA"))
.unwrap_or("dev")
)
}

async fn boot(mode: StartMode, environment: &Environment) -> Result<BootResult> {
create_app::<Self>(mode, environment).await
}

fn routes(_ctx: &AppContext) -> AppRoutes {
AppRoutes::empty().add_route(controllers::graphiql::routes())
}

async fn after_routes(router: AxumRouter, _ctx: &AppContext) -> Result<AxumRouter> {
let schema = Schema::build(QueryRoot, MutationRoot, SubscriptionRoot)
.data(Storage::default())
.finish();
Ok(router.route_service("/ws", GraphQLSubscription::new(schema)))
}

fn connect_workers<'a>(_p: &'a mut Processor, _ctx: &'a AppContext) {}

fn register_tasks(_tasks: &mut Tasks) {}
}
7 changes: 7 additions & 0 deletions loco/subscription/src/bin/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
use loco_rs::cli;
use loco_subscription::app::App;

#[tokio::main]
async fn main() -> eyre::Result<()> {
cli::main::<App>().await
}
23 changes: 23 additions & 0 deletions loco/subscription/src/controllers/graphiql.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
use async_graphql::{http::GraphiQLSource, Schema};
use async_graphql_axum::GraphQL;
use axum::debug_handler;
use books::{MutationRoot, QueryRoot, Storage, SubscriptionRoot};
use loco_rs::prelude::*;

#[debug_handler]
async fn graphiql() -> Result<Response> {
format::html(
&GraphiQLSource::build()
.endpoint("/")
.subscription_endpoint("/ws")
.finish(),
)
}

pub fn routes() -> Routes {
let schema = Schema::build(QueryRoot, MutationRoot, SubscriptionRoot)
.data(Storage::default())
.finish();

Routes::new().add("/", get(graphiql).post_service(GraphQL::new(schema)))
}
1 change: 1 addition & 0 deletions loco/subscription/src/controllers/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub mod graphiql;
2 changes: 2 additions & 0 deletions loco/subscription/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
pub mod app;
pub mod controllers;