Split Query and Database types in example schema (#360)

This commit is contained in:
Andrey Kutejko 2019-05-18 18:07:51 +02:00 committed by Christian Legnitto
parent c93911958a
commit 5b9a0bd31b
12 changed files with 68 additions and 59 deletions

View file

@ -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::<Database>::new());
let schema = RootNode::new(Query, EmptyMutation::<Database>::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::<Database>::new());
let schema = RootNode::new(Query, EmptyMutation::<Database>::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::<Database>::new());
let schema = RootNode::new(Query, EmptyMutation::<Database>::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::<Database>::new());
let schema = RootNode::new(Query, EmptyMutation::<Database>::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::<Database>::new());
let schema = RootNode::new(Query, EmptyMutation::<Database>::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::<Database>::new());
let schema = RootNode::new(Query, EmptyMutation::<Database>::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::<Database>::new());
let schema = RootNode::new(Query, EmptyMutation::<Database>::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::<Database>::new());
let schema = RootNode::new(Query, EmptyMutation::<Database>::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::<Database>::new());
let schema = RootNode::new(Query, EmptyMutation::<Database>::new());
let mut result =
crate::introspect(&schema, &database, IntrospectionFormat::WithoutDescriptions).unwrap();

View file

@ -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)]

View file

@ -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::<Database>::new());
let schema = RootNode::new(Query, EmptyMutation::<Database>::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::<Database>::new());
let schema = RootNode::new(Query, EmptyMutation::<Database>::new());
let doc = r#"
{
@ -109,7 +110,7 @@ fn test_hero_name_and_friends() {
}
}"#;
let database = Database::new();
let schema = RootNode::new(&database, EmptyMutation::<Database>::new());
let schema = RootNode::new(Query, EmptyMutation::<Database>::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::<Database>::new());
let schema = RootNode::new(Query, EmptyMutation::<Database>::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::<Database>::new());
let schema = RootNode::new(Query, EmptyMutation::<Database>::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::<Database>::new());
let schema = RootNode::new(Query, EmptyMutation::<Database>::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::<Database>::new());
let schema = RootNode::new(Query, EmptyMutation::<Database>::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::<Database>::new());
let schema = RootNode::new(Query, EmptyMutation::<Database>::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::<Database>::new());
let schema = RootNode::new(Query, EmptyMutation::<Database>::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::<Database>::new());
let schema = RootNode::new(Query, EmptyMutation::<Database>::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::<Database>::new());
let schema = RootNode::new(Query, EmptyMutation::<Database>::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::<Database>::new());
let schema = RootNode::new(Query, EmptyMutation::<Database>::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::<Database>::new());
let schema = RootNode::new(Query, EmptyMutation::<Database>::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::<Database>::new());
let schema = RootNode::new(Query, EmptyMutation::<Database>::new());
assert_eq!(
crate::execute(doc, None, &schema, &Variables::new(), &database),

View file

@ -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<Episode>) -> Option<&Character> {
Some(self.get_hero(episode).as_character())
fn hero(database: &Database, episode: Option<Episode>) -> Option<&Character> {
Some(database.get_hero(episode).as_character())
}
}

View file

@ -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::<Database>::new()));
let root_node = Arc::new(RootNode::new(Query, EmptyMutation::<Database>::new()));
let new_service = move || {
let root_node = root_node.clone();

View file

@ -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::<Database>::new()));
let root_node = Arc::new(RootNode::new(Query, EmptyMutation::<Database>::new()));
let new_service = move || {
let root_node = root_node.clone();

View file

@ -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<Database> {
fn main() {
let mut mount = Mount::new();
let graphql_endpoint = GraphQLHandler::new(
context_factory,
Database::new(),
EmptyMutation::<Database>::new(),
);
let graphql_endpoint =
GraphQLHandler::new(context_factory, Query, EmptyMutation::<Database>::new());
let graphiql_endpoint = GraphiQLHandler::new("/graphql");
mount.mount("/", graphiql_endpoint);

View file

@ -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<Handler> {
Box::new(GraphQLHandler::new(
context_factory,
Database::new(),
Query,
EmptyMutation::<Database>::new(),
))
}

View file

@ -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<Database>>;
type Schema = RootNode<'static, Query, EmptyMutation<Database>>;
#[rocket::get("/")]
fn graphiql() -> content::Html<String> {
@ -34,10 +35,7 @@ fn post_graphql_handler(
fn main() {
rocket::ignite()
.manage(Database::new())
.manage(Schema::new(
Database::new(),
EmptyMutation::<Database>::new(),
))
.manage(Schema::new(Query, EmptyMutation::<Database>::new()))
.mount(
"/",
rocket::routes![graphiql, get_graphql_handler, post_graphql_handler],

View file

@ -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<Database>>;
/// # type Schema = RootNode<'static, Query, EmptyMutation<Database>>;
/// #
/// #[rocket::get("/graphql?<request..>")]
/// 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<Database>>;
type Schema = RootNode<'static, Query, EmptyMutation<Database>>;
#[get("/?<request..>")]
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::<Database>::new(),
))
rocket::ignite()
.manage(Database::new())
.manage(Schema::new(Query, EmptyMutation::<Database>::new()))
}
fn make_test_response(request: &LocalRequest) -> http_tests::TestResponse {

View file

@ -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<Database>>;
type Schema = RootNode<'static, Query, EmptyMutation<Database>>;
fn schema() -> Schema {
Schema::new(Database::new(), EmptyMutation::<Database>::new())
Schema::new(Query, EmptyMutation::<Database>::new())
}
fn main() {

View file

@ -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<Database>>;
type Schema = juniper::RootNode<'static, Query, EmptyMutation<Database>>;
let schema: Schema = RootNode::new(Database::new(), EmptyMutation::<Database>::new());
let schema: Schema = RootNode::new(Query, EmptyMutation::<Database>::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<Database>>;
type Schema = juniper::RootNode<'static, Query, EmptyMutation<Database>>;
let schema: Schema = RootNode::new(Database::new(), EmptyMutation::<Database>::new());
let schema: Schema = RootNode::new(Query, EmptyMutation::<Database>::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<Database>>;
type Schema = juniper::RootNode<'static, Query, EmptyMutation<Database>>;
fn warp_server() -> warp::filters::BoxedFilter<(warp::http::Response<Vec<u8>>,)> {
let schema: Schema = RootNode::new(Database::new(), EmptyMutation::<Database>::new());
let schema: Schema = RootNode::new(Query, EmptyMutation::<Database>::new());
let state = warp::any().map(move || Database::new());
let filter = warp::filters::path::end().and(make_graphql_filter(schema, state.boxed()));