2018-04-13 09:18:42 +08:00
|
|
|
use juniper::FieldResult;
|
|
|
|
use juniper::RootNode;
|
|
|
|
|
|
|
|
#[derive(GraphQLEnum)]
|
|
|
|
enum Episode {
|
|
|
|
NewHope,
|
|
|
|
Empire,
|
|
|
|
Jedi,
|
|
|
|
}
|
|
|
|
|
2020-01-12 17:14:08 +01:00
|
|
|
use juniper::{GraphQLEnum, GraphQLInputObject, GraphQLObject};
|
|
|
|
|
2018-04-13 09:18:42 +08:00
|
|
|
#[derive(GraphQLObject)]
|
|
|
|
#[graphql(description = "A humanoid creature in the Star Wars universe")]
|
|
|
|
struct Human {
|
|
|
|
id: String,
|
|
|
|
name: String,
|
|
|
|
appears_in: Vec<Episode>,
|
|
|
|
home_planet: String,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(GraphQLInputObject)]
|
|
|
|
#[graphql(description = "A humanoid creature in the Star Wars universe")]
|
|
|
|
struct NewHuman {
|
|
|
|
name: String,
|
|
|
|
appears_in: Vec<Episode>,
|
|
|
|
home_planet: String,
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct QueryRoot;
|
|
|
|
|
2020-01-12 17:14:08 +01:00
|
|
|
#[juniper::object]
|
|
|
|
impl QueryRoot {
|
|
|
|
fn human(id: String) -> FieldResult<Human> {
|
|
|
|
Ok(Human {
|
2018-04-13 09:18:42 +08:00
|
|
|
id: "1234".to_owned(),
|
|
|
|
name: "Luke".to_owned(),
|
|
|
|
appears_in: vec![Episode::NewHope],
|
|
|
|
home_planet: "Mars".to_owned(),
|
|
|
|
})
|
|
|
|
}
|
2020-01-12 17:14:08 +01:00
|
|
|
}
|
2018-04-13 09:18:42 +08:00
|
|
|
|
|
|
|
pub struct MutationRoot;
|
|
|
|
|
2020-01-12 17:14:08 +01:00
|
|
|
#[juniper::object]
|
|
|
|
impl MutationRoot {
|
2020-06-18 07:33:01 +09:00
|
|
|
fn create_human(new_human: NewHuman) -> FieldResult<Human> {
|
2020-01-12 17:14:08 +01:00
|
|
|
Ok(Human {
|
2018-04-13 09:18:42 +08:00
|
|
|
id: "1234".to_owned(),
|
|
|
|
name: new_human.name,
|
|
|
|
appears_in: new_human.appears_in,
|
|
|
|
home_planet: new_human.home_planet,
|
|
|
|
})
|
|
|
|
}
|
2020-01-12 17:14:08 +01:00
|
|
|
}
|
2018-04-13 09:18:42 +08:00
|
|
|
|
|
|
|
pub type Schema = RootNode<'static, QueryRoot, MutationRoot>;
|
|
|
|
|
|
|
|
pub fn create_schema() -> Schema {
|
|
|
|
Schema::new(QueryRoot {}, MutationRoot {})
|
|
|
|
}
|