Skip to content

Commit

Permalink
Merge pull request #57 from bvanneerven/graphiql-update
Browse files Browse the repository at this point in the history
Update Playground to GraphiQL v2
  • Loading branch information
sunli829 committed Sep 1, 2022
2 parents 5846ade + b41f8f9 commit 0c17521
Show file tree
Hide file tree
Showing 22 changed files with 187 additions and 190 deletions.
13 changes: 8 additions & 5 deletions actix-web/error-extensions/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@ extern crate thiserror;

use actix_web::{guard, web, web::Data, App, HttpResponse, HttpServer};
use async_graphql::{
http::{playground_source, GraphQLPlaygroundConfig},
EmptyMutation, EmptySubscription, ErrorExtensions, FieldError, FieldResult, Object, ResultExt,
Schema,
http::GraphiQLSource, EmptyMutation, EmptySubscription, ErrorExtensions, FieldError,
FieldResult, Object, ResultExt, Schema,
};
use async_graphql_actix_web::{GraphQLRequest, GraphQLResponse};

Expand Down Expand Up @@ -102,12 +101,16 @@ async fn index(
async fn gql_playgound() -> HttpResponse {
HttpResponse::Ok()
.content_type("text/html; charset=utf-8")
.body(playground_source(GraphQLPlaygroundConfig::new("/")))
.body(
GraphiQLSource::build()
.endpoint("http://localhost:8000")
.finish(),
)
}

#[actix_web::main]
async fn main() -> std::io::Result<()> {
println!("Playground: http://localhost:8000");
println!("GraphiQL IDE: http://localhost:8000");

HttpServer::new(move || {
App::new()
Expand Down
18 changes: 9 additions & 9 deletions actix-web/starwars/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
use actix_web::{guard, web, web::Data, App, HttpResponse, HttpServer, Result};
use async_graphql::{
http::{playground_source, GraphQLPlaygroundConfig},
EmptyMutation, EmptySubscription, Schema,
};
use async_graphql::{http::GraphiQLSource, EmptyMutation, EmptySubscription, Schema};
use async_graphql_actix_web::{GraphQLRequest, GraphQLResponse};
use starwars::{QueryRoot, StarWars, StarWarsSchema};

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

async fn index_playground() -> Result<HttpResponse> {
let source = playground_source(GraphQLPlaygroundConfig::new("/").subscription_endpoint("/"));
async fn index_graphiql() -> Result<HttpResponse> {
Ok(HttpResponse::Ok()
.content_type("text/html; charset=utf-8")
.body(source))
.body(
GraphiQLSource::build()
.endpoint("http://localhost:8000")
.finish(),
))
}

#[actix_web::main]
Expand All @@ -23,13 +23,13 @@ async fn main() -> std::io::Result<()> {
.data(StarWars::new())
.finish();

println!("Playground: http://localhost:8000");
println!("GraphiQL IDE: http://localhost:8000");

HttpServer::new(move || {
App::new()
.app_data(Data::new(schema.clone()))
.service(web::resource("/").guard(guard::Post()).to(index))
.service(web::resource("/").guard(guard::Get()).to(index_playground))
.service(web::resource("/").guard(guard::Get()).to(index_graphiql))
})
.bind("127.0.0.1:8000")?
.run()
Expand Down
20 changes: 10 additions & 10 deletions actix-web/subscription/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
use actix_web::{guard, web, web::Data, App, HttpRequest, HttpResponse, HttpServer, Result};
use async_graphql::{
http::{playground_source, GraphQLPlaygroundConfig},
Schema,
};
use async_graphql::{http::GraphiQLSource, Schema};
use async_graphql_actix_web::{GraphQLRequest, GraphQLResponse, GraphQLSubscription};
use books::{BooksSchema, MutationRoot, QueryRoot, Storage, SubscriptionRoot};

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

async fn index_playground() -> Result<HttpResponse> {
async fn index_graphiql() -> Result<HttpResponse> {
Ok(HttpResponse::Ok()
.content_type("text/html; charset=utf-8")
.body(playground_source(
GraphQLPlaygroundConfig::new("/").subscription_endpoint("/"),
)))
.body(
GraphiQLSource::build()
.endpoint("http://localhost:8000")
.subscription_endpoint("ws://localhost:8000")
.finish(),
))
}

async fn index_ws(
Expand All @@ -32,7 +32,7 @@ async fn main() -> std::io::Result<()> {
.data(Storage::default())
.finish();

println!("Playground: http://localhost:8000");
println!("GraphiQL IDE: http://localhost:8000");

HttpServer::new(move || {
App::new()
Expand All @@ -44,7 +44,7 @@ async fn main() -> std::io::Result<()> {
.guard(guard::Header("upgrade", "websocket"))
.to(index_ws),
)
.service(web::resource("/").guard(guard::Get()).to(index_playground))
.service(web::resource("/").guard(guard::Get()).to(index_graphiql))
})
.bind("127.0.0.1:8000")?
.run()
Expand Down
20 changes: 10 additions & 10 deletions actix-web/token-from-header/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
use actix_web::{
guard, http::header::HeaderMap, web, App, HttpRequest, HttpResponse, HttpServer, Result,
};
use async_graphql::{
http::{playground_source, GraphQLPlaygroundConfig},
Data, EmptyMutation, Schema,
};
use async_graphql::{http::GraphiQLSource, Data, EmptyMutation, Schema};
use async_graphql_actix_web::{GraphQLRequest, GraphQLResponse, GraphQLSubscription};
use token::{on_connection_init, QueryRoot, SubscriptionRoot, Token, TokenSchema};

async fn gql_playground() -> HttpResponse {
async fn graphiql() -> HttpResponse {
HttpResponse::Ok()
.content_type("text/html; charset=utf-8")
.body(playground_source(
GraphQLPlaygroundConfig::new("/").subscription_endpoint("/ws"),
))
.body(
GraphiQLSource::build()
.endpoint("http://localhost:8000")
.subscription_endpoint("ws://localhost:8000/ws")
.finish(),
)
}

fn get_token_from_headers(headers: &HeaderMap) -> Option<Token> {
Expand Down Expand Up @@ -54,12 +54,12 @@ async fn index_ws(
async fn main() -> std::io::Result<()> {
let schema = Schema::new(QueryRoot, EmptyMutation, SubscriptionRoot);

println!("Playground: http://localhost:8000");
println!("GraphiQL IDE: http://localhost:8000");

HttpServer::new(move || {
App::new()
.app_data(web::Data::new(schema.clone()))
.service(web::resource("/").guard(guard::Get()).to(gql_playground))
.service(web::resource("/").guard(guard::Get()).to(graphiql))
.service(web::resource("/").guard(guard::Post()).to(index))
.service(web::resource("/ws").to(index_ws))
})
Expand Down
10 changes: 7 additions & 3 deletions actix-web/upload/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use actix_web::{guard, web, web::Data, App, HttpResponse, HttpServer};
use async_graphql::{
http::{playground_source, GraphQLPlaygroundConfig, MultipartOptions},
http::{GraphiQLSource, MultipartOptions},
EmptySubscription, Schema,
};
use async_graphql_actix_web::{GraphQLRequest, GraphQLResponse};
Expand All @@ -13,7 +13,11 @@ async fn index(schema: web::Data<FilesSchema>, req: GraphQLRequest) -> GraphQLRe
async fn gql_playgound() -> HttpResponse {
HttpResponse::Ok()
.content_type("text/html; charset=utf-8")
.body(playground_source(GraphQLPlaygroundConfig::new("/")))
.body(
GraphiQLSource::build()
.endpoint("http://localhost:8000")
.finish(),
)
}

#[actix_web::main]
Expand All @@ -22,7 +26,7 @@ async fn main() -> std::io::Result<()> {
.data(Storage::default())
.finish();

println!("Playground: http://localhost:8000");
println!("GraphiQL IDE: http://localhost:8000");

HttpServer::new(move || {
App::new()
Expand Down
17 changes: 9 additions & 8 deletions axum/starwars/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
use async_graphql::{
http::{playground_source, GraphQLPlaygroundConfig},
EmptyMutation, EmptySubscription, Schema,
};
use async_graphql::{http::GraphiQLSource, EmptyMutation, EmptySubscription, Schema};
use async_graphql_axum::{GraphQLRequest, GraphQLResponse};
use axum::{
extract::Extension,
Expand All @@ -18,8 +15,12 @@ async fn graphql_handler(
schema.execute(req.into_inner()).await.into()
}

async fn graphql_playground() -> impl IntoResponse {
response::Html(playground_source(GraphQLPlaygroundConfig::new("/")))
async fn graphiql() -> impl IntoResponse {
response::Html(
GraphiQLSource::build()
.endpoint("http://localhost:8000")
.finish(),
)
}

#[tokio::main]
Expand All @@ -29,10 +30,10 @@ async fn main() {
.finish();

let app = Router::new()
.route("/", get(graphql_playground).post(graphql_handler))
.route("/", get(graphiql).post(graphql_handler))
.layer(Extension(schema));

println!("Playground: http://localhost:8000");
println!("GraphiQL IDE: http://localhost:8000");

Server::bind(&"0.0.0.0:8000".parse().unwrap())
.serve(app.into_make_service())
Expand Down
20 changes: 10 additions & 10 deletions axum/subscription/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
use async_graphql::{
http::{playground_source, GraphQLPlaygroundConfig},
Schema,
};
use async_graphql::{http::GraphiQLSource, Schema};
use async_graphql_axum::{GraphQLRequest, GraphQLResponse, GraphQLSubscription};
use axum::{
extract::Extension,
Expand All @@ -15,10 +12,13 @@ async fn graphql_handler(schema: Extension<BooksSchema>, req: GraphQLRequest) ->
schema.execute(req.into_inner()).await.into()
}

async fn graphql_playground() -> impl IntoResponse {
response::Html(playground_source(
GraphQLPlaygroundConfig::new("/").subscription_endpoint("/ws"),
))
async fn graphiql() -> impl IntoResponse {
response::Html(
GraphiQLSource::build()
.endpoint("http://localhost:8000")
.subscription_endpoint("ws://localhost:8000/ws")
.finish(),
)
}

#[tokio::main]
Expand All @@ -28,11 +28,11 @@ async fn main() {
.finish();

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

println!("Playground: http://localhost:8000");
println!("GraphiQL IDE: http://localhost:8000");

Server::bind(&"0.0.0.0:8000".parse().unwrap())
.serve(app.into_make_service())
Expand Down
17 changes: 9 additions & 8 deletions axum/upload/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
use async_graphql::{
http::{playground_source, GraphQLPlaygroundConfig},
EmptySubscription, Schema,
};
use async_graphql::{http::GraphiQLSource, EmptySubscription, Schema};
use async_graphql_axum::{GraphQLRequest, GraphQLResponse};
use axum::{
extract::Extension,
Expand All @@ -17,8 +14,12 @@ async fn graphql_handler(schema: Extension<FilesSchema>, req: GraphQLRequest) ->
schema.execute(req.0).await.into()
}

async fn graphql_playground() -> impl IntoResponse {
Html(playground_source(GraphQLPlaygroundConfig::new("/")))
async fn graphiql() -> impl IntoResponse {
Html(
GraphiQLSource::build()
.endpoint("http://localhost:8000")
.finish(),
)
}

#[tokio::main]
Expand All @@ -27,10 +28,10 @@ async fn main() {
.data(Storage::default())
.finish();

println!("Playground: http://localhost:8000");
println!("GraphiQL IDE: http://localhost:8000");

let app = Router::new()
.route("/", get(graphql_playground).post(graphql_handler))
.route("/", get(graphiql).post(graphql_handler))
.layer(Extension(schema))
.layer(
CorsLayer::new()
Expand Down
17 changes: 9 additions & 8 deletions poem/starwars/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
use async_graphql::{
http::{playground_source, GraphQLPlaygroundConfig},
EmptyMutation, EmptySubscription, Schema,
};
use async_graphql::{http::GraphiQLSource, EmptyMutation, EmptySubscription, Schema};
use async_graphql_poem::GraphQL;
use poem::{get, handler, listener::TcpListener, web::Html, IntoResponse, Route, Server};
use starwars::{QueryRoot, StarWars};

#[handler]
async fn graphql_playground() -> impl IntoResponse {
Html(playground_source(GraphQLPlaygroundConfig::new("/")))
async fn graphiql() -> impl IntoResponse {
Html(
GraphiQLSource::build()
.endpoint("http://localhost:8000")
.finish(),
)
}

#[tokio::main]
Expand All @@ -17,9 +18,9 @@ async fn main() {
.data(StarWars::new())
.finish();

let app = Route::new().at("/", get(graphql_playground).post(GraphQL::new(schema)));
let app = Route::new().at("/", get(graphiql).post(GraphQL::new(schema)));

println!("Playground: http://localhost:8000");
println!("GraphiQL IDE: http://localhost:8000");
Server::new(TcpListener::bind("0.0.0.0:8000"))
.run(app)
.await
Expand Down
23 changes: 10 additions & 13 deletions poem/subscription-redis/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
use async_graphql::{
http::{playground_source, GraphQLPlaygroundConfig},
Context, Object, Result, Schema, Subscription,
};
use async_graphql::{http::GraphiQLSource, Context, Object, Result, Schema, Subscription};
use async_graphql_poem::{GraphQL, GraphQLSubscription};
use futures_util::{Stream, StreamExt};
use poem::{get, handler, listener::TcpListener, web::Html, IntoResponse, Route, Server};
Expand Down Expand Up @@ -43,10 +40,13 @@ impl SubscriptionRoot {
}

#[handler]
async fn graphql_playground() -> impl IntoResponse {
Html(playground_source(
GraphQLPlaygroundConfig::new("/").subscription_endpoint("/ws"),
))
async fn graphiql() -> impl IntoResponse {
Html(
GraphiQLSource::build()
.endpoint("http://localhost:8000")
.subscription_endpoint("ws://localhost:8000/ws")
.finish(),
)
}

#[tokio::main]
Expand All @@ -58,13 +58,10 @@ async fn main() {
.finish();

let app = Route::new()
.at(
"/",
get(graphql_playground).post(GraphQL::new(schema.clone())),
)
.at("/", get(graphiql).post(GraphQL::new(schema.clone())))
.at("/ws", get(GraphQLSubscription::new(schema)));

println!("Playground: http://localhost:8000");
println!("GraphiQL IDE: http://localhost:8000");
Server::new(TcpListener::bind("0.0.0.0:8000"))
.run(app)
.await
Expand Down
Loading

0 comments on commit 0c17521

Please sign in to comment.