stoptime-rs/src/graphql.rs

93 lines
2.8 KiB
Rust

//! The GraphQL schema implementation.
//!
//! Provides the schema with the root query and mutation.
use crate::models::{
CompanyInfo, Customer, Invoice, NewCustomer, NewInvoice, NewTimeEntry, TimeEntry,
};
use crate::DbConn;
use diesel::prelude::*;
use juniper::{object, Context, FieldResult, RootNode};
impl Context for DbConn {}
/// The GraphQL schema.
pub type Schema = RootNode<'static, Query, Mutation>;
/// The GraphQL query root.
pub struct Query;
#[object(Context = DbConn)]
impl Query {
/// Returns the current API version.
fn api_version() -> &'static str {
"1.0"
}
/// Returns the company info with the given ID.
fn company_info(context: &DbConn, id: i32) -> FieldResult<CompanyInfo> {
retrieve!(CompanyInfo, id, **context).map_err(Into::into)
}
/// Returns all known customers.
fn customers(context: &DbConn) -> FieldResult<Vec<Customer>> {
all!(Customer, **context).map_err(Into::into)
}
/// Returns the customer with the given ID.
fn customer(context: &DbConn, id: i32) -> FieldResult<Customer> {
retrieve!(Customer, id, **context).map_err(Into::into)
}
/// Returns all known customers.
fn customers(context: &DbConn) -> FieldResult<Vec<Customer>> {
all!(Customer, **context).map_err(Into::into)
}
/// Returns the customer with the given ID.
fn invoice(context: &DbConn, id: i32) -> FieldResult<Invoice> {
retrieve!(Invoice, id, **context).map_err(Into::into)
}
/// Returns all known invoices.
fn invoices(context: &DbConn) -> FieldResult<Vec<Invoice>> {
all!(Invoice, **context).map_err(Into::into)
}
/// Returns the time entry with the given ID.
fn time_entry(context: &DbConn, id: i32) -> FieldResult<TimeEntry> {
retrieve!(TimeEntry, id, **context).map_err(Into::into)
}
/// Returns all known invoices.
fn time_entries(context: &DbConn) -> FieldResult<Vec<TimeEntry>> {
all!(TimeEntry, **context).map_err(Into::into)
}
}
/// The GraphQL mutation root.
pub struct Mutation;
#[object(Context = DbConn)]
impl Mutation {
/// Returns the current API version.
fn api_version() -> &'static str {
"1.0"
}
/// Creates a new customer.
fn create_customer(context: &DbConn, new_customer: NewCustomer) -> FieldResult<Customer> {
create!(Customer, new_customer, **context).map_err(Into::into)
}
/// Creates a new invoice.
fn create_invoice(context: &DbConn, new_invoice: NewInvoice) -> FieldResult<Invoice> {
create!(Invoice, new_invoice, **context).map_err(Into::into)
}
/// Creates a new time entry.
fn create_time_entry(context: &DbConn, new_time_entry: NewTimeEntry) -> FieldResult<TimeEntry> {
create!(TimeEntry, new_time_entry, **context).map_err(Into::into)
}
}