@@ -6,19 +6,22 @@ mod schema;
66
77use actix_web:: { web, HttpResponse , Result } ;
88use app_env_vars:: AppEnvVars ;
9- use async_graphql:: { http:: GraphiQLSource , EmptyMutation , EmptySubscription , Schema } ;
9+ use async_graphql:: {
10+ http:: GraphiQLSource , EmptySubscription , Error , Result as GraphqlResult , Schema ,
11+ } ;
1012use async_graphql_actix_web:: { GraphQLRequest , GraphQLResponse } ;
1113use diesel_async:: {
1214 pooled_connection:: { deadpool:: Pool , AsyncDieselConnectionManager } ,
1315 AsyncPgConnection ,
1416} ;
1517use errors:: ApplicationError ;
16- use graphql_schema:: Query ;
18+ use graphql_schema:: { Mutation , Query } ;
1719use meilisearch_sdk:: Client as SearchClient ;
20+ use validator:: Validate ;
1821
19- pub type ApplicationSchema = Schema < Query , EmptyMutation , EmptySubscription > ;
22+ pub type ApplicationSchema = Schema < Query , Mutation , EmptySubscription > ;
2023
21- //Represents application data passed to graphql resolvers
24+ /// Represents application data passed to graphql resolvers
2225pub struct GraphQlData {
2326 pub pool : Pool < AsyncPgConnection > ,
2427 pub client : SearchClient ,
@@ -36,7 +39,7 @@ pub async fn index(schema: web::Data<ApplicationSchema>, req: GraphQLRequest) ->
3639 schema. execute ( req_inner) . await . into ( )
3740}
3841
39- //We build the graphql schema and any data required to be passed to all resolvers
42+ /// We build the graphql schema and any data required to be passed to all resolvers
4043pub fn build_schema ( app_env_vars : & AppEnvVars ) -> Result < ApplicationSchema , ApplicationError > {
4144 let client = SearchClient :: new (
4245 & app_env_vars. meilisearch_host ,
@@ -49,8 +52,19 @@ pub fn build_schema(app_env_vars: &AppEnvVars) -> Result<ApplicationSchema, Appl
4952 let schema_data = GraphQlData { pool, client } ;
5053
5154 Ok (
52- Schema :: build ( Query :: default ( ) , EmptyMutation , EmptySubscription )
55+ Schema :: build ( Query :: default ( ) , Mutation :: default ( ) , EmptySubscription )
5356 . data ( schema_data)
5457 . finish ( ) ,
5558 )
5659}
60+
61+ /// Helper function for returning an error if inputs do not match the set conditions
62+ pub fn validate_input < T : Validate > ( input : & T ) -> GraphqlResult < ( ) > {
63+ if let Err ( e) = input. validate ( ) {
64+ log:: error!( "Validation error: {}" , e) ;
65+ let err = serde_json:: to_string ( & e) . unwrap ( ) ;
66+ let err = Error :: from ( err) ;
67+ return Err ( err) ;
68+ }
69+ Ok ( ( ) )
70+ }
0 commit comments