Skip to content

Commit a47d1c5

Browse files
authored
Add actix-web integration (#603)
1 parent 2ba9f71 commit a47d1c5

File tree

9 files changed

+684
-0
lines changed

9 files changed

+684
-0
lines changed

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ members = [
1212
"juniper_rocket_async",
1313
"juniper_subscriptions",
1414
"juniper_warp",
15+
"juniper_actix",
1516
]
1617
exclude = [
1718
"docs/book/tests",

juniper/release.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,7 @@ pre-release-replacements = [
3030
{file="../juniper_warp/Cargo.toml", search="\\[dev-dependencies\\.juniper\\]\nversion = \"[^\"]+\"", replace="[dev-dependencies.juniper]\nversion = \"{{version}}\""},
3131
# Subscriptions
3232
{file="../juniper_subscriptions/Cargo.toml", search="juniper = \\{ version = \"[^\"]+\"", replace="juniper = { version = \"{{version}}\""},
33+
# Actix-Web
34+
{file="../juniper_actix/Cargo.toml", search="juniper = \\{ version = \"[^\"]+\"", replace="juniper = { version = \"{{version}}\""},
35+
{file="../juniper_actix/Cargo.toml", search="\\[dev-dependencies\\.juniper\\]\nversion = \"[^\"]+\"", replace="[dev-dependencies.juniper]\nversion = \"{{version}}\""},
3336
]

juniper_actix/.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
/target
2+
/examples/**/target/**/*
3+
**/*.rs.bk
4+
Cargo.lock

juniper_actix/CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# master
2+
3+
- Initial Release

juniper_actix/Cargo.toml

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
[package]
2+
name = "juniper_actix"
3+
version = "0.1.0"
4+
authors = ["Jordao Rosario <[email protected]>"]
5+
description = "Juniper GraphQL integration with Actix"
6+
license = "BSD-2-Clause"
7+
documentation = "https://docs.rs/juniper_actix"
8+
repository = "https://github.com/graphql-rust/juniper"
9+
edition = "2018"
10+
11+
12+
[dependencies]
13+
actix = "0.9.0"
14+
actix-rt = "1.0.0"
15+
actix-web = { version = "2.0.0", features = ["rustls"] }
16+
actix-web-actors = "2.0.0"
17+
futures = { version = "0.3.1", features = ["compat"] }
18+
juniper = { version = "0.14.2", path = "../juniper", default-features = false }
19+
tokio = { version = "0.2", features = ["time"] }
20+
serde_json = "1.0.24"
21+
serde_derive = "1.0.75"
22+
failure = "0.1.7"
23+
serde = "1.0.75"
24+
25+
[dev-dependencies]
26+
juniper = { version = "0.14.2", path = "../juniper", features = ["expose-test-schema", "serde_json"] }
27+
env_logger = "0.5.11"
28+
log = "0.4.3"
29+
percent-encoding = "1.0"
30+
tokio = { version = "0.2", features = ["rt-core", "macros", "blocking"] }
31+
actix-cors = "0.2.0"
32+
actix-identity = "0.2.0"
33+
bytes = "0.5.4"

juniper_actix/LICENSE

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
BSD 2-Clause License
2+
3+
Copyright (c) 2018, Jordao Rosario
4+
All rights reserved.
5+
6+
Redistribution and use in source and binary forms, with or without
7+
modification, are permitted provided that the following conditions are met:
8+
9+
* Redistributions of source code must retain the above copyright notice, this
10+
list of conditions and the following disclaimer.
11+
12+
* Redistributions in binary form must reproduce the above copyright notice,
13+
this list of conditions and the following disclaimer in the documentation
14+
and/or other materials provided with the distribution.
15+
16+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17+
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18+
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19+
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
20+
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21+
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
22+
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
23+
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24+
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25+
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

juniper_actix/README.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# juniper_actix
2+
3+
This repository contains the [actix][actix] web server integration for
4+
[Juniper][Juniper], a [GraphQL][GraphQL] implementation for Rust, its inspired and some parts are copied from [juniper_warp][juniper_warp].
5+
6+
## Documentation
7+
8+
For documentation, including guides and examples, check out [Juniper][Juniper].
9+
10+
A basic usage example can also be found in the [API documentation][documentation].
11+
12+
## Examples
13+
14+
Check [examples/actix_server][example] for example code of a working actix
15+
server with GraphQL handlers.
16+
17+
## Links
18+
19+
* [Juniper][Juniper]
20+
* [API Reference][documentation]
21+
* [actix][actix]
22+
23+
## License
24+
25+
This project is under the BSD-2 license.
26+
27+
Check the LICENSE file for details.
28+
29+
[actix]: https://github.com/actix/actix-web
30+
[Juniper]: https://github.com/graphql-rust/juniper
31+
[GraphQL]: http://graphql.org
32+
[documentation]: https://docs.rs/juniper_actix
33+
[example]: https://github.com/graphql-rust/juniper/blob/master/juniper_actix/examples/actix_server.rs
34+
[juniper_warp]:https://docs.rs/juniper_warp
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
#![deny(warnings)]
2+
3+
extern crate log;
4+
use actix_cors::Cors;
5+
use actix_web::{middleware, web, App, Error, HttpResponse, HttpServer};
6+
use juniper::{
7+
tests::{model::Database, schema::Query},
8+
EmptyMutation, EmptySubscription, RootNode,
9+
};
10+
use juniper_actix::{
11+
graphiql_handler as gqli_handler, graphql_handler, playground_handler as play_handler,
12+
};
13+
14+
type Schema = RootNode<'static, Query, EmptyMutation<Database>, EmptySubscription<Database>>;
15+
16+
fn schema() -> Schema {
17+
Schema::new(
18+
Query,
19+
EmptyMutation::<Database>::new(),
20+
EmptySubscription::<Database>::new(),
21+
)
22+
}
23+
24+
async fn graphiql_handler() -> Result<HttpResponse, Error> {
25+
gqli_handler("/", None).await
26+
}
27+
async fn playground_handler() -> Result<HttpResponse, Error> {
28+
play_handler("/", None).await
29+
}
30+
async fn graphql(
31+
req: actix_web::HttpRequest,
32+
payload: actix_web::web::Payload,
33+
schema: web::Data<Schema>,
34+
) -> Result<HttpResponse, Error> {
35+
let context = Database::new();
36+
graphql_handler(&schema, &context, req, payload).await
37+
}
38+
39+
#[actix_rt::main]
40+
async fn main() -> std::io::Result<()> {
41+
::std::env::set_var("RUST_LOG", "actix_web=info");
42+
env_logger::init();
43+
let server = HttpServer::new(move || {
44+
App::new()
45+
.data(schema())
46+
.wrap(middleware::Compress::default())
47+
.wrap(middleware::Logger::default())
48+
.wrap(
49+
Cors::new()
50+
.allowed_methods(vec!["POST", "GET"])
51+
.supports_credentials()
52+
.max_age(3600)
53+
.finish(),
54+
)
55+
.service(
56+
web::resource("/")
57+
.route(web::post().to(graphql))
58+
.route(web::get().to(graphql)),
59+
)
60+
.service(web::resource("/playground").route(web::get().to(playground_handler)))
61+
.service(web::resource("/graphiql").route(web::get().to(graphiql_handler)))
62+
});
63+
server.bind("127.0.0.1:8080").unwrap().run().await
64+
}

0 commit comments

Comments
 (0)