-
Notifications
You must be signed in to change notification settings - Fork 422
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
244 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
mod enums; | ||
mod scalar; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,156 @@ | ||
use std::collections::HashMap; | ||
|
||
use executor::FieldResult; | ||
use value::Value; | ||
use schema::model::RootNode; | ||
|
||
struct DefaultName(i64); | ||
struct OtherOrder(i64); | ||
struct Named(i64); | ||
struct ScalarDescription(i64); | ||
|
||
struct Root; | ||
|
||
/* | ||
Syntax to validate: | ||
* Default name vs. custom name | ||
* Description vs. no description on the scalar | ||
*/ | ||
|
||
graphql_scalar!(DefaultName { | ||
resolve(&self) -> Value { | ||
Value::int(self.0) | ||
} | ||
|
||
from_input_value(v: &InputValue) -> Option<DefaultName> { | ||
v.as_int_value().map(|i| DefaultName(i)) | ||
} | ||
}); | ||
|
||
graphql_scalar!(OtherOrder { | ||
from_input_value(v: &InputValue) -> Option<OtherOrder> { | ||
v.as_int_value().map(|i| OtherOrder(i)) | ||
} | ||
|
||
resolve(&self) -> Value { | ||
Value::int(self.0) | ||
} | ||
}); | ||
|
||
graphql_scalar!(Named as "ANamedScalar" { | ||
resolve(&self) -> Value { | ||
Value::int(self.0) | ||
} | ||
|
||
from_input_value(v: &InputValue) -> Option<Named> { | ||
v.as_int_value().map(|i| Named(i)) | ||
} | ||
}); | ||
|
||
graphql_scalar!(ScalarDescription { | ||
description: "A sample scalar, represented as an integer" | ||
|
||
resolve(&self) -> Value { | ||
Value::int(self.0) | ||
} | ||
|
||
from_input_value(v: &InputValue) -> Option<ScalarDescription> { | ||
v.as_int_value().map(|i| ScalarDescription(i)) | ||
} | ||
}); | ||
|
||
graphql_object!(Root: () as "Root" |&self| { | ||
field default_name() -> FieldResult<DefaultName> { Ok(DefaultName(0)) } | ||
field other_order() -> FieldResult<OtherOrder> { Ok(OtherOrder(0)) } | ||
field named() -> FieldResult<Named> { Ok(Named(0)) } | ||
field scalar_description() -> FieldResult<ScalarDescription> { Ok(ScalarDescription(0)) } | ||
}); | ||
|
||
fn run_type_info_query<F>(doc: &str, f: F) where F: Fn(&HashMap<String, Value>) -> () { | ||
let schema = RootNode::new(Root {}, ()); | ||
|
||
let (result, errs) = ::execute(doc, None, &schema, &HashMap::new(), &()) | ||
.expect("Execution failed"); | ||
|
||
assert_eq!(errs, []); | ||
|
||
println!("Result: {:?}", result); | ||
|
||
let type_info = result | ||
.as_object_value().expect("Result is not an object") | ||
.get("__type").expect("__type field missing") | ||
.as_object_value().expect("__type field not an object value"); | ||
|
||
f(type_info); | ||
} | ||
|
||
#[test] | ||
fn default_name_introspection() { | ||
let doc = r#" | ||
{ | ||
__type(name: "DefaultName") { | ||
name | ||
description | ||
} | ||
} | ||
"#; | ||
|
||
run_type_info_query(doc, |type_info| { | ||
assert_eq!(type_info.get("name"), Some(&Value::string("DefaultName"))); | ||
assert_eq!(type_info.get("description"), Some(&Value::null())); | ||
}); | ||
} | ||
|
||
#[test] | ||
fn other_order_introspection() { | ||
let doc = r#" | ||
{ | ||
__type(name: "OtherOrder") { | ||
name | ||
description | ||
} | ||
} | ||
"#; | ||
|
||
run_type_info_query(doc, |type_info| { | ||
assert_eq!(type_info.get("name"), Some(&Value::string("OtherOrder"))); | ||
assert_eq!(type_info.get("description"), Some(&Value::null())); | ||
}); | ||
} | ||
|
||
#[test] | ||
fn named_introspection() { | ||
let doc = r#" | ||
{ | ||
__type(name: "ANamedScalar") { | ||
name | ||
description | ||
} | ||
} | ||
"#; | ||
|
||
run_type_info_query(doc, |type_info| { | ||
assert_eq!(type_info.get("name"), Some(&Value::string("ANamedScalar"))); | ||
assert_eq!(type_info.get("description"), Some(&Value::null())); | ||
}); | ||
} | ||
|
||
#[test] | ||
fn scalar_description_introspection() { | ||
let doc = r#" | ||
{ | ||
__type(name: "ScalarDescription") { | ||
name | ||
description | ||
} | ||
} | ||
"#; | ||
|
||
run_type_info_query(doc, |type_info| { | ||
assert_eq!(type_info.get("name"), Some(&Value::string("ScalarDescription"))); | ||
assert_eq!(type_info.get("description"), Some(&Value::string("A sample scalar, represented as an integer"))); | ||
}); | ||
} |