1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
use executor::Context;
use tests::model::{Character, Database, Droid, Episode, Human};

impl Context for Database {}

graphql_interface!(<'a> &'a Character: Database as "Character" |&self| {
    description: "A character in the Star Wars Trilogy"

    field id() -> &str as "The id of the character" {
        self.id()
    }

    field name() -> Option<&str> as "The name of the character" {
        Some(self.name())
    }

    field friends(&executor) -> Vec<&Character>
    as "The friends of the character" {
        executor.context().get_friends(self.as_character())
    }

    field appears_in() -> &[Episode] as "Which movies they appear in" {
        self.appears_in()
    }

    instance_resolvers: |&context| {
        &Human => context.get_human(&self.id()),
        &Droid => context.get_droid(&self.id()),
    }
});

graphql_object!(<'a> &'a Human: Database as "Human" |&self| {
    description: "A humanoid creature in the Star Wars universe."

    interfaces: [&Character]

    field id() -> &str as "The id of the human"{
        self.id()
    }

    field name() -> Option<&str> as "The name of the human" {
        Some(self.name())
    }

    field friends(&executor) -> Vec<&Character>
    as "The friends of the human" {
        executor.context().get_friends(self.as_character())
    }

    field appears_in() -> &[Episode] as "Which movies they appear in" {
        self.appears_in()
    }

    field home_planet() -> &Option<String> as "The home planet of the human" {
        self.home_planet()
    }
});

graphql_object!(<'a> &'a Droid: Database as "Droid" |&self| {
    description: "A mechanical creature in the Star Wars universe."

    interfaces: [&Character]

    field id() -> &str as "The id of the droid" {
        self.id()
    }

    field name() -> Option<&str> as "The name of the droid" {
        Some(self.name())
    }

    field friends(&executor) -> Vec<&Character>
    as "The friends of the droid" {
        executor.context().get_friends(self.as_character())
    }

    field appears_in() -> &[Episode] as "Which movies they appear in" {
        self.appears_in()
    }

    field primary_function() -> &Option<String> as "The primary function of the droid" {
        self.primary_function()
    }
});

graphql_object!(Database: Database as "Query" |&self| {
    description: "The root query object of the schema"

    field human(
        id: String as "id of the human"
    ) -> Option<&Human> {
        self.get_human(&id)
    }

    field droid(
        id: String as "id of the droid"
    ) -> Option<&Droid> {
        self.get_droid(&id)
    }

    field hero(
        episode: Option<Episode> as
        "If omitted, returns the hero of the whole saga. If provided, returns \
        the hero of that particular episode"
    ) -> Option<&Character> {
        Some(self.get_hero(episode).as_character())
    }
});