filter.rs

Overview

This file defines filtering functionality for account-related queries within the system. It primarily provides the AccountFilter struct, which enables building dynamic filter expressions for account data retrieval, supporting complex conditional operations such as logical OR combinations. The filter structure is designed to integrate with GraphQL queries by leveraging async_graphql and serialization capabilities through serde.

The file also includes a test module to verify the correctness of the filter-to-query translation logic.

Detailed Descriptions

Struct: AccountFilter

#[derive(InputObject, Debug, Serialize, Default)]
#[graphql(rename_fields = "snake_case")]
pub struct AccountFilter {
    id: OptStringFilter,
    dapp_id: OptStringFilter,
    #[graphql(name = "OR")]
    or: Option<Box<AccountFilter>>,
}

Purpose

AccountFilter serves as an input object for GraphQL queries to specify filtering criteria for accounts. It allows filtering on the account's id and dapp_id fields and supports recursive logical OR conditions by nesting additional AccountFilter instances.

Fields

Traits Implemented

Usage Example

let filter = AccountFilter {
    id: Some(StringFilter { eq: Some("account123".to_string()), ..Default::default() }),
    dapp_id: None,
    or: None,
};

This filter would translate into a query condition where the account ID equals "account123".

Trait: WhereOp for AccountFilter

The WhereOp trait is implemented for AccountFilter, enabling it to convert the filter structure into a query-compatible operation. Although the trait methods are not explicitly defined in this file, the implementation allows invoking to_where() on an AccountFilter instance, which returns a string representation of the query condition.

Example (from test)

let af = AccountFilter {
    id: Some(StringFilter { eq: Some("-1:555...555".to_string()), ..Default::default() }),
    ..Default::default()
};

let query_condition = af.to_where().unwrap();
assert_eq!(query_condition, format!("WHERE id = {:?}", "-1:555...555"));

Test Module: tests

The tests module contains unit tests validating the behavior of AccountFilter and its integration with the WhereOp trait.

Test: test_account_filter

This test ensures correctness of filter serialization into query language.

Important Implementation Details

Interactions with Other System Components

Visual Diagram

classDiagram
class AccountFilter {
- id: OptStringFilter
- dapp_id: OptStringFilter
- or: Option<Box<AccountFilter>>
+ to_where()
}
AccountFilter ..> OptStringFilter : uses
AccountFilter ..> WhereOp : implements

This diagram depicts AccountFilter with its fields and method, as well as its relationship to OptStringFilter and the WhereOp trait.