filter.rs
Overview
This file defines the MessageFilter struct, which acts as a flexible input filter for querying message data based on various optional criteria. It is designed to be used within a GraphQL API context, allowing clients to specify complex filtering conditions for messages, including fields such as identifiers, timestamps, source and destination addresses, message types, and status codes. The filter supports logical OR operations to combine multiple filter conditions recursively.
Components
MessageFilter Struct
Purpose: Represents a set of optional filter criteria for querying messages.
Derives:
InputObject: Allows the struct to be used as an input type in GraphQL queries.Debug: Enables formatting the struct for debugging.
Serialize: Supports serialization, useful for logging or transferring filter data.
GraphQL Behavior: Field names are renamed to snake_case to conform to common GraphQL naming conventions.
Fields
Field | Type | Description |
|---|---|---|
|
| Optional string-based filter applied to the message's identifier. |
|
| Optional floating point filter for filtering messages by their creation timestamp. |
|
| Optional string filter for the destination address of the message. |
|
| Optional integer filter to specify the message type. |
|
| Optional string filter for the source address of the message. |
|
| Optional integer filter for the message status code. |
| Optional nested filter that allows combining this filter with another using logical OR. |
Usage Example
let filter = MessageFilter {
id: OptStringFilter::equals("msg123".to_string()),
created_at: OptFloatFilter::greater_than(1625097600.0),
dst: OptStringFilter::contains("0xabc123".to_string()),
msg_type: OptIntFilter::equals(1),
src: OptStringFilter::is_null(false),
status: OptIntFilter::in(vec![1, 2, 3]),
or: Some(Box::new(MessageFilter {
status: OptIntFilter::equals(4),
..Default::default()
})),
};
This example illustrates how to construct a MessageFilter that matches messages with a specific ID, created after a certain timestamp, destined for an address containing "0xabc123", with a message type of 1, a non-null source, status in a list of values, or alternatively, status equal to 4.
WhereOp Trait Implementation
MessageFilter implements the WhereOp trait, which likely defines behavior or constraints needed for filtering operations within the system. This implementation enables MessageFilter to be used polymorphically wherever a WhereOp is required, such as composing query conditions in the GraphQL resolver layer.
Implementation Details
Recursive Filtering: The
orfield allows the nesting ofMessageFilterinstances. This design supports complex logical OR combinations of filtering criteria, enabling clients to build queries that can match any of multiple conditions.Use of Optional Filters: Each field is wrapped in an optional filter type (
OptStringFilter,OptIntFilter,OptFloatFilter), which encapsulates various filtering operations (e.g., equals, greater than, contains). These types abstract the specifics of filter operations and align with GraphQL input handling.Serialization and GraphQL Compatibility: By deriving
SerializeandInputObject, the struct ensures seamless data transfer and integration with the GraphQL schema. The renaming of fields to snake_case matches common GraphQL naming conventions, enhancing client usability.
Interaction with Other Parts of the System
Filter Types: The file imports and uses several filter types (
OptStringFilter,OptIntFilter,OptFloatFilter) from thecrate::schema::graphql::filtermodule. These types provide the actual filtering logic and operation specifications.GraphQL Schema Integration: The struct defined here is part of the GraphQL input schema, likely used in query or mutation inputs to filter message entities.
Trait
WhereOp: By implementing this trait,MessageFilterintegrates into the system's filtering framework, enabling dynamic query building and execution.Potential Use in Query Resolvers: In the context of message data retrieval, instances of
MessageFilterare expected to be passed to resolver functions or database query builders to apply the specified filtering criteria.
Diagram
classDiagram
class MessageFilter {
-id: OptStringFilter
-created_at: OptFloatFilter
-dst: OptStringFilter
-msg_type: OptIntFilter
-src: OptStringFilter
-status: OptIntFilter
-or: Option<MessageFilter>
+WhereOp
}
MessageFilter --> OptStringFilter : "id, dst, src"
MessageFilter --> OptFloatFilter : "created_at"
MessageFilter --> OptIntFilter : "msg_type, status"
MessageFilter --> MessageFilter : "or (recursive)"
The diagram illustrates the MessageFilter struct with its fields and the recursive relationship via the or field. It also shows dependencies on the optional filter types. This structure supports composable and flexible message filtering within the GraphQL API.