Fragment graphql module, add Appwrite vars to context

This commit is contained in:
2023-01-15 17:35:43 +01:00
parent 34e28384ce
commit b20fb5f079
11 changed files with 280 additions and 146 deletions

View File

@@ -29,6 +29,7 @@ macro_rules! find_element {
use diesel::prelude::*;
#[derive(Debug)]
pub struct Database {
conn: Pool<ConnectionManager<PgConnection>>,
}

View File

@@ -1,4 +1,4 @@
use crate::db::Database;
use crate::{db::Database, graphql::Context};
use diesel::prelude::*;
use juniper::GraphQLEnum;
use tracing::info;
@@ -63,11 +63,11 @@ pub struct Language {
impl Language {
fn relationship(
&self,
context: &Database,
db: &Database,
relationship: AgentLanguageRelation,
) -> Vec<User> {
use schema::langandagents::dsl;
match &mut context.conn() {
match &mut db.conn() {
Ok(conn) => dsl::langandagents
.filter(dsl::language.eq(self.id))
.filter(dsl::relationship.eq(relationship))
@@ -97,7 +97,7 @@ impl Language {
}
}
#[juniper::graphql_object(Context = Database)]
#[juniper::graphql_object(Context = Context)]
impl Language {
#[graphql(description = "Unique identifier of the language")]
fn id(&self) -> String {
@@ -125,9 +125,9 @@ impl Language {
name = "targetLanguage",
description = "Languages in which the current language is translated"
)]
fn target_language(&self, context: &Database) -> Vec<Language> {
fn target_language(&self, context: &Context) -> Vec<Language> {
use schema::langtranslatesto::dsl;
match &mut context.conn() {
match &mut context.db.conn() {
Ok(conn) => dsl::langtranslatesto
.filter(dsl::langfrom.eq(self.id))
.load::<LangTranslatesTo>(conn)
@@ -187,9 +187,9 @@ impl Language {
#[graphql(
description = "User with administrative rights over the language"
)]
fn owner(&self, context: &Database) -> User {
fn owner(&self, context: &Context) -> User {
use schema::users::dsl;
match &mut context.conn() {
match &mut context.db.conn() {
Ok(conn) => dsl::users
.find(self.owner.clone())
.first::<User>(conn)
@@ -206,15 +206,15 @@ impl Language {
#[graphql(
description = "People who participate in the elaboration of the language's dictionary"
)]
fn authors(&self, context: &Database) -> Vec<User> {
self.relationship(context, AgentLanguageRelation::Author)
fn authors(&self, context: &Context) -> Vec<User> {
self.relationship(&context.db, AgentLanguageRelation::Author)
}
#[graphql(
description = "People who can and do redistribute the language's dictionary"
)]
fn publishers(&self, context: &Database) -> Vec<User> {
self.relationship(context, AgentLanguageRelation::Publisher)
fn publishers(&self, context: &Context) -> Vec<User> {
self.relationship(&context.db, AgentLanguageRelation::Publisher)
}
}

View File

@@ -1,15 +1,15 @@
use super::super::schema::{userfollows, users};
use diesel::prelude::*;
use crate::db::Database;
use crate::graphql::Context;
#[derive(Queryable, Insertable, Debug, Clone, PartialEq, Eq)]
pub struct User {
pub id: String,
pub username: String,
id: String,
username: String,
}
#[juniper::graphql_object(Context = Database)]
#[juniper::graphql_object(Context = Context)]
impl User {
#[graphql(description = "Appwrite ID of the user")]
pub fn id(&self) -> String {
@@ -22,9 +22,9 @@ impl User {
}
#[graphql(description = "Who the user follows")]
pub fn following(&self, context: &Database) -> Vec<User> {
pub fn following(&self, context: &Context) -> Vec<User> {
use super::super::schema::{userfollows, users};
let conn = &mut context.conn().unwrap();
let conn = &mut context.db.conn().unwrap();
userfollows::dsl::userfollows
.filter(userfollows::dsl::follower.eq(self.id.clone()))
.load::<UserFollow>(conn)

View File

@@ -1,5 +1,5 @@
use super::super::schema;
use crate::db::Database;
use crate::{db::Database, graphql::Context};
use diesel::prelude::*;
use juniper::GraphQLEnum;
use schema::{wordrelation, words};
@@ -60,11 +60,11 @@ pub struct Word {
impl Word {
fn relationship(
&self,
context: &Database,
db: &Database,
relationship: WordRelationship,
) -> Vec<Word> {
use schema::wordrelation::dsl;
match &mut context.conn() {
match &mut db.conn() {
Ok(conn) => dsl::wordrelation
.filter(dsl::wordsource.eq(self.norm.clone()))
.filter(dsl::relationship.eq(relationship))
@@ -84,7 +84,7 @@ impl Word {
}
}
#[juniper::graphql_object(Context = Database)]
#[juniper::graphql_object(Context = Context)]
impl Word {
#[graphql(description = "Normal form of the word")]
fn norm(&self) -> String {
@@ -97,10 +97,10 @@ impl Word {
}
#[graphql(description = "Base form of the current word")]
fn lemma(&self, context: &Database) -> Option<Word> {
fn lemma(&self, context: &Context) -> Option<Word> {
use schema::words::dsl;
match self.lemma.clone() {
Some(lemma) => match &mut context.conn() {
Some(lemma) => match &mut context.db.conn() {
Ok(conn) => {
match dsl::words.find(lemma.clone()).first::<Word>(conn) {
Ok(word) => Some(word),
@@ -123,9 +123,9 @@ impl Word {
}
#[graphql(description = "Language to which the word belongs")]
fn language(&self, context: &Database) -> Language {
fn language(&self, context: &Context) -> Language {
use schema::languages::dsl;
match &mut context.conn() {
match &mut context.db.conn() {
Ok(conn) => {
match dsl::languages.find(self.language).first::<Language>(conn)
{
@@ -185,16 +185,16 @@ impl Word {
name = "related",
description = "Words related to the current word"
)]
fn related_words(&self, context: &Database) -> Vec<Word> {
self.relationship(context, WordRelationship::Related)
fn related_words(&self, context: &Context) -> Vec<Word> {
self.relationship(&context.db, WordRelationship::Related)
}
#[graphql(
name = "definitions",
description = "Words that define the current word"
)]
fn definitions(&self, context: &Database) -> Vec<Word> {
self.relationship(context, WordRelationship::Definition)
fn definitions(&self, context: &Context) -> Vec<Word> {
self.relationship(&context.db, WordRelationship::Definition)
}
}