Integration tests: impl_object refactor
This commit is contained in:
parent
a993c16b85
commit
bf50c3eb86
8 changed files with 109 additions and 64 deletions
|
@ -4,15 +4,7 @@ version = "0.1.0"
|
||||||
publish = false
|
publish = false
|
||||||
edition = "2018"
|
edition = "2018"
|
||||||
|
|
||||||
[dependencies]
|
|
||||||
juniper = { version = "0.11.0", path = "../../juniper" }
|
|
||||||
serde_json = { version = "1" }
|
|
||||||
|
|
||||||
[dev-dependencies]
|
[dev-dependencies]
|
||||||
|
juniper = { path = "../../juniper" }
|
||||||
|
serde_json = { version = "1" }
|
||||||
fnv = "1.0.3"
|
fnv = "1.0.3"
|
||||||
indexmap = "1.0"
|
|
||||||
|
|
||||||
[[test]]
|
|
||||||
name = "integration_tests"
|
|
||||||
path = "src/lib.rs"
|
|
||||||
harness = true
|
|
||||||
|
|
|
@ -19,7 +19,7 @@ enum DocEnum {
|
||||||
Foo,
|
Foo,
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Doc 1.
|
/// Doc 1.\
|
||||||
/// Doc 2.
|
/// Doc 2.
|
||||||
///
|
///
|
||||||
/// Doc 4.
|
/// Doc 4.
|
||||||
|
@ -85,7 +85,7 @@ fn test_multi_doc_comment() {
|
||||||
let meta = MultiDocEnum::meta(&(), &mut registry);
|
let meta = MultiDocEnum::meta(&(), &mut registry);
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
meta.description(),
|
meta.description(),
|
||||||
Some(&"Doc 1. Doc 2.\nDoc 4.".to_string())
|
Some(&"Doc 1. Doc 2.\n\nDoc 4.".to_string())
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -27,7 +27,7 @@ struct DocComment {
|
||||||
regular_field: bool,
|
regular_field: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Doc 1.
|
/// Doc 1.\
|
||||||
/// Doc 2.
|
/// Doc 2.
|
||||||
///
|
///
|
||||||
/// Doc 4.
|
/// Doc 4.
|
||||||
|
@ -151,7 +151,7 @@ fn test_multi_doc_comment() {
|
||||||
let meta = MultiDocComment::meta(&(), &mut registry);
|
let meta = MultiDocComment::meta(&(), &mut registry);
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
meta.description(),
|
meta.description(),
|
||||||
Some(&"Doc 1. Doc 2.\nDoc 4.".to_string())
|
Some(&"Doc 1. Doc 2.\n\nDoc 4.".to_string())
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -12,20 +12,20 @@ use juniper::{self, execute, EmptyMutation, GraphQLType, RootNode, Value, Variab
|
||||||
#[graphql(
|
#[graphql(
|
||||||
name = "MyObj",
|
name = "MyObj",
|
||||||
description = "obj descr",
|
description = "obj descr",
|
||||||
scalar = "DefaultScalarValue"
|
scalar = DefaultScalarValue
|
||||||
)]
|
)]
|
||||||
struct Obj {
|
struct Obj {
|
||||||
regular_field: bool,
|
regular_field: bool,
|
||||||
#[graphql(
|
#[graphql(
|
||||||
name = "renamedField",
|
name = "renamedField",
|
||||||
description = "descr",
|
description = "descr",
|
||||||
deprecation = "field descr"
|
deprecated = "field deprecation"
|
||||||
)]
|
)]
|
||||||
c: i32,
|
c: i32,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(GraphQLObject, Debug, PartialEq)]
|
#[derive(GraphQLObject, Debug, PartialEq)]
|
||||||
#[graphql(scalar = "DefaultScalarValue")]
|
#[graphql(scalar = DefaultScalarValue)]
|
||||||
struct Nested {
|
struct Nested {
|
||||||
obj: Obj,
|
obj: Obj,
|
||||||
}
|
}
|
||||||
|
@ -39,7 +39,7 @@ struct DocComment {
|
||||||
regular_field: bool,
|
regular_field: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Doc 1.
|
/// Doc 1.\
|
||||||
/// Doc 2.
|
/// Doc 2.
|
||||||
///
|
///
|
||||||
/// Doc 4.
|
/// Doc 4.
|
||||||
|
@ -75,56 +75,57 @@ struct Context;
|
||||||
impl juniper::Context for Context {}
|
impl juniper::Context for Context {}
|
||||||
|
|
||||||
#[derive(GraphQLObject, Debug)]
|
#[derive(GraphQLObject, Debug)]
|
||||||
#[graphql(Context = "Context")]
|
#[graphql(Context = Context)]
|
||||||
struct WithCustomContext {
|
struct WithCustomContext {
|
||||||
a: bool,
|
a: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
juniper::graphql_object!(Query: () |&self| {
|
#[juniper::impl_object]
|
||||||
field obj() -> Obj {
|
impl Query {
|
||||||
Obj{
|
fn obj() -> Obj {
|
||||||
regular_field: true,
|
Obj {
|
||||||
c: 22,
|
regular_field: true,
|
||||||
}
|
c: 22,
|
||||||
}
|
|
||||||
|
|
||||||
field nested() -> Nested {
|
|
||||||
Nested{
|
|
||||||
obj: Obj{
|
|
||||||
regular_field: false,
|
|
||||||
c: 333,
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
field doc() -> DocComment {
|
fn nested() -> Nested {
|
||||||
DocComment{
|
Nested {
|
||||||
regular_field: true,
|
obj: Obj {
|
||||||
}
|
regular_field: false,
|
||||||
|
c: 333,
|
||||||
|
},
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
field multi_doc() -> MultiDocComment {
|
fn doc() -> DocComment {
|
||||||
MultiDocComment{
|
DocComment {
|
||||||
regular_field: true,
|
regular_field: true,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
field override_doc() -> OverrideDocComment {
|
fn multi_doc() -> MultiDocComment {
|
||||||
OverrideDocComment{
|
MultiDocComment {
|
||||||
regular_field: true,
|
regular_field: true,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
field skipped_field_obj() -> SkippedFieldObj {
|
fn override_doc() -> OverrideDocComment {
|
||||||
SkippedFieldObj{
|
OverrideDocComment {
|
||||||
|
regular_field: true,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn skipped_field_obj() -> SkippedFieldObj {
|
||||||
|
SkippedFieldObj {
|
||||||
regular_field: false,
|
regular_field: false,
|
||||||
skipped: 42,
|
skipped: 42,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_doc_comment() {
|
fn test_doc_comment_simple() {
|
||||||
let mut registry: juniper::Registry = juniper::Registry::new(FnvHashMap::default());
|
let mut registry: juniper::Registry = juniper::Registry::new(FnvHashMap::default());
|
||||||
let meta = DocComment::meta(&(), &mut registry);
|
let meta = DocComment::meta(&(), &mut registry);
|
||||||
assert_eq!(meta.description(), Some(&"Object comment.".to_string()));
|
assert_eq!(meta.description(), Some(&"Object comment.".to_string()));
|
||||||
|
@ -143,14 +144,14 @@ fn test_multi_doc_comment() {
|
||||||
let meta = MultiDocComment::meta(&(), &mut registry);
|
let meta = MultiDocComment::meta(&(), &mut registry);
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
meta.description(),
|
meta.description(),
|
||||||
Some(&"Doc 1. Doc 2.\nDoc 4.".to_string())
|
Some(&"Doc 1. Doc 2.\n\nDoc 4.".to_string())
|
||||||
);
|
);
|
||||||
|
|
||||||
check_descriptions(
|
check_descriptions(
|
||||||
"MultiDocComment",
|
"MultiDocComment",
|
||||||
&Value::scalar("Doc 1. Doc 2.\nDoc 4."),
|
&Value::scalar("Doc 1. Doc 2.\n\nDoc 4."),
|
||||||
"regularField",
|
"regularField",
|
||||||
&Value::scalar("Field 1. Field 2."),
|
&Value::scalar("Field 1.\nField 2."),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,3 +1,5 @@
|
||||||
|
mod util;
|
||||||
|
|
||||||
mod derive_enum;
|
mod derive_enum;
|
||||||
mod derive_input_object;
|
mod derive_input_object;
|
||||||
mod derive_object;
|
mod derive_object;
|
||||||
|
|
54
integration_tests/juniper_tests/src/codegen/util.rs
Normal file
54
integration_tests/juniper_tests/src/codegen/util.rs
Normal file
|
@ -0,0 +1,54 @@
|
||||||
|
use juniper::{DefaultScalarValue, GraphQLType, RootNode, Value, Variables};
|
||||||
|
use std::default::Default;
|
||||||
|
|
||||||
|
pub fn run_query<Query, Mutation, Context>(query: &str) -> Value
|
||||||
|
where
|
||||||
|
Query: GraphQLType<DefaultScalarValue, TypeInfo = (), Context = Context> + Default,
|
||||||
|
Mutation: GraphQLType<DefaultScalarValue, TypeInfo = (), Context = Context> + Default,
|
||||||
|
Context: Default,
|
||||||
|
{
|
||||||
|
let schema = RootNode::new(Query::default(), Mutation::default());
|
||||||
|
let (result, errs) =
|
||||||
|
juniper::execute(query, None, &schema, &Variables::new(), &Context::default())
|
||||||
|
.expect("Execution failed");
|
||||||
|
|
||||||
|
assert_eq!(errs, []);
|
||||||
|
result
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn run_info_query<Query, Mutation, Context>(type_name: &str) -> Value
|
||||||
|
where
|
||||||
|
Query: GraphQLType<DefaultScalarValue, TypeInfo = (), Context = Context> + Default,
|
||||||
|
Mutation: GraphQLType<DefaultScalarValue, TypeInfo = (), Context = Context> + Default,
|
||||||
|
Context: Default,
|
||||||
|
{
|
||||||
|
let query = format!(
|
||||||
|
r#"
|
||||||
|
{{
|
||||||
|
__type(name: "{}") {{
|
||||||
|
name,
|
||||||
|
description,
|
||||||
|
fields {{
|
||||||
|
name
|
||||||
|
description
|
||||||
|
args {{
|
||||||
|
name
|
||||||
|
description
|
||||||
|
type {{
|
||||||
|
name
|
||||||
|
}}
|
||||||
|
}}
|
||||||
|
}}
|
||||||
|
}}
|
||||||
|
}}
|
||||||
|
"#,
|
||||||
|
type_name
|
||||||
|
);
|
||||||
|
let result = run_query::<Query, Mutation, Context>(&query);
|
||||||
|
result
|
||||||
|
.as_object_value()
|
||||||
|
.expect("Result is not an object")
|
||||||
|
.get_field_value("__type")
|
||||||
|
.expect("__type field missing")
|
||||||
|
.clone()
|
||||||
|
}
|
|
@ -151,15 +151,18 @@ juniper::graphql_scalar!(i64 as "Long" where Scalar = MyScalarValue {
|
||||||
|
|
||||||
struct TestType;
|
struct TestType;
|
||||||
|
|
||||||
juniper::graphql_object!(TestType: () where Scalar = MyScalarValue |&self| {
|
#[juniper::impl_object(
|
||||||
field long_field() -> i64 {
|
Scalar = MyScalarValue
|
||||||
|
)]
|
||||||
|
impl TestType {
|
||||||
|
fn long_field() -> i64 {
|
||||||
(::std::i32::MAX as i64) + 1
|
(::std::i32::MAX as i64) + 1
|
||||||
}
|
}
|
||||||
|
|
||||||
field long_with_arg(long_arg: i64) -> i64 {
|
fn long_with_arg(long_arg: i64) -> i64 {
|
||||||
long_arg
|
long_arg
|
||||||
}
|
}
|
||||||
});
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
fn run_variable_query<F>(query: &str, vars: Variables<MyScalarValue>, f: F)
|
fn run_variable_query<F>(query: &str, vars: Variables<MyScalarValue>, f: F)
|
||||||
|
|
|
@ -1,11 +1,4 @@
|
||||||
extern crate juniper;
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
extern crate serde_json;
|
|
||||||
|
|
||||||
#[cfg(test)]
|
|
||||||
extern crate fnv;
|
|
||||||
#[cfg(test)]
|
|
||||||
extern crate indexmap;
|
|
||||||
|
|
||||||
mod codegen;
|
mod codegen;
|
||||||
|
#[cfg(test)]
|
||||||
mod custom_scalar;
|
mod custom_scalar;
|
||||||
|
|
Loading…
Reference in a new issue