Skip to content

Commit

Permalink
Update examples
Browse files Browse the repository at this point in the history
  • Loading branch information
sunli829 committed Nov 13, 2021
1 parent 28931e5 commit aef1433
Show file tree
Hide file tree
Showing 27 changed files with 98 additions and 826 deletions.
6 changes: 0 additions & 6 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,6 @@ members = [
"federation/federation-products",
"federation/federation-reviews",

"tide/starwars",
"tide/token-from-header",
"tide/dataloader",
"tide/dataloader-postgres",
"tide/subscription",

"rocket/starwars",
"rocket/upload",

Expand Down
2 changes: 1 addition & 1 deletion actix-web/error-extensions/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ edition = "2021"
[dependencies]
async-graphql = { path = "../../.." }
async-graphql-actix-web = { path = "../../../integrations/actix-web" }
actix-web = { version = "4.0.0-beta.8", default-features = false }
actix-web = { version = "4.0.0-beta.10", default-features = false }
thiserror = "1.0"
serde_json = "1.0"
6 changes: 3 additions & 3 deletions actix-web/error-extensions/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use async_graphql::{
EmptyMutation, EmptySubscription, ErrorExtensions, FieldError, FieldResult, Object, ResultExt,
Schema,
};
use async_graphql_actix_web::{Request, Response};
use async_graphql_actix_web::{GraphQLRequest, GraphQLResponse};

#[derive(Debug, Error)]
pub enum MyError {
Expand Down Expand Up @@ -95,8 +95,8 @@ impl QueryRoot {

async fn index(
schema: web::Data<Schema<QueryRoot, EmptyMutation, EmptySubscription>>,
req: Request,
) -> Response {
req: GraphQLRequest,
) -> GraphQLResponse {
schema.execute(req.into_inner()).await.into()
}

Expand Down
2 changes: 1 addition & 1 deletion actix-web/starwars/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@ edition = "2021"
[dependencies]
async-graphql = { path = "../../.." }
async-graphql-actix-web = { path = "../../../integrations/actix-web" }
actix-web = { version = "4.0.0-beta.8", default-features = false }
actix-web = { version = "4.0.0-beta.10", default-features = false }
starwars = { path = "../../models/starwars" }
4 changes: 2 additions & 2 deletions actix-web/starwars/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ use actix_web::web::Data;
use actix_web::{guard, web, App, HttpResponse, HttpServer, Result};
use async_graphql::http::{playground_source, GraphQLPlaygroundConfig};
use async_graphql::{EmptyMutation, EmptySubscription, Schema};
use async_graphql_actix_web::{Request, Response};
use async_graphql_actix_web::{GraphQLRequest, GraphQLResponse};
use starwars::{QueryRoot, StarWars, StarWarsSchema};

async fn index(schema: web::Data<StarWarsSchema>, req: Request) -> Response {
async fn index(schema: web::Data<StarWarsSchema>, req: GraphQLRequest) -> GraphQLResponse {
schema.execute(req.into_inner()).await.into()
}

Expand Down
3 changes: 1 addition & 2 deletions actix-web/subscription/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,5 @@ edition = "2021"
[dependencies]
async-graphql = { path = "../../.." }
async-graphql-actix-web = { path = "../../../integrations/actix-web" }
actix-web = { version = "4.0.0-beta.8", default-features = false }
actix-web-actors = "3.0.0"
actix-web = { version = "4.0.0-beta.10", default-features = false }
books = { path = "../../models/books" }
6 changes: 3 additions & 3 deletions actix-web/subscription/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ use actix_web::web::Data;
use actix_web::{guard, web, App, HttpRequest, HttpResponse, HttpServer, Result};
use async_graphql::http::{playground_source, GraphQLPlaygroundConfig};
use async_graphql::Schema;
use async_graphql_actix_web::{Request, Response, WSSubscription};
use async_graphql_actix_web::{GraphQLRequest, GraphQLResponse, GraphQLSubscription};
use books::{BooksSchema, MutationRoot, QueryRoot, Storage, SubscriptionRoot};

async fn index(schema: web::Data<BooksSchema>, req: Request) -> Response {
async fn index(schema: web::Data<BooksSchema>, req: GraphQLRequest) -> GraphQLResponse {
schema.execute(req.into_inner()).await.into()
}

Expand All @@ -22,7 +22,7 @@ async fn index_ws(
req: HttpRequest,
payload: web::Payload,
) -> Result<HttpResponse> {
WSSubscription::start(Schema::clone(&*schema), &req, payload)
GraphQLSubscription::new(Schema::clone(&*schema)).start(&req, payload)
}

#[actix_web::main]
Expand Down
3 changes: 1 addition & 2 deletions actix-web/token-from-header/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,8 @@ edition = "2021"
[dependencies]
async-graphql = { path = "../../.." }
async-graphql-actix-web = { path = "../../../integrations/actix-web" }
actix-web = { version = "4.0.0-beta.8", default-features = false }
actix-web = { version = "4.0.0-beta.10", default-features = false }
futures = "0.3"
actix-web-actors = "3.0.0"
serde_json = "1.0"
serde_derive = "1.0"
serde = "1.0"
38 changes: 22 additions & 16 deletions actix-web/token-from-header/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use actix_web::{guard, web, App, HttpRequest, HttpResponse, HttpServer, Result};
use async_graphql::http::{playground_source, GraphQLPlaygroundConfig};
use async_graphql::{Context, Data, EmptyMutation, Object, Schema, Subscription};
use async_graphql_actix_web::{Request, Response, WSSubscription};
use async_graphql_actix_web::{GraphQLRequest, GraphQLResponse, GraphQLSubscription};
use futures::{stream, Stream};

type MySchema = Schema<QueryRoot, EmptyMutation, SubscriptionRoot>;
Expand Down Expand Up @@ -29,7 +29,11 @@ impl SubscriptionRoot {
}
}

async fn index(schema: web::Data<MySchema>, req: HttpRequest, gql_request: Request) -> Response {
async fn index(
schema: web::Data<MySchema>,
req: HttpRequest,
gql_request: GraphQLRequest,
) -> GraphQLResponse {
let token = req
.headers()
.get("Token")
Expand All @@ -54,20 +58,22 @@ async fn index_ws(
req: HttpRequest,
payload: web::Payload,
) -> Result<HttpResponse> {
WSSubscription::start_with_initializer(Schema::clone(&*schema), &req, payload, |value| async {
#[derive(serde_derive::Deserialize)]
struct Payload {
token: String,
}
GraphQLSubscription::new(Schema::clone(&*schema))
.on_connection_init(|value| async {
#[derive(serde_derive::Deserialize)]
struct Payload {
token: String,
}

if let Ok(payload) = serde_json::from_value::<Payload>(value) {
let mut data = Data::default();
data.insert(MyToken(payload.token));
Ok(data)
} else {
Err("Token is required".into())
}
})
if let Ok(payload) = serde_json::from_value::<Payload>(value) {
let mut data = Data::default();
data.insert(MyToken(payload.token));
Ok(data)
} else {
Err("Token is required".into())
}
})
.start(&req, payload)
}

#[actix_web::main]
Expand All @@ -78,7 +84,7 @@ async fn main() -> std::io::Result<()> {

HttpServer::new(move || {
App::new()
.app_data(Data::new(schema.clone()))
.app_data(web::Data::new(schema.clone()))
.service(web::resource("/").guard(guard::Post()).to(index))
.service(
web::resource("/")
Expand Down
2 changes: 1 addition & 1 deletion actix-web/upload/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@ edition = "2021"
[dependencies]
async-graphql = { path = "../../.." }
async-graphql-actix-web = { path = "../../../integrations/actix-web" }
actix-web = { version = "4.0.0-beta.8", default-features = false }
actix-web = { version = "4.0.0-beta.10", default-features = false }
files = { path = "../../models/files" }
4 changes: 2 additions & 2 deletions actix-web/upload/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ use actix_web::web::Data;
use actix_web::{guard, web, App, HttpResponse, HttpServer};
use async_graphql::http::{playground_source, GraphQLPlaygroundConfig, MultipartOptions};
use async_graphql::{EmptySubscription, Schema};
use async_graphql_actix_web::{Request, Response};
use async_graphql_actix_web::{GraphQLRequest, GraphQLResponse};
use files::{FilesSchema, MutationRoot, QueryRoot, Storage};

async fn index(schema: web::Data<FilesSchema>, req: Request) -> Response {
async fn index(schema: web::Data<FilesSchema>, req: GraphQLRequest) -> GraphQLResponse {
schema.execute(req.into_inner()).await.into()
}

Expand Down
22 changes: 4 additions & 18 deletions axum/subscription/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
use async_graphql::http::{playground_source, GraphQLPlaygroundConfig, ALL_WEBSOCKET_PROTOCOLS};
use async_graphql::http::{playground_source, GraphQLPlaygroundConfig};
use async_graphql::Schema;
use async_graphql_axum::{
graphql_subscription, GraphQLRequest, GraphQLResponse, SecWebsocketProtocol,
};
use axum::extract::{self, ws::WebSocketUpgrade, TypedHeader};
use async_graphql_axum::{GraphQLRequest, GraphQLResponse, GraphQLSubscription};
use axum::response::{self, IntoResponse};
use axum::routing::get;
use axum::{AddExtensionLayer, Router, Server};
use axum::{extract, AddExtensionLayer, Router, Server};
use books::{BooksSchema, MutationRoot, QueryRoot, Storage, SubscriptionRoot};

async fn graphql_handler(
Expand All @@ -16,17 +13,6 @@ async fn graphql_handler(
schema.execute(req.into_inner()).await.into()
}

async fn graphql_subscription_handler(
ws: WebSocketUpgrade,
schema: extract::Extension<BooksSchema>,
protocol: TypedHeader<SecWebsocketProtocol>,
) -> impl IntoResponse {
ws.protocols(ALL_WEBSOCKET_PROTOCOLS)
.on_upgrade(move |socket| async move {
graphql_subscription(socket, schema.0.clone(), protocol.0).await
})
}

async fn graphql_playground() -> impl IntoResponse {
response::Html(playground_source(
GraphQLPlaygroundConfig::new("/").subscription_endpoint("/ws"),
Expand All @@ -41,7 +27,7 @@ async fn main() {

let app = Router::new()
.route("/", get(graphql_playground).post(graphql_handler))
.route("/ws", get(graphql_subscription_handler))
.route("/ws", GraphQLSubscription::new(schema.clone()))
.layer(AddExtensionLayer::new(schema));

println!("Playground: http://localhost:8000");
Expand Down
9 changes: 6 additions & 3 deletions rocket/starwars/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use async_graphql::{
http::{playground_source, GraphQLPlaygroundConfig},
EmptyMutation, EmptySubscription, Schema,
};
use async_graphql_rocket::{Query, Request, Response};
use async_graphql_rocket::{GraphQLQuery, GraphQLRequest, GraphQLResponse};
use rocket::{response::content, routes, State};
use starwars::{QueryRoot, StarWars};

Expand All @@ -14,12 +14,15 @@ fn graphql_playground() -> content::Html<String> {
}

#[rocket::get("/graphql?<query..>")]
async fn graphql_query(schema: &State<StarWarsSchema>, query: Query) -> Response {
async fn graphql_query(schema: &State<StarWarsSchema>, query: GraphQLQuery) -> GraphQLResponse {
query.execute(schema).await
}

#[rocket::post("/graphql", data = "<request>", format = "application/json")]
async fn graphql_request(schema: &State<StarWarsSchema>, request: Request) -> Response {
async fn graphql_request(
schema: &State<StarWarsSchema>,
request: GraphQLRequest,
) -> GraphQLResponse {
request.execute(schema).await
}

Expand Down
11 changes: 7 additions & 4 deletions rocket/upload/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use async_graphql::{
http::{playground_source, GraphQLPlaygroundConfig},
EmptyMutation, EmptySubscription, Schema,
};
use async_graphql_rocket::{Query, Request, Response};
use async_graphql_rocket::{GraphQLQuery, GraphQLRequest, GraphQLResponse};
use files::{FilesSchema, MutationRoot, QueryRoot, Storage};
use rocket::{response::content, routes, State};

Expand All @@ -14,12 +14,12 @@ fn graphql_playground() -> content::Html<String> {
}

#[rocket::get("/graphql?<query..>")]
async fn graphql_query(schema: &State<FilesSchema>, query: Query) -> Response {
async fn graphql_query(schema: &State<FilesSchema>, query: GraphQLQuery) -> GraphQLResponse {
query.execute(schema).await
}

#[rocket::post("/graphql", data = "<request>", format = "application/json", rank = 1)]
async fn graphql_request(schema: &State<FilesSchema>, request: Request) -> Response {
async fn graphql_request(schema: &State<FilesSchema>, request: GraphQLRequest) -> GraphQLResponse {
request.execute(schema).await
}

Expand All @@ -29,7 +29,10 @@ async fn graphql_request(schema: &State<FilesSchema>, request: Request) -> Respo
format = "multipart/form-data",
rank = 2
)]
async fn graphql_request_multipart(schema: &State<FilesSchema>, request: Request) -> Response {
async fn graphql_request_multipart(
schema: &State<FilesSchema>,
request: GraphQLRequest,
) -> GraphQLResponse {
request.execute(schema).await
}

Expand Down
20 changes: 0 additions & 20 deletions tide/dataloader-postgres/Cargo.toml

This file was deleted.

Loading

0 comments on commit aef1433

Please sign in to comment.