Initial GraphQL API sort of working
This commit is contained in:
@@ -1,2 +1,73 @@
|
||||
pub mod models;
|
||||
pub mod schema;
|
||||
|
||||
use self::models::languages::Language;
|
||||
use diesel::pg::PgConnection;
|
||||
use diesel::r2d2::{ConnectionManager, Pool, PooledConnection};
|
||||
use dotenvy::dotenv;
|
||||
use std::env;
|
||||
use tracing::info;
|
||||
|
||||
use diesel::prelude::*;
|
||||
// use diesel::query_dsl::RunQueryDsl;
|
||||
|
||||
pub struct Database {
|
||||
conn: Pool<ConnectionManager<PgConnection>>,
|
||||
}
|
||||
|
||||
impl juniper::Context for Database {}
|
||||
|
||||
impl Database {
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
conn: Database::get_connection_pool(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn get_connection_pool() -> Pool<ConnectionManager<PgConnection>> {
|
||||
dotenv().ok();
|
||||
let database_url =
|
||||
env::var("DATABASE_URL").expect("DATABASE_URL must be set!");
|
||||
info!("Connecting to {}", database_url);
|
||||
let manager = ConnectionManager::<PgConnection>::new(database_url);
|
||||
Pool::builder()
|
||||
.test_on_check_out(true)
|
||||
.build(manager)
|
||||
.expect("Could not build connection pool")
|
||||
}
|
||||
|
||||
fn conn(
|
||||
&self,
|
||||
) -> Result<PooledConnection<ConnectionManager<PgConnection>>, ()> {
|
||||
self.conn
|
||||
.get()
|
||||
.map_err(|e| info!("Failed to connect to database: {:?}", e))
|
||||
}
|
||||
|
||||
pub fn all_languages(&self) -> Result<Vec<Language>, ()> {
|
||||
use self::schema::languages::dsl::languages;
|
||||
languages.load::<Language>(&mut self.conn()?).map_err(|e| {
|
||||
info!("Failed to retrieve languages from database: {:?}", e);
|
||||
})
|
||||
}
|
||||
|
||||
pub fn language(&self, name: &str) -> Option<Language> {
|
||||
use self::schema::languages::dsl::languages;
|
||||
match &mut self.conn() {
|
||||
Ok(val) => languages
|
||||
.find(name.to_string())
|
||||
.first::<Language>(val)
|
||||
.map_or_else(
|
||||
|e| {
|
||||
info!(
|
||||
"Failed to retrieve language {} from database: {:?}",
|
||||
name, e
|
||||
);
|
||||
None
|
||||
},
|
||||
Some,
|
||||
),
|
||||
Err(_) => None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,16 +1,18 @@
|
||||
use super::super::schema::{langandagents, languages};
|
||||
use diesel::prelude::*;
|
||||
use juniper::GraphQLEnum;
|
||||
|
||||
#[derive(diesel_derive_enum::DbEnum, Debug, Clone, PartialEq, Eq)]
|
||||
#[derive(diesel_derive_enum::DbEnum, Debug, Clone, PartialEq, Eq, GraphQLEnum)]
|
||||
#[DieselTypePath = "crate::db::schema::sql_types::Release"]
|
||||
pub enum Release {
|
||||
Public,
|
||||
#[graphql(name="NON_COMMERCIAL")]
|
||||
NonCommercial,
|
||||
Research,
|
||||
Private,
|
||||
}
|
||||
|
||||
#[derive(diesel_derive_enum::DbEnum, Debug, Clone, PartialEq, Eq)]
|
||||
#[derive(diesel_derive_enum::DbEnum, Debug, Clone, PartialEq, Eq, GraphQLEnum)]
|
||||
#[DieselTypePath = "crate::db::schema::sql_types::Dictgenre"]
|
||||
pub enum DictGenre {
|
||||
General,
|
||||
@@ -22,7 +24,7 @@ pub enum DictGenre {
|
||||
Terminology,
|
||||
}
|
||||
|
||||
#[derive(diesel_derive_enum::DbEnum, Debug, Clone, PartialEq, Eq)]
|
||||
#[derive(diesel_derive_enum::DbEnum, Debug, Clone, PartialEq, Eq, GraphQLEnum)]
|
||||
#[DieselTypePath = "crate::db::schema::sql_types::Agentlanguagerelation"]
|
||||
pub enum AgentLanguageRelation {
|
||||
Publisher,
|
||||
@@ -31,17 +33,35 @@ pub enum AgentLanguageRelation {
|
||||
|
||||
#[derive(Queryable, Insertable, Debug, Clone, PartialEq, Eq)]
|
||||
pub struct Language {
|
||||
release: Release,
|
||||
created: chrono::NaiveDateTime,
|
||||
name: String,
|
||||
owner: String,
|
||||
targetlanguage: Vec<String>,
|
||||
genre: Vec<DictGenre>,
|
||||
native: Option<String>,
|
||||
release: Release,
|
||||
targetlanguage: Vec<Option<String>>,
|
||||
genre: Vec<Option<DictGenre>>,
|
||||
abstract_: Option<String>,
|
||||
created: chrono::NaiveDateTime,
|
||||
description: Option<String>,
|
||||
rights: Option<String>,
|
||||
license: Option<String>,
|
||||
owner: String,
|
||||
}
|
||||
|
||||
#[juniper::graphql_object]
|
||||
impl Language {
|
||||
#[graphql(name = "release")]
|
||||
fn release(&self) -> Release {
|
||||
self.release.clone()
|
||||
}
|
||||
|
||||
#[graphql(name = "created")]
|
||||
fn created(&self) -> String {
|
||||
self.created.to_string()
|
||||
}
|
||||
|
||||
#[graphql(name = "name")]
|
||||
fn name(&self) -> String {
|
||||
self.name.clone()
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Queryable, Insertable, Debug, Clone, PartialEq, Eq)]
|
||||
|
||||
@@ -7,6 +7,17 @@ pub struct User {
|
||||
pub username: String,
|
||||
}
|
||||
|
||||
#[juniper::graphql_object]
|
||||
impl User {
|
||||
pub fn id(&self) -> &str {
|
||||
self.id.as_str()
|
||||
}
|
||||
|
||||
pub fn username(&self) -> &str {
|
||||
self.username.as_str()
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Queryable, Insertable, Debug, Clone, PartialEq, Eq)]
|
||||
#[diesel(table_name = userfollows)]
|
||||
pub struct UserFollow {
|
||||
|
||||
@@ -31,7 +31,7 @@ pub enum PartOfSpeech {
|
||||
}
|
||||
|
||||
#[derive(Queryable, Insertable, Debug, Clone, PartialEq, Eq)]
|
||||
struct Word {
|
||||
pub struct Word {
|
||||
norm: String,
|
||||
native: Option<String>,
|
||||
lemma: Option<String>,
|
||||
@@ -48,7 +48,7 @@ struct Word {
|
||||
|
||||
#[derive(Queryable, Insertable, Debug, Clone, PartialEq, Eq)]
|
||||
#[diesel(table_name = wordrelation)]
|
||||
struct WordRelation {
|
||||
pub struct WordRelation {
|
||||
id: i32,
|
||||
wordsource: String,
|
||||
wordtarget: String,
|
||||
|
||||
Reference in New Issue
Block a user