From 5b9a0bd31b2eb6822f1181c8088715e01f821df7 Mon Sep 17 00:00:00 2001 From: Andrey Kutejko Date: Sat, 18 May 2019 18:07:51 +0200 Subject: [PATCH] Split Query and Database types in example schema (#360) --- juniper/src/tests/introspection_tests.rs | 19 ++++++++-------- juniper/src/tests/mod.rs | 2 +- juniper/src/tests/query_tests.rs | 29 ++++++++++++------------ juniper/src/tests/schema.rs | 19 +++++++++------- juniper_hyper/examples/hyper_server.rs | 3 ++- juniper_hyper/src/lib.rs | 3 ++- juniper_iron/examples/iron_server.rs | 8 +++---- juniper_iron/src/lib.rs | 3 ++- juniper_rocket/examples/rocket_server.rs | 8 +++---- juniper_rocket/src/lib.rs | 13 ++++++----- juniper_warp/examples/warp_server.rs | 5 ++-- juniper_warp/src/lib.rs | 15 +++++++----- 12 files changed, 68 insertions(+), 59 deletions(-) diff --git a/juniper/src/tests/introspection_tests.rs b/juniper/src/tests/introspection_tests.rs index 0bd0eb6c..d631e5a3 100644 --- a/juniper/src/tests/introspection_tests.rs +++ b/juniper/src/tests/introspection_tests.rs @@ -5,6 +5,7 @@ use crate::executor::Variables; use crate::introspection::IntrospectionFormat; use crate::schema::model::RootNode; use crate::tests::model::Database; +use crate::tests::schema::Query; use crate::types::scalars::EmptyMutation; #[test] @@ -18,7 +19,7 @@ fn test_introspection_query_type_name() { } }"#; let database = Database::new(); - let schema = RootNode::new(&database, EmptyMutation::::new()); + let schema = RootNode::new(Query, EmptyMutation::::new()); assert_eq!( crate::execute(doc, None, &schema, &Variables::new(), &database), @@ -45,7 +46,7 @@ fn test_introspection_type_name() { } }"#; let database = Database::new(); - let schema = RootNode::new(&database, EmptyMutation::::new()); + let schema = RootNode::new(Query, EmptyMutation::::new()); assert_eq!( crate::execute(doc, None, &schema, &Variables::new(), &database), @@ -71,7 +72,7 @@ fn test_introspection_specific_object_type_name_and_kind() { } "#; let database = Database::new(); - let schema = RootNode::new(&database, EmptyMutation::::new()); + let schema = RootNode::new(Query, EmptyMutation::::new()); assert_eq!( crate::execute(doc, None, &schema, &Variables::new(), &database), @@ -98,7 +99,7 @@ fn test_introspection_specific_interface_type_name_and_kind() { } "#; let database = Database::new(); - let schema = RootNode::new(&database, EmptyMutation::::new()); + let schema = RootNode::new(Query, EmptyMutation::::new()); assert_eq!( crate::execute(doc, None, &schema, &Variables::new(), &database), @@ -125,7 +126,7 @@ fn test_introspection_documentation() { } "#; let database = Database::new(); - let schema = RootNode::new(&database, EmptyMutation::::new()); + let schema = RootNode::new(Query, EmptyMutation::::new()); assert_eq!( crate::execute(doc, None, &schema, &Variables::new(), &database), @@ -155,7 +156,7 @@ fn test_introspection_directives() { "#; let database = Database::new(); - let schema = RootNode::new(&database, EmptyMutation::::new()); + let schema = RootNode::new(Query, EmptyMutation::::new()); let mut result = crate::execute(q, None, &schema, &Variables::new(), &database).unwrap(); sort_schema_value(&mut result.0); @@ -199,7 +200,7 @@ fn test_introspection_possible_types() { } "#; let database = Database::new(); - let schema = RootNode::new(&database, EmptyMutation::::new()); + let schema = RootNode::new(Query, EmptyMutation::::new()); let result = crate::execute(doc, None, &schema, &Variables::new(), &database); @@ -235,7 +236,7 @@ fn test_introspection_possible_types() { #[test] fn test_builtin_introspection_query() { let database = Database::new(); - let schema = RootNode::new(&database, EmptyMutation::::new()); + let schema = RootNode::new(Query, EmptyMutation::::new()); let mut result = crate::introspect(&schema, &database, IntrospectionFormat::default()).unwrap(); sort_schema_value(&mut result.0); @@ -246,7 +247,7 @@ fn test_builtin_introspection_query() { #[test] fn test_builtin_introspection_query_without_descriptions() { let database = Database::new(); - let schema = RootNode::new(&database, EmptyMutation::::new()); + let schema = RootNode::new(Query, EmptyMutation::::new()); let mut result = crate::introspect(&schema, &database, IntrospectionFormat::WithoutDescriptions).unwrap(); diff --git a/juniper/src/tests/mod.rs b/juniper/src/tests/mod.rs index 44629d10..c2ecd041 100644 --- a/juniper/src/tests/mod.rs +++ b/juniper/src/tests/mod.rs @@ -5,7 +5,7 @@ mod introspection_tests; pub mod model; #[cfg(test)] mod query_tests; -mod schema; +pub mod schema; #[cfg(test)] mod schema_introspection; #[cfg(test)] diff --git a/juniper/src/tests/query_tests.rs b/juniper/src/tests/query_tests.rs index 3646c3e1..a8f7b634 100644 --- a/juniper/src/tests/query_tests.rs +++ b/juniper/src/tests/query_tests.rs @@ -2,6 +2,7 @@ use crate::ast::InputValue; use crate::executor::Variables; use crate::schema::model::RootNode; use crate::tests::model::Database; +use crate::tests::schema::Query; use crate::types::scalars::EmptyMutation; use crate::value::Value; @@ -14,7 +15,7 @@ fn test_hero_name() { } }"#; let database = Database::new(); - let schema = RootNode::new(&database, EmptyMutation::::new()); + let schema = RootNode::new(Query, EmptyMutation::::new()); assert_eq!( crate::execute(doc, None, &schema, &Variables::new(), &database), @@ -35,7 +36,7 @@ fn test_hero_name() { #[test] fn test_hero_field_order() { let database = Database::new(); - let schema = RootNode::new(&database, EmptyMutation::::new()); + let schema = RootNode::new(Query, EmptyMutation::::new()); let doc = r#" { @@ -109,7 +110,7 @@ fn test_hero_name_and_friends() { } }"#; let database = Database::new(); - let schema = RootNode::new(&database, EmptyMutation::::new()); + let schema = RootNode::new(Query, EmptyMutation::::new()); assert_eq!( crate::execute(doc, None, &schema, &Variables::new(), &database), @@ -171,7 +172,7 @@ fn test_hero_name_and_friends_and_friends_of_friends() { } }"#; let database = Database::new(); - let schema = RootNode::new(&database, EmptyMutation::::new()); + let schema = RootNode::new(Query, EmptyMutation::::new()); assert_eq!( crate::execute(doc, None, &schema, &Variables::new(), &database), @@ -332,7 +333,7 @@ fn test_hero_name_and_friends_and_friends_of_friends() { fn test_query_name() { let doc = r#"{ human(id: "1000") { name } }"#; let database = Database::new(); - let schema = RootNode::new(&database, EmptyMutation::::new()); + let schema = RootNode::new(Query, EmptyMutation::::new()); assert_eq!( crate::execute(doc, None, &schema, &Variables::new(), &database), @@ -358,7 +359,7 @@ fn test_query_name() { fn test_query_alias_single() { let doc = r#"{ luke: human(id: "1000") { name } }"#; let database = Database::new(); - let schema = RootNode::new(&database, EmptyMutation::::new()); + let schema = RootNode::new(Query, EmptyMutation::::new()); assert_eq!( crate::execute(doc, None, &schema, &Variables::new(), &database), @@ -388,7 +389,7 @@ fn test_query_alias_multiple() { leia: human(id: "1003") { name } }"#; let database = Database::new(); - let schema = RootNode::new(&database, EmptyMutation::::new()); + let schema = RootNode::new(Query, EmptyMutation::::new()); assert_eq!( crate::execute(doc, None, &schema, &Variables::new(), &database), @@ -433,7 +434,7 @@ fn test_query_alias_multiple_with_fragment() { homePlanet }"#; let database = Database::new(); - let schema = RootNode::new(&database, EmptyMutation::::new()); + let schema = RootNode::new(Query, EmptyMutation::::new()); assert_eq!( crate::execute(doc, None, &schema, &Variables::new(), &database), @@ -475,7 +476,7 @@ fn test_query_alias_multiple_with_fragment() { fn test_query_name_variable() { let doc = r#"query FetchSomeIDQuery($someId: String!) { human(id: $someId) { name } }"#; let database = Database::new(); - let schema = RootNode::new(&database, EmptyMutation::::new()); + let schema = RootNode::new(Query, EmptyMutation::::new()); let vars = vec![("someId".to_owned(), InputValue::scalar("1000"))] .into_iter() @@ -505,7 +506,7 @@ fn test_query_name_variable() { fn test_query_name_invalid_variable() { let doc = r#"query FetchSomeIDQuery($someId: String!) { human(id: $someId) { name } }"#; let database = Database::new(); - let schema = RootNode::new(&database, EmptyMutation::::new()); + let schema = RootNode::new(Query, EmptyMutation::::new()); let vars = vec![("someId".to_owned(), InputValue::scalar("some invalid id"))] .into_iter() @@ -524,7 +525,7 @@ fn test_query_name_invalid_variable() { fn test_query_friends_names() { let doc = r#"{ human(id: "1000") { friends { name } } }"#; let database = Database::new(); - let schema = RootNode::new(&database, EmptyMutation::::new()); + let schema = RootNode::new(Query, EmptyMutation::::new()); assert_eq!( crate::execute(doc, None, &schema, &Variables::new(), &database), @@ -581,7 +582,7 @@ fn test_query_inline_fragments_droid() { } "#; let database = Database::new(); - let schema = RootNode::new(&database, EmptyMutation::::new()); + let schema = RootNode::new(Query, EmptyMutation::::new()); assert_eq!( crate::execute(doc, None, &schema, &Variables::new(), &database), @@ -618,7 +619,7 @@ fn test_query_inline_fragments_human() { } "#; let database = Database::new(); - let schema = RootNode::new(&database, EmptyMutation::::new()); + let schema = RootNode::new(Query, EmptyMutation::::new()); assert_eq!( crate::execute(doc, None, &schema, &Variables::new(), &database), @@ -652,7 +653,7 @@ fn test_object_typename() { } }"#; let database = Database::new(); - let schema = RootNode::new(&database, EmptyMutation::::new()); + let schema = RootNode::new(Query, EmptyMutation::::new()); assert_eq!( crate::execute(doc, None, &schema, &Variables::new(), &database), diff --git a/juniper/src/tests/schema.rs b/juniper/src/tests/schema.rs index a39abd59..87dcefed 100644 --- a/juniper/src/tests/schema.rs +++ b/juniper/src/tests/schema.rs @@ -1,3 +1,5 @@ +#![allow(missing_docs)] + use crate::executor::Context; use crate::tests::model::{Character, Database, Droid, Episode, Human}; @@ -95,27 +97,28 @@ impl<'a> &'a Droid { } } +pub struct Query; + #[crate::object_internal( - name = "Query", Context = Database, Scalar = crate::DefaultScalarValue, )] /// The root query object of the schema -impl Database { +impl Query { #[graphql(arguments(id(description = "id of the human")))] - fn human(&self, id: String) -> Option<&Human> { - self.get_human(&id) + fn human(database: &Database, id: String) -> Option<&Human> { + database.get_human(&id) } #[graphql(arguments(id(description = "id of the droid")))] - fn droid(&self, id: String) -> Option<&Droid> { - self.get_droid(&id) + fn droid(database: &Database, id: String) -> Option<&Droid> { + database.get_droid(&id) } #[graphql(arguments(episode( description = "If omitted, returns the hero of the whole saga. If provided, returns the hero of that particular episode" )))] - fn hero(&self, episode: Option) -> Option<&Character> { - Some(self.get_hero(episode).as_character()) + fn hero(database: &Database, episode: Option) -> Option<&Character> { + Some(database.get_hero(episode).as_character()) } } diff --git a/juniper_hyper/examples/hyper_server.rs b/juniper_hyper/examples/hyper_server.rs index 595526d7..cacacc76 100644 --- a/juniper_hyper/examples/hyper_server.rs +++ b/juniper_hyper/examples/hyper_server.rs @@ -10,6 +10,7 @@ use hyper::service::service_fn; use hyper::Method; use hyper::{Body, Response, Server, StatusCode}; use juniper::tests::model::Database; +use juniper::tests::schema::Query; use juniper::EmptyMutation; use juniper::RootNode; use std::sync::Arc; @@ -20,7 +21,7 @@ fn main() { let addr = ([127, 0, 0, 1], 3000).into(); let db = Arc::new(Database::new()); - let root_node = Arc::new(RootNode::new(db.clone(), EmptyMutation::::new())); + let root_node = Arc::new(RootNode::new(Query, EmptyMutation::::new())); let new_service = move || { let root_node = root_node.clone(); diff --git a/juniper_hyper/src/lib.rs b/juniper_hyper/src/lib.rs index ec088cbd..2cececa6 100644 --- a/juniper_hyper/src/lib.rs +++ b/juniper_hyper/src/lib.rs @@ -311,6 +311,7 @@ mod tests { use hyper::{Body, Response, Server, StatusCode}; use juniper::http::tests as http_tests; use juniper::tests::model::Database; + use juniper::tests::schema::Query; use juniper::EmptyMutation; use juniper::RootNode; use reqwest; @@ -362,7 +363,7 @@ mod tests { let addr = ([127, 0, 0, 1], 3001).into(); let db = Arc::new(Database::new()); - let root_node = Arc::new(RootNode::new(db.clone(), EmptyMutation::::new())); + let root_node = Arc::new(RootNode::new(Query, EmptyMutation::::new())); let new_service = move || { let root_node = root_node.clone(); diff --git a/juniper_iron/examples/iron_server.rs b/juniper_iron/examples/iron_server.rs index 1939c5af..174e4135 100644 --- a/juniper_iron/examples/iron_server.rs +++ b/juniper_iron/examples/iron_server.rs @@ -9,6 +9,7 @@ use std::env; use iron::prelude::*; use juniper::tests::model::Database; +use juniper::tests::schema::Query; use juniper::EmptyMutation; use juniper_iron::{GraphQLHandler, GraphiQLHandler}; use logger::Logger; @@ -21,11 +22,8 @@ fn context_factory(_: &mut Request) -> IronResult { fn main() { let mut mount = Mount::new(); - let graphql_endpoint = GraphQLHandler::new( - context_factory, - Database::new(), - EmptyMutation::::new(), - ); + let graphql_endpoint = + GraphQLHandler::new(context_factory, Query, EmptyMutation::::new()); let graphiql_endpoint = GraphiQLHandler::new("/graphql"); mount.mount("/", graphiql_endpoint); diff --git a/juniper_iron/src/lib.rs b/juniper_iron/src/lib.rs index d56f0b94..3e09cc88 100644 --- a/juniper_iron/src/lib.rs +++ b/juniper_iron/src/lib.rs @@ -442,6 +442,7 @@ mod tests { use juniper::http::tests as http_tests; use juniper::tests::model::Database; + use juniper::tests::schema::Query; use juniper::EmptyMutation; use super::GraphQLHandler; @@ -529,7 +530,7 @@ mod tests { fn make_handler() -> Box { Box::new(GraphQLHandler::new( context_factory, - Database::new(), + Query, EmptyMutation::::new(), )) } diff --git a/juniper_rocket/examples/rocket_server.rs b/juniper_rocket/examples/rocket_server.rs index 8417ce7e..09669f44 100644 --- a/juniper_rocket/examples/rocket_server.rs +++ b/juniper_rocket/examples/rocket_server.rs @@ -4,9 +4,10 @@ use rocket::response::content; use rocket::State; use juniper::tests::model::Database; +use juniper::tests::schema::Query; use juniper::{EmptyMutation, RootNode}; -type Schema = RootNode<'static, Database, EmptyMutation>; +type Schema = RootNode<'static, Query, EmptyMutation>; #[rocket::get("/")] fn graphiql() -> content::Html { @@ -34,10 +35,7 @@ fn post_graphql_handler( fn main() { rocket::ignite() .manage(Database::new()) - .manage(Schema::new( - Database::new(), - EmptyMutation::::new(), - )) + .manage(Schema::new(Query, EmptyMutation::::new())) .mount( "/", rocket::routes![graphiql, get_graphql_handler, post_graphql_handler], diff --git a/juniper_rocket/src/lib.rs b/juniper_rocket/src/lib.rs index 9fcc5034..4c876f5f 100644 --- a/juniper_rocket/src/lib.rs +++ b/juniper_rocket/src/lib.rs @@ -209,10 +209,11 @@ impl GraphQLResponse { /// # use rocket::response::content; /// # use rocket::State; /// # + /// # use juniper::tests::schema::Query; /// # use juniper::tests::model::Database; /// # use juniper::{EmptyMutation, FieldError, RootNode, Value}; /// # - /// # type Schema = RootNode<'static, Database, EmptyMutation>; + /// # type Schema = RootNode<'static, Query, EmptyMutation>; /// # /// #[rocket::get("/graphql?")] /// fn get_graphql_handler( @@ -487,10 +488,11 @@ mod tests { use juniper::http::tests as http_tests; use juniper::tests::model::Database; + use juniper::tests::schema::Query; use juniper::EmptyMutation; use juniper::RootNode; - type Schema = RootNode<'static, Database, EmptyMutation>; + type Schema = RootNode<'static, Query, EmptyMutation>; #[get("/?")] fn get_graphql_handler( @@ -565,10 +567,9 @@ mod tests { } fn make_rocket_without_routes() -> Rocket { - rocket::ignite().manage(Database::new()).manage(Schema::new( - Database::new(), - EmptyMutation::::new(), - )) + rocket::ignite() + .manage(Database::new()) + .manage(Schema::new(Query, EmptyMutation::::new())) } fn make_test_response(request: &LocalRequest) -> http_tests::TestResponse { diff --git a/juniper_warp/examples/warp_server.rs b/juniper_warp/examples/warp_server.rs index d158f769..ffe87784 100644 --- a/juniper_warp/examples/warp_server.rs +++ b/juniper_warp/examples/warp_server.rs @@ -3,13 +3,14 @@ extern crate log; use juniper::tests::model::Database; +use juniper::tests::schema::Query; use juniper::{EmptyMutation, RootNode}; use warp::{http::Response, Filter}; -type Schema = RootNode<'static, Database, EmptyMutation>; +type Schema = RootNode<'static, Query, EmptyMutation>; fn schema() -> Schema { - Schema::new(Database::new(), EmptyMutation::::new()) + Schema::new(Query, EmptyMutation::::new()) } fn main() { diff --git a/juniper_warp/src/lib.rs b/juniper_warp/src/lib.rs index fe8224c1..78a368fe 100644 --- a/juniper_warp/src/lib.rs +++ b/juniper_warp/src/lib.rs @@ -391,11 +391,12 @@ mod tests { #[test] fn graphql_handler_works_json_post() { use juniper::tests::model::Database; + use juniper::tests::schema::Query; use juniper::{EmptyMutation, RootNode}; - type Schema = juniper::RootNode<'static, Database, EmptyMutation>; + type Schema = juniper::RootNode<'static, Query, EmptyMutation>; - let schema: Schema = RootNode::new(Database::new(), EmptyMutation::::new()); + let schema: Schema = RootNode::new(Query, EmptyMutation::::new()); let state = warp::any().map(move || Database::new()); let filter = warp::path("graphql2").and(make_graphql_filter(schema, state.boxed())); @@ -422,11 +423,12 @@ mod tests { #[test] fn batch_requests_work() { use juniper::tests::model::Database; + use juniper::tests::schema::Query; use juniper::{EmptyMutation, RootNode}; - type Schema = juniper::RootNode<'static, Database, EmptyMutation>; + type Schema = juniper::RootNode<'static, Query, EmptyMutation>; - let schema: Schema = RootNode::new(Database::new(), EmptyMutation::::new()); + let schema: Schema = RootNode::new(Query, EmptyMutation::::new()); let state = warp::any().map(move || Database::new()); let filter = warp::path("graphql2").and(make_graphql_filter(schema, state.boxed())); @@ -469,15 +471,16 @@ mod tests_http_harness { use super::*; use juniper::http::tests::{run_http_test_suite, HTTPIntegration, TestResponse}; use juniper::tests::model::Database; + use juniper::tests::schema::Query; use juniper::EmptyMutation; use juniper::RootNode; use warp; use warp::Filter; - type Schema = juniper::RootNode<'static, Database, EmptyMutation>; + type Schema = juniper::RootNode<'static, Query, EmptyMutation>; fn warp_server() -> warp::filters::BoxedFilter<(warp::http::Response>,)> { - let schema: Schema = RootNode::new(Database::new(), EmptyMutation::::new()); + let schema: Schema = RootNode::new(Query, EmptyMutation::::new()); let state = warp::any().map(move || Database::new()); let filter = warp::filters::path::end().and(make_graphql_filter(schema, state.boxed()));