From 46590ae8842082de8584e78681637b4e013f58d9 Mon Sep 17 00:00:00 2001 From: Christoph Herzog Date: Sat, 16 Nov 2019 02:21:28 +0100 Subject: [PATCH] book: Fix union and scalar code examples --- docs/book/content/types/interfaces.md | 4 +- docs/book/content/types/scalars.md | 5 ++- docs/book/content/types/unions.md | 59 +++++++++++++++++---------- 3 files changed, 42 insertions(+), 26 deletions(-) diff --git a/docs/book/content/types/interfaces.md b/docs/book/content/types/interfaces.md index b9d459d2..1d5e288e 100644 --- a/docs/book/content/types/interfaces.md +++ b/docs/book/content/types/interfaces.md @@ -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 = |&self| { +juniper::graphql_interface!(<'a> &'a dyn Character: () as "Character" where Scalar = |&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 = |&self| { +juniper::graphql_interface!(<'a> &'a dyn Character: Database as "Character" where Scalar = |&self| { field id() -> &str { self.id() } instance_resolvers: |&context| { diff --git a/docs/book/content/types/scalars.md b/docs/book/content/types/scalars.md index 7ab9a1e6..c630f66f 100644 --- a/docs/book/content/types/scalars.md +++ b/docs/book/content/types/scalars.md @@ -121,8 +121,9 @@ juniper::graphql_scalar!(Date where Scalar = { // Define how to parse a primitive type into your custom scalar. from_input_value(v: &InputValue) -> Option { - v.as_scalar_value::() - .and_then(|s| s.parse().ok()) + v.as_scalar_value() + .and_then(|v| v.as_str()) + .and_then(|s| s.parse().ok()) } // Define how to parse a string value. diff --git a/docs/book/content/types/unions.md b/docs/book/content/types/unions.md index f38a356c..19847423 100644 --- a/docs/book/content/types/unions.md +++ b/docs/book/content/types/unions.md @@ -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 = |&self| { - instance_resolvers: |_| { - // The left hand side indicates the concrete type T, the right hand - // side should be an expression returning Option - &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 = |&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 = |&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 = |&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() {} ```