book: Fix union and scalar code examples

This commit is contained in:
Christoph Herzog 2019-11-16 02:21:28 +01:00
parent 9ce3d04007
commit 46590ae884
3 changed files with 42 additions and 26 deletions

View file

@ -49,7 +49,7 @@ impl Character for Droid {
fn as_droid(&self) -> Option<&Droid> { Some(&self) } fn as_droid(&self) -> Option<&Droid> { Some(&self) }
} }
juniper::graphql_interface!(<'a> &'a Character: () as "Character" where Scalar = <S> |&self| { juniper::graphql_interface!(<'a> &'a dyn Character: () as "Character" where Scalar = <S> |&self| {
field id() -> &str { self.id() } field id() -> &str { self.id() }
instance_resolvers: |_| { instance_resolvers: |_| {
@ -111,7 +111,7 @@ impl Character for Droid {
fn id(&self) -> &str { self.id.as_str() } fn id(&self) -> &str { self.id.as_str() }
} }
juniper::graphql_interface!(<'a> &'a Character: Database as "Character" where Scalar = <S> |&self| { juniper::graphql_interface!(<'a> &'a dyn Character: Database as "Character" where Scalar = <S> |&self| {
field id() -> &str { self.id() } field id() -> &str { self.id() }
instance_resolvers: |&context| { instance_resolvers: |&context| {

View file

@ -121,8 +121,9 @@ juniper::graphql_scalar!(Date where Scalar = <S> {
// Define how to parse a primitive type into your custom scalar. // Define how to parse a primitive type into your custom scalar.
from_input_value(v: &InputValue) -> Option<Date> { from_input_value(v: &InputValue) -> Option<Date> {
v.as_scalar_value::<String>() v.as_scalar_value()
.and_then(|s| s.parse().ok()) .and_then(|v| v.as_str())
.and_then(|s| s.parse().ok())
} }
// Define how to parse a string value. // Define how to parse a string value.

View file

@ -42,14 +42,15 @@ impl Character for Droid {
fn as_droid(&self) -> Option<&Droid> { Some(&self) } fn as_droid(&self) -> Option<&Droid> { Some(&self) }
} }
juniper::graphql_union!(<'a> &'a Character: () as "Character" where Scalar = <S> |&self| { #[juniper::graphql_union]
instance_resolvers: |_| { impl<'a> GraphQLUnion for &'a dyn Character {
// The left hand side indicates the concrete type T, the right hand fn resolve(&self) {
// side should be an expression returning Option<T> match self {
&Human => self.as_human(), Human => self.as_human(),
&Droid => self.as_droid(), Droid => self.as_droid(),
}
} }
}); }
# fn main() {} # fn main() {}
``` ```
@ -93,12 +94,18 @@ impl Character for Droid {
fn id(&self) -> &str { self.id.as_str() } fn id(&self) -> &str { self.id.as_str() }
} }
juniper::graphql_union!(<'a> &'a Character: Database as "Character" where Scalar = <S> |&self| {
instance_resolvers: |&context| { #[juniper::graphql_union(
&Human => context.humans.get(self.id()), Context = Database
&Droid => context.droids.get(self.id()), )]
impl<'a> GraphQLUnion for &'a dyn Character {
fn resolve(&self, context: &Database) {
match self {
Human => context.humans.get(self.id()),
Droid => context.droids.get(self.id()),
}
} }
}); }
# fn main() {} # fn main() {}
``` ```
@ -132,12 +139,17 @@ struct Character {
id: String, id: String,
} }
juniper::graphql_union!(Character: Database where Scalar = <S> |&self| { #[juniper::graphql_union(
instance_resolvers: |&context| { Context = Database,
&Human => context.humans.get(&self.id), )]
&Droid => context.droids.get(&self.id), impl GraphQLUnion for Character {
fn resolve(&self, context: &Database) {
match self {
Human => { context.humans.get(&self.id) },
Droid => { context.droids.get(&self.id) },
}
} }
}); }
# fn main() {} # fn main() {}
``` ```
@ -163,12 +175,15 @@ enum Character {
Droid(Droid), Droid(Droid),
} }
juniper::graphql_union!(Character: () where Scalar = <S> |&self| { #[juniper::graphql_union]
instance_resolvers: |_| { impl Character {
&Human => match *self { Character::Human(ref h) => Some(h), _ => None }, fn resolve(&self) {
&Droid => match *self { Character::Droid(ref d) => Some(d), _ => None }, match self {
Human => { match *self { Character::Human(ref h) => Some(h), _ => None } },
Droid => { match *self { Character::Droid(ref d) => Some(d), _ => None } },
}
} }
}); }
# fn main() {} # fn main() {}
``` ```