Fragment graphql module, add Appwrite vars to context
This commit is contained in:
54
src/graphql/mod.rs
Normal file
54
src/graphql/mod.rs
Normal file
@@ -0,0 +1,54 @@
|
||||
use rocket::response::content::RawHtml;
|
||||
use rocket::State;
|
||||
|
||||
use juniper::EmptySubscription;
|
||||
use juniper_rocket::{GraphQLRequest, GraphQLResponse};
|
||||
|
||||
use crate::appwrite::APVariables;
|
||||
use crate::db::Database;
|
||||
|
||||
#[derive(Default, Debug)]
|
||||
pub struct Context {
|
||||
pub db: Database,
|
||||
pub appwrite: APVariables,
|
||||
}
|
||||
|
||||
impl juniper::Context for Context {}
|
||||
|
||||
mod query;
|
||||
use query::Query;
|
||||
|
||||
mod mutation;
|
||||
use mutation::Mutation;
|
||||
|
||||
type Schema =
|
||||
juniper::RootNode<'static, Query, Mutation, EmptySubscription<Context>>;
|
||||
|
||||
pub fn create_schema() -> Schema {
|
||||
Schema::new(Query {}, Mutation {}, EmptySubscription::default())
|
||||
}
|
||||
|
||||
#[rocket::get("/")]
|
||||
pub fn graphiql() -> RawHtml<String> {
|
||||
let graphql_endpoint_url = "/graphql";
|
||||
juniper_rocket::graphiql_source(graphql_endpoint_url, None)
|
||||
}
|
||||
|
||||
#[rocket::get("/graphql?<request>")]
|
||||
pub async fn get_graphql_handler(
|
||||
context: &State<Context>,
|
||||
request: GraphQLRequest,
|
||||
schema: &State<Schema>,
|
||||
) -> GraphQLResponse {
|
||||
request.execute(schema, context).await
|
||||
}
|
||||
|
||||
#[allow(clippy::needless_pass_by_value)]
|
||||
#[rocket::post("/graphql", data = "<request>")]
|
||||
pub async fn post_graphql_handler(
|
||||
context: &State<Context>,
|
||||
request: GraphQLRequest,
|
||||
schema: &State<Schema>,
|
||||
) -> GraphQLResponse {
|
||||
request.execute(schema, context).await
|
||||
}
|
||||
32
src/graphql/mutation.rs
Normal file
32
src/graphql/mutation.rs
Normal file
@@ -0,0 +1,32 @@
|
||||
use super::Context;
|
||||
|
||||
pub struct Mutation;
|
||||
|
||||
#[juniper::graphql_object(Context = Context)]
|
||||
impl Mutation {
|
||||
fn api_version(
|
||||
session_id: Option<String>,
|
||||
user_id: Option<String>,
|
||||
context: &Context,
|
||||
) -> String {
|
||||
"0.1".into()
|
||||
// if session_id.is_some() && user_id.is_some() {
|
||||
// match context
|
||||
// .appwrite
|
||||
// .check_session(session_id.unwrap(), user_id.unwrap())
|
||||
// {
|
||||
// Ok(true) => "0.1 (authentified)".into(),
|
||||
// Ok(false) => "0.1 (not authentified)".into(),
|
||||
// Err(e) => {
|
||||
// info!(
|
||||
// "Error while checking if the user is connected: {:?}",
|
||||
// e
|
||||
// );
|
||||
// "0.1 (auth failed)"
|
||||
// }
|
||||
// }
|
||||
// } else {
|
||||
// "0.1 (not authentified)"
|
||||
// }
|
||||
}
|
||||
}
|
||||
65
src/graphql/query.rs
Normal file
65
src/graphql/query.rs
Normal file
@@ -0,0 +1,65 @@
|
||||
use super::Context;
|
||||
use crate::db::models::{languages::Language, users::User, words::Word};
|
||||
|
||||
use std::str::FromStr;
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct Query;
|
||||
|
||||
#[juniper::graphql_object(Context = Context)]
|
||||
impl Query {
|
||||
#[graphql(
|
||||
name = "allLanguages",
|
||||
description = "Retrieve all languages defined in the database"
|
||||
)]
|
||||
fn all_languages(context: &Context) -> Vec<Language> {
|
||||
context.db.all_languages().unwrap()
|
||||
}
|
||||
|
||||
#[graphql(
|
||||
description = "Retrieve a specific language from its name and its owner's id",
|
||||
arguments(
|
||||
name(description = "Name of the language"),
|
||||
owner(description = "ID of the owner of the language")
|
||||
)
|
||||
)]
|
||||
fn language(
|
||||
context: &Context,
|
||||
name: String,
|
||||
owner: String,
|
||||
) -> Option<Language> {
|
||||
context.db.language(name.as_str(), owner.as_str())
|
||||
}
|
||||
|
||||
#[graphql(
|
||||
description = "Retrieve a specific user from its id",
|
||||
arguments(id(description = "Appwrite ID of a user"))
|
||||
)]
|
||||
fn user(context: &Context, id: String) -> Option<User> {
|
||||
context.db.user(id.as_str())
|
||||
}
|
||||
|
||||
#[graphql(
|
||||
description = "Retrieve a specific word from its id",
|
||||
arguments(id(description = "Unique identifier of a word"))
|
||||
)]
|
||||
fn word(context: &Context, id: String) -> Option<Word> {
|
||||
context.db.word_id(id.as_str())
|
||||
}
|
||||
|
||||
#[graphql(
|
||||
description = "Retrieve all words with a set normal form from a set language",
|
||||
arguments(
|
||||
owner(
|
||||
description = "ID of the owner of the language to search a word in"
|
||||
),
|
||||
language(description = "Name of the language to search a word in"),
|
||||
word(description = "Word to search")
|
||||
)
|
||||
)]
|
||||
fn words(context: &Context, language: String, word: String) -> Vec<Word> {
|
||||
context
|
||||
.db
|
||||
.words(uuid::Uuid::from_str(&language).unwrap(), word.as_str())
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user