Skip to content

Implementation for juniper_actix #603

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Apr 21, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ members = [
"juniper_rocket_async",
"juniper_subscriptions",
"juniper_warp",
"juniper_actix",
]
exclude = [
"docs/book/tests",
Expand Down
3 changes: 3 additions & 0 deletions juniper/release.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,7 @@ pre-release-replacements = [
{file="../juniper_warp/Cargo.toml", search="\\[dev-dependencies\\.juniper\\]\nversion = \"[^\"]+\"", replace="[dev-dependencies.juniper]\nversion = \"{{version}}\""},
# Subscriptions
{file="../juniper_subscriptions/Cargo.toml", search="juniper = \\{ version = \"[^\"]+\"", replace="juniper = { version = \"{{version}}\""},
# Actix-Web
{file="../juniper_actix/Cargo.toml", search="juniper = \\{ version = \"[^\"]+\"", replace="juniper = { version = \"{{version}}\""},
{file="../juniper_actix/Cargo.toml", search="\\[dev-dependencies\\.juniper\\]\nversion = \"[^\"]+\"", replace="[dev-dependencies.juniper]\nversion = \"{{version}}\""},
]
4 changes: 4 additions & 0 deletions juniper_actix/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/target
/examples/**/target/**/*
**/*.rs.bk
Cargo.lock
3 changes: 3 additions & 0 deletions juniper_actix/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# master

- Initial Release
33 changes: 33 additions & 0 deletions juniper_actix/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
[package]
name = "juniper_actix"
version = "0.1.0"
authors = ["Jordao Rosario <[email protected]>"]
description = "Juniper GraphQL integration with Actix"
license = "BSD-2-Clause"
documentation = "https://docs.rs/juniper_actix"
repository = "https://github.com/graphql-rust/juniper"
edition = "2018"


[dependencies]
actix = "0.9.0"
actix-rt = "1.0.0"
actix-web = { version = "2.0.0", features = ["rustls"] }
actix-web-actors = "2.0.0"
futures = { version = "0.3.1", features = ["compat"] }
juniper = { version = "0.14.2", path = "../juniper", default-features = false }
tokio = { version = "0.2", features = ["time"] }
serde_json = "1.0.24"
serde_derive = "1.0.75"
failure = "0.1.7"
serde = "1.0.75"

[dev-dependencies]
juniper = { version = "0.14.2", path = "../juniper", features = ["expose-test-schema", "serde_json"] }
env_logger = "0.5.11"
log = "0.4.3"
percent-encoding = "1.0"
tokio = { version = "0.2", features = ["rt-core", "macros", "blocking"] }
actix-cors = "0.2.0"
actix-identity = "0.2.0"
bytes = "0.5.4"
25 changes: 25 additions & 0 deletions juniper_actix/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
BSD 2-Clause License

Copyright (c) 2018, Jordao Rosario
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

* Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.

* Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 changes: 34 additions & 0 deletions juniper_actix/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# juniper_actix

This repository contains the [actix][actix] web server integration for
[Juniper][Juniper], a [GraphQL][GraphQL] implementation for Rust, its inspired and some parts are copied from [juniper_warp][juniper_warp].

## Documentation

For documentation, including guides and examples, check out [Juniper][Juniper].

A basic usage example can also be found in the [API documentation][documentation].

## Examples

Check [examples/actix_server][example] for example code of a working actix
server with GraphQL handlers.

## Links

* [Juniper][Juniper]
* [API Reference][documentation]
* [actix][actix]

## License

This project is under the BSD-2 license.

Check the LICENSE file for details.

[actix]: https://github.com/actix/actix-web
[Juniper]: https://github.com/graphql-rust/juniper
[GraphQL]: http://graphql.org
[documentation]: https://docs.rs/juniper_actix
[example]: https://github.com/graphql-rust/juniper/blob/master/juniper_actix/examples/actix_server.rs
[juniper_warp]:https://docs.rs/juniper_warp
64 changes: 64 additions & 0 deletions juniper_actix/examples/actix_server.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
#![deny(warnings)]

extern crate log;
use actix_cors::Cors;
use actix_web::{middleware, web, App, Error, HttpResponse, HttpServer};
use juniper::{
tests::{model::Database, schema::Query},
EmptyMutation, EmptySubscription, RootNode,
};
use juniper_actix::{
graphiql_handler as gqli_handler, graphql_handler, playground_handler as play_handler,
};

type Schema = RootNode<'static, Query, EmptyMutation<Database>, EmptySubscription<Database>>;

fn schema() -> Schema {
Schema::new(
Query,
EmptyMutation::<Database>::new(),
EmptySubscription::<Database>::new(),
)
}

async fn graphiql_handler() -> Result<HttpResponse, Error> {
gqli_handler("/", None).await
}
async fn playground_handler() -> Result<HttpResponse, Error> {
play_handler("/", None).await
}
async fn graphql(
req: actix_web::HttpRequest,
payload: actix_web::web::Payload,
schema: web::Data<Schema>,
) -> Result<HttpResponse, Error> {
let context = Database::new();
graphql_handler(&schema, &context, req, payload).await
}

#[actix_rt::main]
async fn main() -> std::io::Result<()> {
::std::env::set_var("RUST_LOG", "actix_web=info");
env_logger::init();
let server = HttpServer::new(move || {
App::new()
.data(schema())
.wrap(middleware::Compress::default())
.wrap(middleware::Logger::default())
.wrap(
Cors::new()
.allowed_methods(vec!["POST", "GET"])
.supports_credentials()
.max_age(3600)
.finish(),
)
.service(
web::resource("/")
.route(web::post().to(graphql))
.route(web::get().to(graphql)),
)
.service(web::resource("/playground").route(web::get().to(playground_handler)))
.service(web::resource("/graphiql").route(web::get().to(graphiql_handler)))
});
server.bind("127.0.0.1:8080").unwrap().run().await
}
Loading