1
0
mirror of https://github.com/actix/examples synced 2025-06-26 17:17:42 +02:00

update async-graphql example

This commit is contained in:
Rob Ede
2022-02-17 21:29:55 +00:00
parent e0888cd255
commit 1b23e3ff3d
14 changed files with 164 additions and 147 deletions

View File

@ -0,0 +1,16 @@
[package]
name = "async-graphql-demo"
version = "1.0.0"
edition = "2021"
[dependencies]
actix-web = "4.0.0-rc.3"
actix-web-lab = "0.10"
actix-cors = "0.6.0-beta.10"
async-graphql = "3"
async-graphql-actix-web = "3"
env_logger = "0.9"
log = "0.4"
slab = "0.4.2"

View File

@ -0,0 +1,38 @@
## GraphQL using `async-graphql`
> Getting started using [async-graphql](https://github.com/async-graphql/async-graphql) with Actix Web.
## Usage
```bash
cd graphql/graphql-demo
cargo run
```
## Endpoints
```
GET/POST http://localhost:8080/graphql GraphQL endpoint
GET http://localhost:8080/graphiql GraphQL playground UI
```
## Query Examples
```graphql
{
humans {
edges {
node {
id
name
friends {
id
name
}
appearsIn
homePlanet
}
}
}
}
```

View File

@ -0,0 +1,53 @@
use actix_cors::Cors;
use actix_web::{get, middleware::Logger, route, web, App, HttpServer, Responder};
use actix_web_lab::respond::Html;
use async_graphql::{
http::{playground_source, GraphQLPlaygroundConfig},
EmptyMutation, EmptySubscription, Schema,
};
use async_graphql_actix_web::{GraphQLRequest, GraphQLResponse};
mod star_wars;
use self::star_wars::{QueryRoot, StarWars, StarWarsSchema};
/// GraphQL endpoint
#[route("/graphql", method = "GET", method = "POST")]
async fn graphql(
schema: web::Data<StarWarsSchema>,
req: GraphQLRequest,
) -> GraphQLResponse {
schema.execute(req.into_inner()).await.into()
}
/// GraphiQL playground UI
#[get("/graphiql")]
async fn graphql_playground() -> impl Responder {
Html(playground_source(
GraphQLPlaygroundConfig::new("/graphql").subscription_endpoint("/graphql"),
))
}
#[actix_web::main]
async fn main() -> std::io::Result<()> {
env_logger::init_from_env(env_logger::Env::new().default_filter_or("info"));
let schema = Schema::build(QueryRoot, EmptyMutation, EmptySubscription)
.data(StarWars::new())
.finish();
log::info!("starting HTTP server on port 8080");
log::info!("GraphiQL playground: http://localhost:8080/graphiql");
HttpServer::new(move || {
App::new()
.app_data(web::Data::new(schema.clone()))
.service(graphql)
.service(graphql_playground)
.wrap(Cors::permissive())
.wrap(Logger::default())
})
.workers(2)
.bind(("127.0.0.1", 8080))?
.run()
.await
}

View File

@ -1,13 +1,14 @@
mod model;
use std::collections::HashMap;
use async_graphql::{EmptyMutation, EmptySubscription, Schema};
use model::Episode;
use slab::Slab;
use std::collections::HashMap;
pub use model::QueryRoot;
pub type StarWarsSchema = Schema<QueryRoot, EmptyMutation, EmptySubscription>;
mod model;
pub use self::model::QueryRoot;
pub struct StarWarsChar {
id: &'static str,
name: &'static str,

View File

@ -1,7 +1,8 @@
use super::StarWars;
use async_graphql::connection::{query, Connection, Edge, EmptyFields};
use async_graphql::{Context, Enum, FieldResult, Interface, Object};
use super::StarWars;
/// One of the films in the Star Wars Trilogy
#[derive(Enum, Copy, Clone, Eq, PartialEq)]
pub enum Episode {
@ -218,7 +219,8 @@ async fn query_characters(
.enumerate()
.map(|(idx, item)| Edge::new(start + idx, *item)),
);
Ok(connection)
FieldResult::Ok(connection)
},
)
.await

View File

@ -1,10 +0,0 @@
[package]
name = "async-graphql-demo"
version = "1.0.0"
edition = "2021"
[dependencies]
actix-web = "3.0.0"
async-graphql = "2.0.0"
async-graphql-actix-web = "2.0.0"
slab = "0.4.2"

View File

@ -1,34 +0,0 @@
Getting started using [Async-graphql](https://github.com/async-graphql/async-graphql) with Actix Web.
## Run
```bash
cd graphql/graphql-demo
cargo run --bin async-graphql-demo
```
## Endpoints
GET http://127.0.0.1:8000/ GraphQL Playground UI
POST http://127.0.0.1:8000/ For GraphQL query
## Query Examples
```graphql
{
humans {
edges {
node {
id
name
friends {
id
name
}
appearsIn
homePlanet
}
}
}
}
```

View File

@ -1,38 +0,0 @@
mod starwars;
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 starwars::{QueryRoot, StarWars, StarWarsSchema};
async fn index(schema: web::Data<StarWarsSchema>, req: Request) -> Response {
schema.execute(req.into_inner()).await.into()
}
async fn index_playground() -> Result<HttpResponse> {
Ok(HttpResponse::Ok()
.content_type("text/html; charset=utf-8")
.body(playground_source(
GraphQLPlaygroundConfig::new("/").subscription_endpoint("/"),
)))
}
#[actix_web::main]
async fn main() -> std::io::Result<()> {
let schema = Schema::build(QueryRoot, EmptyMutation, EmptySubscription)
.data(StarWars::new())
.finish();
println!("Playground: http://localhost:8000");
HttpServer::new(move || {
App::new()
.data(schema.clone())
.service(web::resource("/").guard(guard::Post()).to(index))
.service(web::resource("/").guard(guard::Get()).to(index_playground))
})
.bind("127.0.0.1:8000")?
.run()
.await
}

View File

@ -1,4 +1,4 @@
# juniper-advanced
# GraphQL using Juniper and MySQL
GraphQL Implementation in Rust using Actix, Juniper, and MySQL as Database

View File

@ -18,7 +18,7 @@ async fn main() -> std::io::Result<()> {
let pool = get_db_pool();
log::info!("starting HTTP server on port 8080");
log::info!("the GraphiQL interface HTTP server at http://localhost:8080/graphiql");
log::info!("GraphiQL playground: http://localhost:8080/graphiql");
HttpServer::new(move || {
App::new()

View File

@ -1,4 +1,4 @@
# Juniper
# GraphQL using Juniper
[Juniper](https://github.com/graphql-rust/juniper) integration for Actix Web.
If you want more advanced example, see also the [juniper-advanced example].

View File

@ -40,7 +40,8 @@ async fn main() -> io::Result<()> {
// Create Juniper schema
let schema = Arc::new(create_schema());
log::info!("starting HTTP server at http://localhost:8080");
log::info!("starting HTTP server on port 8080");
log::info!("GraphiQL playground: http://localhost:8080/graphiql");
// Start HTTP server
HttpServer::new(move || {