Added table query.
This commit is contained in:
		
							
								
								
									
										26
									
								
								src/lib.rs
									
									
									
									
									
								
							
							
						
						
									
										26
									
								
								src/lib.rs
									
									
									
									
									
								
							@@ -24,6 +24,17 @@ struct Query;
 | 
			
		||||
 | 
			
		||||
#[Object]
 | 
			
		||||
impl Query {
 | 
			
		||||
    async fn table(&self, ctx: &Context<'_>, name: String) -> Result<Option<Table>> {
 | 
			
		||||
        let tbls = ctx
 | 
			
		||||
            .data::<RwLock<Vec<Table>>>()
 | 
			
		||||
            .unwrap()
 | 
			
		||||
            .read()
 | 
			
		||||
            .await
 | 
			
		||||
            .to_vec();
 | 
			
		||||
        let idx = tbls.binary_search_by(|t| t.name.cmp(&name)).unwrap();
 | 
			
		||||
        Ok(Some(tbls[idx].clone()))
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    async fn tables(&self, ctx: &Context<'_>) -> Vec<Table> {
 | 
			
		||||
        ctx.data::<RwLock<Vec<Table>>>()
 | 
			
		||||
            .unwrap()
 | 
			
		||||
@@ -41,6 +52,7 @@ impl Mutation {
 | 
			
		||||
        let mut tables = ctx.data::<RwLock<Vec<Table>>>().unwrap().write().await;
 | 
			
		||||
        let output = Table::new(name).await;
 | 
			
		||||
        tables.push(output.clone());
 | 
			
		||||
        tables.sort_by_key(|k| k.name.clone());
 | 
			
		||||
        Ok(Some(output))
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@@ -85,6 +97,18 @@ mod support {
 | 
			
		||||
mod queries {
 | 
			
		||||
    use super::*;
 | 
			
		||||
 | 
			
		||||
    #[async_std::test]
 | 
			
		||||
    async fn list_table() {
 | 
			
		||||
        let db = Database::new();
 | 
			
		||||
        db.execute(r#"mutation {createTable(name: "wilma"){name}}"#)
 | 
			
		||||
            .await;
 | 
			
		||||
        db.execute(r#"mutation {createTable(name: "betty"){name}}"#)
 | 
			
		||||
            .await;
 | 
			
		||||
        let output = db.execute(r#"{table(name: "wilma"){name}}"#).await;
 | 
			
		||||
        let expected = r#"{"data":{"table":{"name":"wilma"}}}"#;
 | 
			
		||||
        support::compare(&db, &output, &expected);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    #[async_std::test]
 | 
			
		||||
    async fn list_tables() {
 | 
			
		||||
        let db = Database::new();
 | 
			
		||||
@@ -93,7 +117,7 @@ mod queries {
 | 
			
		||||
        db.execute(r#"mutation {createTable(name: "barney"){name}}"#)
 | 
			
		||||
            .await;
 | 
			
		||||
        let output = db.execute(r#"{tables{name}}"#).await;
 | 
			
		||||
        let expected = r#"{"data":{"tables":[{"name":"fred"},{"name":"barney"}]}}"#;
 | 
			
		||||
        let expected = r#"{"data":{"tables":[{"name":"barney"},{"name":"fred"}]}}"#;
 | 
			
		||||
        support::compare(&db, &output, &expected);
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user