Add basic mutation root
This commit is contained in:
parent
3767438d67
commit
88f8ef867c
3 changed files with 80 additions and 6 deletions
|
|
@ -15,6 +15,6 @@ CREATE TABLE records (
|
|||
id SERIAL PRIMARY KEY,
|
||||
dataset_id INTEGER NOT NULL REFERENCES datasets(id),
|
||||
location_id INTEGER REFERENCES locations(id),
|
||||
timestamp TIMESTAMPTZ NOT NULL,
|
||||
timestamp TIMESTAMPTZ NOT NULL DEFAULT now(),
|
||||
data JSONB
|
||||
);
|
||||
|
|
|
|||
|
|
@ -18,7 +18,7 @@ impl QueryRoot {
|
|||
.fetch_all(pool)
|
||||
.await?;
|
||||
|
||||
return Ok(rows.into_iter().map(|r| Dataset::from(r)).collect());
|
||||
Ok(rows.into_iter().map(Dataset::from).collect())
|
||||
}
|
||||
|
||||
pub async fn query_dataset(
|
||||
|
|
@ -56,3 +56,77 @@ impl QueryRoot {
|
|||
Ok(row.into())
|
||||
}
|
||||
}
|
||||
|
||||
pub struct MutationRoot;
|
||||
|
||||
#[Object]
|
||||
impl MutationRoot {
|
||||
pub async fn create_dataset(
|
||||
&self,
|
||||
ctx: &Context<'_>,
|
||||
name: String,
|
||||
description: Option<String>,
|
||||
) -> anyhow::Result<i32> {
|
||||
let pool = get_pg_pool(ctx);
|
||||
|
||||
let id: i32 = sqlx::query_scalar(
|
||||
r#"INSERT INTO datasets (name, description)
|
||||
VALUES ($1, $2)
|
||||
RETURNING id;"#,
|
||||
)
|
||||
.bind(name)
|
||||
.bind(description)
|
||||
.fetch_one(pool)
|
||||
.await?;
|
||||
|
||||
Ok(id)
|
||||
}
|
||||
|
||||
pub async fn create_location(
|
||||
&self,
|
||||
ctx: &Context<'_>,
|
||||
name: String,
|
||||
lat: f64,
|
||||
lon: f64,
|
||||
) -> anyhow::Result<i32> {
|
||||
let pool = get_pg_pool(ctx);
|
||||
|
||||
let id: i32 = sqlx::query_scalar(
|
||||
r#"INSERT INTO locations (name, lat, lon)
|
||||
VALUES ($1, $2)
|
||||
RETURNING id;"#,
|
||||
)
|
||||
.bind(name)
|
||||
.bind(lat)
|
||||
.bind(lon)
|
||||
.fetch_one(pool)
|
||||
.await?;
|
||||
|
||||
Ok(id)
|
||||
}
|
||||
|
||||
pub async fn record_into_dataset(
|
||||
&self,
|
||||
ctx: &Context<'_>,
|
||||
dataset_id: i32,
|
||||
location_id: Option<i32>,
|
||||
data: serde_json::Value,
|
||||
) -> anyhow::Result<Record> {
|
||||
let pool = get_pg_pool(ctx);
|
||||
|
||||
let row: RecordRow = sqlx::query_as(
|
||||
r#"
|
||||
INSERT INTO records (dataset_id, location_id, data)
|
||||
VALUES ($1, $2, $3)
|
||||
RETURNING *;
|
||||
"#,
|
||||
)
|
||||
.bind(dataset_id)
|
||||
.bind(location_id)
|
||||
.bind(data)
|
||||
.fetch_one(pool)
|
||||
.await?;
|
||||
|
||||
Ok(Record::from(row))
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,14 +1,14 @@
|
|||
use crate::resolvers::QueryRoot;
|
||||
use async_graphql::{EmptyMutation, EmptySubscription, Schema};
|
||||
use crate::resolvers::{MutationRoot, QueryRoot};
|
||||
use async_graphql::{EmptySubscription, Schema};
|
||||
use sqlx::{PgPool, postgres::PgPoolOptions};
|
||||
use std::{env, time::Duration};
|
||||
use tokio::time::timeout;
|
||||
|
||||
pub type AppSchema = Schema<QueryRoot, EmptyMutation, EmptySubscription>;
|
||||
pub type AppSchema = Schema<QueryRoot, MutationRoot, EmptySubscription>;
|
||||
|
||||
pub async fn build_schema() -> anyhow::Result<AppSchema> {
|
||||
let pool = create_pool().await?;
|
||||
Ok(Schema::build(QueryRoot, EmptyMutation, EmptySubscription)
|
||||
Ok(Schema::build(QueryRoot, MutationRoot, EmptySubscription)
|
||||
.data(pool)
|
||||
.finish())
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue