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) }
}
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() }
instance_resolvers: |_| {
@ -111,7 +111,7 @@ impl Character for Droid {
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() }
instance_resolvers: |&context| {

View file

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

View file

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