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
id: OptStringFilter
Optional string filter for the account ID. Supports operations like equality and pattern matching as defined inOptStringFilter.dapp_id: OptStringFilter
Optional string filter for the decentralized application (dapp) ID associated with the account.or: Option<Box<AccountFilter>>
Optional logical OR filter that allows combining multipleAccountFilterconditions. This field is renamed to"OR"in the GraphQL schema to align with common query syntax.
Traits Implemented
InputObject(fromasync_graphql): EnablesAccountFilterto be used as a GraphQL input type.Serialize(fromserde): Allows serialization for data interchange.Default: Provides default values for all fields.Debug: Enables debug formatting.WhereOp(custom trait): Implements the translation of this filter into a SQL WHERE clause or similar query operation.
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
Creates an
AccountFilterinstance with a specificidfilter.Invokes
to_where()to generate the query condition string.Asserts that the generated query string matches the expected SQL WHERE clause format.
This test ensures correctness of filter serialization into query language.
Important Implementation Details
The filter leverages
OptStringFilterfrom the internalfiltermodule to support optional string operations such as equality, which likely encapsulates various string comparison operators.The recursive
orfield allows nesting multiple account filters, enabling complex query logic involving disjunctions.The
#[graphql(rename_fields = "snake_case")]attribute ensures that all GraphQL fields follow snake_case naming conventions, improving API consistency.The use of
Option<Box<AccountFilter>>for theorfield avoids recursive type size issues by boxing the nested filter.
Interactions with Other System Components
Utilizes the
OptStringFilterandWhereOpfrom thecrate::schema::graphql::filtermodule, indicating integration with a broader filtering and querying framework within the system.The
AccountFilterstruct serves as a building block for account-related query APIs in GraphQL, interacting with the GraphQL schema definition and query execution layers.Serialization via
serdesuggests that filters can be communicated over network boundaries or persisted if needed.The test module depends on internal filter components like
StringFilterand theWhereOptrait, reflecting tight integration with the query filtering subsystem.
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.