Skip to main content

DirectiveHandler

Trait DirectiveHandler 

Source
pub trait DirectiveHandler: Send + Sync {
    // Required methods
    fn name(&self) -> &str;
    fn evaluate(
        &self,
        args: &HashMap<String, JsonValue>,
        context: &EvaluationContext,
    ) -> Result<DirectiveResult, DirectiveError>;

    // Provided method
    fn validate_args(
        &self,
        _args: &HashMap<String, JsonValue>,
    ) -> Result<(), DirectiveError> { ... }
}
Expand description

Trait for custom directive handlers.

Implement this trait to create custom directives that can be registered with the DirectiveEvaluatorBuilder.

§Thread Safety

Handlers must be Send + Sync to be used across async contexts.

§Example

use fraiseql_core::graphql::{DirectiveHandler, DirectiveResult, EvaluationContext, DirectiveError};
use std::collections::HashMap;
use serde_json::Value as JsonValue;

struct UppercaseDirective;

impl DirectiveHandler for UppercaseDirective {
    fn name(&self) -> &str {
        "uppercase"
    }

    fn evaluate(
        &self,
        _args: &HashMap<String, JsonValue>,
        _context: &EvaluationContext,
    ) -> Result<DirectiveResult, DirectiveError> {
        // This directive would transform string values to uppercase
        // (actual transformation happens during field resolution)
        Ok(DirectiveResult::Include)
    }
}

Required Methods§

Source

fn name(&self) -> &str

Returns the directive name (without the @ prefix).

Source

fn evaluate( &self, args: &HashMap<String, JsonValue>, context: &EvaluationContext, ) -> Result<DirectiveResult, DirectiveError>

Evaluate the directive with the given arguments and context.

§Arguments
  • args - Parsed directive arguments as a map of name to value
  • context - Evaluation context with variables and user info
§Returns

A DirectiveResult indicating how to handle the field, or an error.

§Errors

Returns DirectiveError if the directive arguments are invalid or evaluation fails.

Provided Methods§

Source

fn validate_args( &self, _args: &HashMap<String, JsonValue>, ) -> Result<(), DirectiveError>

Optional: Validate directive arguments at schema load time.

Called when a schema with this directive is loaded to ensure arguments are valid.

Default implementation accepts all arguments.

§Errors

Returns DirectiveError if the arguments are invalid for this directive.

Dyn Compatibility§

This trait is dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§