mirror of
https://github.com/actix/examples
synced 2024-11-28 08:22:39 +01:00
59 lines
1.3 KiB
Rust
59 lines
1.3 KiB
Rust
use juniper::FieldResult;
|
|
use juniper::RootNode;
|
|
|
|
#[derive(GraphQLEnum)]
|
|
enum Episode {
|
|
NewHope,
|
|
Empire,
|
|
Jedi,
|
|
}
|
|
|
|
#[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;
|
|
|
|
graphql_object!(QueryRoot: () |&self| {
|
|
field human(&executor, id: String) -> FieldResult<Human> {
|
|
Ok(Human{
|
|
id: "1234".to_owned(),
|
|
name: "Luke".to_owned(),
|
|
appears_in: vec![Episode::NewHope],
|
|
home_planet: "Mars".to_owned(),
|
|
})
|
|
}
|
|
});
|
|
|
|
pub struct MutationRoot;
|
|
|
|
graphql_object!(MutationRoot: () |&self| {
|
|
field createHuman(&executor, new_human: NewHuman) -> FieldResult<Human> {
|
|
Ok(Human{
|
|
id: "1234".to_owned(),
|
|
name: new_human.name,
|
|
appears_in: new_human.appears_in,
|
|
home_planet: new_human.home_planet,
|
|
})
|
|
}
|
|
});
|
|
|
|
pub type Schema = RootNode<'static, QueryRoot, MutationRoot>;
|
|
|
|
pub fn create_schema() -> Schema {
|
|
Schema::new(QueryRoot {}, MutationRoot {})
|
|
}
|