Skip to content

Commit

Permalink
Move examples/actix_subscriptions into `juniper_actix/examples/subs…
Browse files Browse the repository at this point in the history
…cription`
  • Loading branch information
tyranron committed Sep 21, 2023
1 parent d46c59a commit 0de7455
Show file tree
Hide file tree
Showing 8 changed files with 41 additions and 209 deletions.
1 change: 0 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,6 @@ jobs:
fail-fast: false
matrix:
example:
- actix_subscriptions
- basic_subscriptions
os:
- ubuntu
Expand Down
1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
resolver = "1" # unifying Cargo features asap for Book tests
members = [
"benches",
"examples/actix_subscriptions",
"examples/basic_subscriptions",
"juniper_codegen",
"juniper",
Expand Down
19 changes: 0 additions & 19 deletions examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,28 +16,9 @@ cargo run --example <example_name>
Where `<example_name>` is one of the following workspace members:

```
actix_subscriptions
basic_subscriptions
```

e.g. to run the `actix_subscriptions` example:

```bash
cargo run --example actix_subscriptions
```

You can also run an example directly from an `examples` workspace directory. To
run the `actix_subscriptions` example:

```bash
cd examples/actix_subscriptions
cargo run
Finished dev [unoptimized + debuginfo] target(s) in 0.13s
Running `/path/to/repo/juniper/target/debug/example_actix_subscriptions`
[2022-11-20T07:46:08Z INFO actix_server::builder] Starting 10 workers
[2022-11-20T07:46:08Z INFO actix_server::server] Actix runtime found; starting in Actix runtime
```

Note if you want to run the code within your own project, you need to change
the relative paths in `Cargo.toml`, e.g:

Expand Down
21 changes: 0 additions & 21 deletions examples/actix_subscriptions/Cargo.toml

This file was deleted.

5 changes: 5 additions & 0 deletions juniper_actix/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,9 @@ bytes = "1.0"
env_logger = "0.10"
juniper = { version = "0.16.0-dev", path = "../juniper", features = ["expose-test-schema"] }
log = "0.4"
rand = "0.8"
tokio = "1.0"

[[example]]
name = "subscription"
required-features = ["subscriptions"]
141 changes: 0 additions & 141 deletions juniper_actix/examples/actix_server.rs

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
//! This example demonstrates asynchronous subscriptions with [`actix_web`].
use std::{env, pin::Pin, time::Duration};

use actix_cors::Cors;
use actix_web::{
http::header,
middleware,
web::{self, Data},
App, Error, HttpRequest, HttpResponse, HttpServer,
App, Error, HttpRequest, HttpResponse, HttpServer, Responder,
};

use juniper::{
Expand All @@ -31,14 +33,40 @@ async fn graphiql() -> Result<HttpResponse, Error> {
}

async fn graphql(
req: actix_web::HttpRequest,
payload: actix_web::web::Payload,
schema: web::Data<Schema>,
req: HttpRequest,
payload: web::Payload,
schema: Data<Schema>,
) -> Result<HttpResponse, Error> {
let context = Database::new();
graphql_handler(&schema, &context, req, payload).await
}

async fn homepage() -> impl Responder {
HttpResponse::Ok()
.insert_header(("content-type", "text/html"))
.message_body(
"<html><h1>juniper_actix/subscription example</h1>\
<div>visit <a href=\"/graphiql\">GraphiQL</a></div>\
<div>visit <a href=\"/playground\">GraphQL Playground</a></div>\
</html>",
)
}

async fn subscriptions(
req: HttpRequest,
stream: web::Payload,
schema: web::Data<Schema>,
) -> Result<HttpResponse, Error> {
let context = Database::new();
let schema = schema.into_inner();
let config = ConnectionConfig::new(context);
// set the keep alive interval to 15 secs so that it doesn't timeout in playground
// playground has a hard-coded timeout set to 20 secs
let config = config.with_keep_alive_interval(Duration::from_secs(15));

subscriptions::ws_handler(req, stream, schema, config).await
}

struct Subscription;

#[derive(GraphQLObject)]
Expand All @@ -53,7 +81,8 @@ type RandomHumanStream =
#[graphql_subscription(context = Database)]
impl Subscription {
#[graphql(
description = "A random humanoid creature in the Star Wars universe every 3 seconds. Second result will be an error."
description = "A random humanoid creature in the Star Wars universe every 3 seconds. \
Second result will be an error."
)]
async fn random_human(context: &Database) -> RandomHumanStream {
let mut counter = 0;
Expand Down Expand Up @@ -88,21 +117,6 @@ impl Subscription {
}
}

async fn subscriptions(
req: HttpRequest,
stream: web::Payload,
schema: web::Data<Schema>,
) -> Result<HttpResponse, Error> {
let context = Database::new();
let schema = schema.into_inner();
let config = ConnectionConfig::new(context);
// set the keep alive interval to 15 secs so that it doesn't timeout in playground
// playground has a hard-coded timeout set to 20 secs
let config = config.with_keep_alive_interval(Duration::from_secs(15));

subscriptions::ws_handler(req, stream, schema, config).await
}

#[actix_web::main]
async fn main() -> std::io::Result<()> {
env::set_var("RUST_LOG", "info");
Expand Down Expand Up @@ -130,11 +144,7 @@ async fn main() -> std::io::Result<()> {
)
.service(web::resource("/playground").route(web::get().to(playground)))
.service(web::resource("/graphiql").route(web::get().to(graphiql)))
.default_service(web::to(|| async {
HttpResponse::Found()
.append_header((header::LOCATION, "/playground"))
.finish()
}))
.default_service(web::to(homepage))
})
.bind("127.0.0.1:8080")?
.run()
Expand Down
2 changes: 1 addition & 1 deletion juniper_warp/examples/subscription.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//! This example demonstrates asynchronous subscriptions with warp and tokio 0.2
//! This example demonstrates asynchronous subscriptions with [`warp`].
use std::{env, pin::Pin, sync::Arc, time::Duration};

Expand Down

0 comments on commit 0de7455

Please sign in to comment.