mirror of
https://github.com/actix/examples
synced 2024-11-30 17:14:35 +01:00
Update Juniper example (#197)
This commit is contained in:
parent
3552b29359
commit
7e37bef4a1
@ -1,15 +1,14 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "juniper-example"
|
name = "juniper-example"
|
||||||
version = "0.1.0"
|
version = "0.2.0"
|
||||||
authors = ["pyros2097 <pyros2097@gmail.com>"]
|
authors = ["pyros2097 <pyros2097@gmail.com>"]
|
||||||
workspace = ".."
|
workspace = ".."
|
||||||
edition = "2018"
|
edition = "2018"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
actix-web = "1.0.0"
|
actix-web = "1.0.9"
|
||||||
env_logger = "0.6"
|
env_logger = "0.7.1"
|
||||||
futures = "0.1"
|
serde = "1.0.103"
|
||||||
serde = "1.0"
|
serde_json = "1.0.44"
|
||||||
serde_json = "1.0"
|
serde_derive = "1.0.103"
|
||||||
serde_derive = "1.0"
|
juniper = "0.14.1"
|
||||||
juniper = "0.9.2"
|
|
||||||
|
@ -1,6 +1,8 @@
|
|||||||
# juniper
|
# Juniper
|
||||||
|
|
||||||
Juniper integration for Actix web
|
[Juniper](https://github.com/graphql-rust/juniper) integration for Actix web
|
||||||
|
|
||||||
|
## Usage
|
||||||
|
|
||||||
### server
|
### server
|
||||||
|
|
||||||
@ -13,3 +15,57 @@ cargo run (or ``cargo watch -x run``)
|
|||||||
### web client
|
### web client
|
||||||
|
|
||||||
[http://127.0.0.1:8080/graphiql](http://127.0.0.1:8080/graphiql)
|
[http://127.0.0.1:8080/graphiql](http://127.0.0.1:8080/graphiql)
|
||||||
|
|
||||||
|
_Query example:_
|
||||||
|
```graphql
|
||||||
|
{
|
||||||
|
human(id: "1234") {
|
||||||
|
name
|
||||||
|
appearsIn
|
||||||
|
homePlanet
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
_Result:_
|
||||||
|
```json
|
||||||
|
{
|
||||||
|
"data": {
|
||||||
|
"human": {
|
||||||
|
"name": "Luke",
|
||||||
|
"appearsIn": [
|
||||||
|
"NEW_HOPE"
|
||||||
|
],
|
||||||
|
"homePlanet": "Mars"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
_Mutation example:_
|
||||||
|
|
||||||
|
```graphql
|
||||||
|
mutation {
|
||||||
|
createHuman(newHuman: {name: "Fresh Kid Ice", appearsIn: EMPIRE, homePlanet: "earth"}) {
|
||||||
|
id
|
||||||
|
name
|
||||||
|
appearsIn
|
||||||
|
homePlanet
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
_Result:_
|
||||||
|
```json
|
||||||
|
{
|
||||||
|
"data": {
|
||||||
|
"createHuman": {
|
||||||
|
"id": "1234",
|
||||||
|
"name": "Fresh Kid Ice",
|
||||||
|
"appearsIn": [
|
||||||
|
"EMPIRE"
|
||||||
|
],
|
||||||
|
"homePlanet": "earth"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
@ -8,7 +8,6 @@ use std::sync::Arc;
|
|||||||
extern crate juniper;
|
extern crate juniper;
|
||||||
|
|
||||||
use actix_web::{middleware, web, App, Error, HttpResponse, HttpServer};
|
use actix_web::{middleware, web, App, Error, HttpResponse, HttpServer};
|
||||||
use futures::future::Future;
|
|
||||||
use juniper::http::graphiql::graphiql_source;
|
use juniper::http::graphiql::graphiql_source;
|
||||||
use juniper::http::GraphQLRequest;
|
use juniper::http::GraphQLRequest;
|
||||||
|
|
||||||
@ -17,43 +16,40 @@ mod schema;
|
|||||||
use crate::schema::{create_schema, Schema};
|
use crate::schema::{create_schema, Schema};
|
||||||
|
|
||||||
fn graphiql() -> HttpResponse {
|
fn graphiql() -> HttpResponse {
|
||||||
let html = graphiql_source("http://127.0.0.1:8080/graphql");
|
let html = graphiql_source("http://127.0.0.1:8080/graphql");
|
||||||
HttpResponse::Ok()
|
HttpResponse::Ok()
|
||||||
.content_type("text/html; charset=utf-8")
|
.content_type("text/html; charset=utf-8")
|
||||||
.body(html)
|
.body(html)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn graphql(
|
fn graphql(
|
||||||
st: web::Data<Arc<Schema>>,
|
st: web::Data<Arc<Schema>>,
|
||||||
data: web::Json<GraphQLRequest>,
|
data: web::Json<GraphQLRequest>,
|
||||||
) -> impl Future<Item = HttpResponse, Error = Error> {
|
) -> Result<HttpResponse, Error> {
|
||||||
web::block(move || {
|
let res = data.execute(&st, &());
|
||||||
let res = data.execute(&st, &());
|
let user = serde_json::to_string(&res)?;
|
||||||
Ok::<_, serde_json::error::Error>(serde_json::to_string(&res)?)
|
Ok(
|
||||||
})
|
HttpResponse::Ok()
|
||||||
.map_err(Error::from)
|
.content_type("application/json")
|
||||||
.and_then(|user| {
|
.body(user)
|
||||||
Ok(HttpResponse::Ok()
|
)
|
||||||
.content_type("application/json")
|
|
||||||
.body(user))
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() -> io::Result<()> {
|
fn main() -> io::Result<()> {
|
||||||
std::env::set_var("RUST_LOG", "actix_web=info");
|
std::env::set_var("RUST_LOG", "actix_web=info");
|
||||||
env_logger::init();
|
env_logger::init();
|
||||||
|
|
||||||
// Create Juniper schema
|
// Create Juniper schema
|
||||||
let schema = std::sync::Arc::new(create_schema());
|
let schema = std::sync::Arc::new(create_schema());
|
||||||
|
|
||||||
// Start http server
|
// Start http server
|
||||||
HttpServer::new(move || {
|
HttpServer::new(move || {
|
||||||
App::new()
|
App::new()
|
||||||
.data(schema.clone())
|
.data(schema.clone())
|
||||||
.wrap(middleware::Logger::default())
|
.wrap(middleware::Logger::default())
|
||||||
.service(web::resource("/graphql").route(web::post().to_async(graphql)))
|
.service(web::resource("/graphql").route(web::post().to_async(graphql)))
|
||||||
.service(web::resource("/graphiql").route(web::get().to(graphiql)))
|
.service(web::resource("/graphiql").route(web::get().to(graphiql)))
|
||||||
})
|
})
|
||||||
.bind("127.0.0.1:8080")?
|
.bind("127.0.0.1:8080")?
|
||||||
.run()
|
.run()
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user