Use object shorthand in tests and documentation

This commit is contained in:
Magnus Hallin 2016-10-20 20:56:23 +02:00
parent 2b1c9cf906
commit ba7839f321
12 changed files with 47 additions and 47 deletions

View file

@ -72,7 +72,7 @@ graphql_enum!(Episode {
Episode::Jedi => "JEDI",
});
graphql_object!(Human: () as "Human" |&self| {
graphql_object!(Human: () |&self| {
description: "A humanoid creature in the Star Wars universe"
// Field resolver methods look almost like ordinary methods. The macro picks

View file

@ -41,7 +41,7 @@ graphql_interface!(Interface: () as "SampleInterface" |&self| {
}
});
graphql_object!(Root: () as "Root" |&self| {
graphql_object!(Root: () |&self| {
description: "The root query object in the schema"
interfaces: [Interface]

View file

@ -43,7 +43,7 @@ struct Database { users: HashMap<String, User> }
// object to provide e.g. database access to the field accessors.
//
// In this example, we use the Database struct as our context.
graphql_object!(User: Database as "User" |&self| {
graphql_object!(User: Database |&self| {
// Expose a simple field as a GraphQL string.
field id() -> &String {
@ -76,7 +76,7 @@ graphql_object!(User: Database as "User" |&self| {
// The context object is passed down to all referenced types - all your exposed
// types need to have the same context type.
graphql_object!(QueryRoot: Database as "Query" |&self| {
graphql_object!(QueryRoot: Database |&self| {
// Arguments work just like they do on functions.
field user(&mut executor, id: String) -> Option<&User> {
@ -112,7 +112,7 @@ use juniper::iron_handlers::GraphQLHandler;
# struct QueryRoot;
# struct Database { users: HashMap<String, User> }
#
# graphql_object!(User: Database as "User" |&self| {
# graphql_object!(User: Database |&self| {
# field id() -> FieldResult<&String> {
# Ok(&self.id)
# }
@ -128,7 +128,7 @@ use juniper::iron_handlers::GraphQLHandler;
# }
# });
#
# graphql_object!(QueryRoot: Database as "Query" |&self| {
# graphql_object!(QueryRoot: Database |&self| {
# field user(&mut executor, id: String) -> FieldResult<Option<&User>> {
# Ok(executor.context().users.get(&id))
# }

View file

@ -13,7 +13,7 @@ The simplest case exposes fields on a struct:
# #[macro_use] extern crate juniper;
struct User { id: String, name: String, group_ids: Vec<String> }
graphql_object!(User: () as "User" |&self| {
graphql_object!(User: () |&self| {
field id() -> &String {
&self.id
}
@ -42,7 +42,7 @@ arguments:
# #[macro_use] extern crate juniper;
struct User { id: String, name: String, group_ids: Vec<String> }
graphql_object!(User: () as "User" |&self| {
graphql_object!(User: () |&self| {
description: "A user in the database"
field id() -> &String as "The user's unique identifier" {
@ -105,7 +105,7 @@ graphql_interface!(<'a> &'a Interface: () as "Interface" |&self| {
}
});
graphql_object!(Implementor: () as "Implementor" |&self| {
graphql_object!(Implementor: () |&self| {
field id() -> &str { &self.id }
interfaces: [&Interface]
@ -130,7 +130,7 @@ continue executing despite some fields failing.
# use juniper::FieldResult;
struct User { id: String }
graphql_object!(User: () as "User" |&self| {
graphql_object!(User: () |&self| {
field id() -> FieldResult<&String> {
Ok(&self.id)
}

View file

@ -18,7 +18,7 @@ Syntax to validate:
*/
graphql_object!(Root: () as "Root" |&self| {
graphql_object!(Root: () |&self| {
field simple() -> i64 { 0 }
field exec_arg(&mut executor) -> i64 { 0 }
field exec_arg_and_more(&mut executor, arg: i64) -> i64 { 0 }

View file

@ -57,7 +57,7 @@ graphql_enum!(EnumDeprecation {
EnumDeprecation::Bar => "BAR" as "The BAR value" deprecated "Please don't use BAR any more",
});
graphql_object!(Root: () as "Root" |&self| {
graphql_object!(Root: () |&self| {
field default_name() -> DefaultName { DefaultName::Foo }
field named() -> Named { Named::Foo }
field no_trailing_comma() -> NoTrailingComma { NoTrailingComma::Foo }

View file

@ -17,7 +17,7 @@ Syntax to validate:
*/
graphql_object!(Root: () as "Root" |&self| {
graphql_object!(Root: () |&self| {
field simple() -> i64 { 0 }
field description() -> i64 as "Field description" { 0 }
@ -31,7 +31,7 @@ graphql_object!(Root: () as "Root" |&self| {
interfaces: [Interface]
});
graphql_interface!(Interface: () as "Interface" |&self| {
graphql_interface!(Interface: () |&self| {
field simple() -> i64 { 0 }
field description() -> i64 as "Field description" { 0 }

View file

@ -48,7 +48,7 @@ graphql_input_object!(
}
);
graphql_object!(Root: () as "Root" |&self| {
graphql_object!(Root: () |&self| {
field test_field(
a1: DefaultName,
a2: NoTrailingComma,

View file

@ -19,7 +19,7 @@ Syntax to validate:
struct Concrete;
struct DefaultName;
struct CustomName;
#[allow(dead_code)]
struct WithLifetime<'a> { data: PhantomData<&'a i64> }
@ -42,7 +42,7 @@ graphql_object!(Concrete: () |&self| {
field simple() -> i64 { 0 }
});
graphql_interface!(DefaultName: () |&self| {
graphql_interface!(CustomName: () as "ACustomNamedInterface" |&self| {
field simple() -> i64 { 0 }
instance_resolvers: |_| { Concrete => Some(Concrete) }
@ -60,7 +60,7 @@ graphql_interface!(<T> WithGenerics<T>: () as "WithGenerics" |&self| {
});
graphql_interface!(DescriptionFirst: () as "DescriptionFirst" |&self| {
graphql_interface!(DescriptionFirst: () |&self| {
description: "A description"
field simple() -> i64 { 0 }
@ -68,7 +68,7 @@ graphql_interface!(DescriptionFirst: () as "DescriptionFirst" |&self| {
instance_resolvers: |_| { Concrete => Some(Concrete) }
});
graphql_interface!(FieldsFirst: () as "FieldsFirst" |&self| {
graphql_interface!(FieldsFirst: () |&self| {
field simple() -> i64 { 0 }
description: "A description"
@ -76,7 +76,7 @@ graphql_interface!(FieldsFirst: () as "FieldsFirst" |&self| {
instance_resolvers: |_| { Concrete => Some(Concrete) }
});
graphql_interface!(InterfacesFirst: () as "InterfacesFirst" |&self| {
graphql_interface!(InterfacesFirst: () |&self| {
instance_resolvers: |_| { Concrete => Some(Concrete) }
field simple() -> i64 { 0 }
@ -84,7 +84,7 @@ graphql_interface!(InterfacesFirst: () as "InterfacesFirst" |&self| {
description: "A description"
});
graphql_interface!(CommasWithTrailing: () as "CommasWithTrailing" |&self| {
graphql_interface!(CommasWithTrailing: () |&self| {
instance_resolvers: |_| { Concrete => Some(Concrete) },
field simple() -> i64 { 0 },
@ -93,7 +93,7 @@ graphql_interface!(CommasWithTrailing: () as "CommasWithTrailing" |&self| {
});
graphql_interface!(CommasOnMeta: () as "CommasOnMeta" |&self| {
graphql_interface!(CommasOnMeta: () |&self| {
instance_resolvers: |_| { Concrete => Some(Concrete) }
description: "A description",
@ -101,7 +101,7 @@ graphql_interface!(CommasOnMeta: () as "CommasOnMeta" |&self| {
});
graphql_interface!(ResolversWithTrailingComma: () as "ResolversWithTrailingComma" |&self| {
graphql_interface!(ResolversWithTrailingComma: () |&self| {
instance_resolvers: |_| { Concrete => Some(Concrete), }
description: "A description",
@ -109,7 +109,7 @@ graphql_interface!(ResolversWithTrailingComma: () as "ResolversWithTrailingComma
});
graphql_object!(<'a> Root: () as "Root" |&self| {
field default_name() -> DefaultName { DefaultName {} }
field custom_name() -> CustomName { CustomName {} }
field with_lifetime() -> WithLifetime<'a> { WithLifetime { data: PhantomData } }
field with_generics() -> WithGenerics<i64> { WithGenerics { data: 123 } }
@ -167,9 +167,9 @@ fn run_type_info_query<F>(type_name: &str, f: F)
}
#[test]
fn introspect_default_name() {
run_type_info_query("DefaultName", |object, fields| {
assert_eq!(object.get("name"), Some(&Value::string("DefaultName")));
fn introspect_custom_name() {
run_type_info_query("ACustomNamedInterface", |object, fields| {
assert_eq!(object.get("name"), Some(&Value::string("ACustomNamedInterface")));
assert_eq!(object.get("description"), Some(&Value::null()));
assert!(fields.contains(&Value::object(vec![

View file

@ -18,7 +18,7 @@ Syntax to validate:
struct Interface;
struct DefaultName;
struct CustomName;
#[allow(dead_code)]
struct WithLifetime<'a> { data: PhantomData<&'a i64> }
@ -35,7 +35,7 @@ struct CommasOnMeta;
struct Root;
graphql_object!(DefaultName: () |&self| {
graphql_object!(CustomName: () as "ACustomNamedType" |&self| {
field simple() -> i64 { 0 }
});
@ -49,7 +49,7 @@ graphql_object!(<T> WithGenerics<T>: () as "WithGenerics" |&self| {
});
graphql_interface!(Interface: () as "Interface" |&self| {
graphql_interface!(Interface: () |&self| {
field simple() -> i64 { 0 }
instance_resolvers: |_| {
@ -57,7 +57,7 @@ graphql_interface!(Interface: () as "Interface" |&self| {
}
});
graphql_object!(DescriptionFirst: () as "DescriptionFirst" |&self| {
graphql_object!(DescriptionFirst: () |&self| {
description: "A description"
field simple() -> i64 { 0 }
@ -65,7 +65,7 @@ graphql_object!(DescriptionFirst: () as "DescriptionFirst" |&self| {
interfaces: [Interface]
});
graphql_object!(FieldsFirst: () as "FieldsFirst" |&self| {
graphql_object!(FieldsFirst: () |&self| {
field simple() -> i64 { 0 }
description: "A description"
@ -73,7 +73,7 @@ graphql_object!(FieldsFirst: () as "FieldsFirst" |&self| {
interfaces: [Interface]
});
graphql_object!(InterfacesFirst: () as "InterfacesFirst" |&self| {
graphql_object!(InterfacesFirst: ()|&self| {
interfaces: [Interface]
field simple() -> i64 { 0 }
@ -81,7 +81,7 @@ graphql_object!(InterfacesFirst: () as "InterfacesFirst" |&self| {
description: "A description"
});
graphql_object!(CommasWithTrailing: () as "CommasWithTrailing" |&self| {
graphql_object!(CommasWithTrailing: () |&self| {
interfaces: [Interface],
field simple() -> i64 { 0 },
@ -90,7 +90,7 @@ graphql_object!(CommasWithTrailing: () as "CommasWithTrailing" |&self| {
});
graphql_object!(CommasOnMeta: () as "CommasOnMeta" |&self| {
graphql_object!(CommasOnMeta: () |&self| {
interfaces: [Interface],
description: "A description",
@ -98,7 +98,7 @@ graphql_object!(CommasOnMeta: () as "CommasOnMeta" |&self| {
});
graphql_object!(<'a> Root: () as "Root" |&self| {
field default_name() -> DefaultName { DefaultName {} }
field custom_name() -> CustomName { CustomName {} }
field with_lifetime() -> WithLifetime<'a> { WithLifetime { data: PhantomData } }
field with_generics() -> WithGenerics<i64> { WithGenerics { data: 123 } }
@ -155,9 +155,9 @@ fn run_type_info_query<F>(type_name: &str, f: F)
}
#[test]
fn introspect_default_name() {
run_type_info_query("DefaultName", |object, fields| {
assert_eq!(object.get("name"), Some(&Value::string("DefaultName")));
fn introspect_custom_name() {
run_type_info_query("ACustomNamedType", |object, fields| {
assert_eq!(object.get("name"), Some(&Value::string("ACustomNamedType")));
assert_eq!(object.get("description"), Some(&Value::null()));
assert_eq!(object.get("interfaces"), Some(&Value::list(vec![])));

View file

@ -61,7 +61,7 @@ graphql_scalar!(ScalarDescription {
}
});
graphql_object!(Root: () as "Root" |&self| {
graphql_object!(Root: () |&self| {
field default_name() -> DefaultName { DefaultName(0) }
field other_order() -> OtherOrder { OtherOrder(0) }
field named() -> Named { Named(0) }

View file

@ -19,7 +19,7 @@ Syntax to validate:
struct Concrete;
enum DefaultName { Concrete(Concrete) }
enum CustomName { Concrete(Concrete) }
enum WithLifetime<'a> { Int(PhantomData<&'a i64>) }
enum WithGenerics<T> { Generic(T) }
@ -36,9 +36,9 @@ graphql_object!(Concrete: () |&self| {
field simple() -> i64 { 123 }
});
graphql_union!(DefaultName: () |&self| {
graphql_union!(CustomName: () as "ACustomNamedUnion" |&self| {
instance_resolvers: |&_| {
&Concrete => match *self { DefaultName::Concrete(ref c) => Some(c) }
&Concrete => match *self { CustomName::Concrete(ref c) => Some(c) }
}
});
@ -83,7 +83,7 @@ graphql_union!(ResolversWithTrailingComma: () |&self| {
});
graphql_object!(<'a> Root: () as "Root" |&self| {
field default_name() -> DefaultName { DefaultName::Concrete(Concrete) }
field custom_name() -> CustomName { CustomName::Concrete(Concrete) }
field with_lifetime() -> WithLifetime<'a> { WithLifetime::Int(PhantomData) }
field with_generics() -> WithGenerics<i64> { WithGenerics::Generic(123) }
field description_first() -> DescriptionFirst { DescriptionFirst::Concrete(Concrete) }
@ -135,9 +135,9 @@ fn run_type_info_query<F>(type_name: &str, f: F)
#[test]
fn introspect_default_name() {
run_type_info_query("DefaultName", |union, possible_types| {
assert_eq!(union.get("name"), Some(&Value::string("DefaultName")));
fn introspect_custom_name() {
run_type_info_query("ACustomNamedUnion", |union, possible_types| {
assert_eq!(union.get("name"), Some(&Value::string("ACustomNamedUnion")));
assert_eq!(union.get("description"), Some(&Value::null()));
assert!(possible_types.contains(&Value::object(vec![