Skip to main content

oxidite_graphql/
schema.rs

1use juniper::{RootNode, GraphQLObject, GraphQLInputObject, FieldResult};
2use crate::context::Context;
3
4// Define basic query root
5pub struct QueryRoot;
6
7#[juniper::graphql_object(Context = Context)]
8impl QueryRoot {
9    async fn api_version() -> &str {
10        "1.0"
11    }
12
13    async fn health_check() -> bool {
14        true
15    }
16}
17
18// Define basic mutation root
19pub struct MutationRoot;
20
21#[juniper::graphql_object(Context = Context)]
22impl MutationRoot {
23    async fn add_todo(text: String) -> FieldResult<String> {
24        Ok(format!("Added todo: {}", text))
25    }
26}
27
28// Create the schema
29pub fn create_schema() -> RootNode<'static, QueryRoot, MutationRoot, juniper::EmptySubscription<Context>> {
30    RootNode::new(
31        QueryRoot,
32        MutationRoot,
33        juniper::EmptySubscription::new(),
34    )
35}
36
37// Export the schema type
38pub type GraphQLSchema = RootNode<'static, QueryRoot, MutationRoot, juniper::EmptySubscription<Context>>;
39
40// Example of how to define a custom object
41#[derive(GraphQLObject)]
42pub struct Todo {
43    pub id: i32,
44    pub text: String,
45    pub completed: bool,
46}
47
48// Example of how to define an input object
49#[derive(GraphQLInputObject)]
50pub struct NewTodo {
51    pub text: String,
52    pub completed: Option<bool>,
53}