formats.rs
Overview
This file defines the BigIntFormat enumeration, which specifies the string representation formats for big integers in the context of GraphQL responses. Because GraphQL has limitations with returning large numeric values directly, big integers are serialized as strings. The BigIntFormat enum provides options to control how these big integers are formatted as strings when sent over GraphQL.
Enum: BigIntFormat
#[derive(Enum, Copy, Clone, Eq, PartialEq)]
pub enum BigIntFormat {
HEX,
DEC,
}
Description
BigIntFormat is an enum annotated with async_graphql::Enum to integrate with GraphQL schema definitions. It allows specifying the string format to be used when serializing big integers for GraphQL responses.
Variants
HEX
Represents the big integer as a hexadecimal string prefixed with"0x". This is the default format used for big integers in the system.DEC
Represents the big integer as a decimal string without any prefix.
Attributes and Derives
#[allow(clippy::upper_case_acronyms)]
This attribute suppresses Clippy lint warnings regarding the use of uppercase acronyms in the enum variant names (HEXandDEC).#[derive(Enum, Copy, Clone, Eq, PartialEq)]
These derives provide several traits:Enumfromasync_graphqlallowsBigIntFormatto be used directly in GraphQL schema definitions as an enum type.CopyandCloneallow the enum values to be duplicated easily.EqandPartialEqenable equality comparisons.
Usage Example
use async_graphql::{Enum, SimpleObject};
#[derive(Enum, Copy, Clone, Eq, PartialEq)]
pub enum BigIntFormat {
HEX,
DEC,
}
#[derive(SimpleObject)]
struct Query {
#[graphql(format = "bigint_format")]
big_number: String,
}
fn format_big_int(value: &num_bigint::BigInt, format: BigIntFormat) -> String {
match format {
BigIntFormat::HEX => format!("0x{:x}", value),
BigIntFormat::DEC => value.to_string(),
}
}
In this example, the format_big_int function converts a big integer into either hexadecimal or decimal string based on the BigIntFormat enum passed.
Implementation Details
The enum addresses the limitation of GraphQL not supporting big integer types natively by providing a way to specify how big integers should be serialized as strings.
The default hexadecimal format includes the
"0x"prefix to clearly indicate the base.The enum integrates seamlessly with the GraphQL schema due to the
async_graphql::Enumderive, allowing clients to specify their preferred format.
Interaction with Other Parts of the System
This enum is typically used in modules or components responsible for serializing or formatting big integer values before returning them in GraphQL queries or mutations.
It assists in maintaining consistent formatting across the system when converting large numeric values for client consumption.
Components that handle data serialization, such as GraphQL resolvers or data transformers, will reference this enum to determine output format.
Interaction with big integer arithmetic libraries (e.g.,
num-bigint) is implied when converting between numeric and string representations.
Mermaid Diagram
classDiagram
class BigIntFormat {
<<Enum>>
+HEX
+DEC
}
This diagram shows the BigIntFormat enum with its two variants, representing the available string formatting options for big integers.