Skip to main content

StartHookPayload

Trait StartHookPayload 

Source
pub trait StartHookPayload<TEndPayload: EndHookPayload<TResponse>, TResponse>
where Self: Sized, TResponse: FromGraphQLErrorToResponse,
{ // Provided methods fn proceed<'exec>( self, ) -> StartHookResult<'exec, Self, TEndPayload, TResponse> { ... } fn end_with_response<'exec>( self, output: TResponse, ) -> StartHookResult<'exec, Self, TEndPayload, TResponse> { ... } fn end_with_graphql_error<'exec>( self, error: GraphQLError, status_code: StatusCode, ) -> StartHookResult<'exec, Self, TEndPayload, TResponse> where TResponse: FromGraphQLErrorToResponse { ... } fn on_end<'exec, F>( self, f: F, ) -> StartHookResult<'exec, Self, TEndPayload, TResponse> where F: FnOnce(TEndPayload) -> EndHookResult<TEndPayload, TResponse> + Send + 'exec { ... } }

Provided Methods§

Source

fn proceed<'exec>(self) -> StartHookResult<'exec, Self, TEndPayload, TResponse>

Continue with the regular flow of the hook This is called in most cases when you don’t short-circuit the hook with a response or an error.

Example:

async fn on_graphql_params<'exec>(
    &'exec self,
   payload: OnGraphQLParamsStartHookPayload<'exec>,
) -> OnGraphQLParamsStartHookResult<'exec> {
   // manipulate payload if needed...
   payload.proceed()
}
Source

fn end_with_response<'exec>( self, output: TResponse, ) -> StartHookResult<'exec, Self, TEndPayload, TResponse>

End the hook execution and return a response to the client immediately, skipping the rest of the execution flow.

Source

fn end_with_graphql_error<'exec>( self, error: GraphQLError, status_code: StatusCode, ) -> StartHookResult<'exec, Self, TEndPayload, TResponse>
where TResponse: FromGraphQLErrorToResponse,

End the hook execution with a GraphQL error, returning a response with the appropriate error format to the client immediately, skipping the rest of the execution flow.

Example:

fn on_http_request<'req>(
    &'req self,
    payload: OnHttpRequestHookPayload<'req>,
) -> OnHttpRequestHookResult<'req> {
    if payload.router_http_request.headers().get("authorization").is_none() {
        return payload.end_with_graphql_error(
            GraphQLError::from_message_and_code("Unauthorized", "UNAUTHORIZED"),
            StatusCode::UNAUTHORIZED,
        );
    }
  
    payload.proceed()
}
Source

fn on_end<'exec, F>( self, f: F, ) -> StartHookResult<'exec, Self, TEndPayload, TResponse>
where F: FnOnce(TEndPayload) -> EndHookResult<TEndPayload, TResponse> + Send + 'exec,

Attach a callback to be executed at the end of the hook, allowing you to manipulate the end payload or response. This is useful when you want to execute some logic after the main execution of the hook

Example:

fn on_http_request<'req>(
    &'req self,
    payload: OnHttpRequestHookPayload<'req>,
) -> OnHttpRequestHookResult<'req> {
    payload.on_end(|payload| {
        payload.map_response(|mut response| {
            response.response_mut().headers_mut().insert(
                "x-served-by",
                "hive-router".parse().unwrap(),
            );
            response
        }).proceed()
    })
}

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§