use juniper::{graphql_value, FieldError, FieldResult, IntoFieldError, ScalarValue}; pub enum Error { Internal, DoesNotExist, CountNegative, } impl IntoFieldError for Error { fn into_field_error(self) -> FieldError { match self { Self::Internal => FieldError::new( "Internal server error", graphql_value!({ "type": "INTERNAL" }), ), Self::DoesNotExist => FieldError::new( "Record does not exist", graphql_value!({ "type": "DOES_NOT_EXIST" }), ), Self::CountNegative => FieldError::new( "Count can not be negative", graphql_value!({ "type": "COUNT_NEGATIVE", }), ), } } } pub trait QueryResultIntoFieldResult { fn into_field_result(self) -> FieldResult; } impl QueryResultIntoFieldResult for diesel::QueryResult { fn into_field_result(self) -> FieldResult { self.map_err(|_| Error::Internal.into_field_error()) } } // pub trait AsyncResultIntoFieldResult { // fn into_field_result(self) -> FieldResult; // } // impl AsyncResultIntoFieldResult // for Result // { // fn into_field_result(self) -> FieldResult { // // match self { // // Ok(x) => Ok(x), // // Err(_) => Err(Error::Internal.into_field_error()), // // } // self.map_err(|_| Error::Internal.into_field_error()) // } // }