Skip to content

Commit

Permalink
chore: and schema support and documentation for U16 (#1481)
Browse files Browse the repository at this point in the history
* chore: document U16 entity

* fix test

* fix another test
  • Loading branch information
ra0x3 authored Nov 29, 2023
1 parent ba28add commit 205ed2d
Show file tree
Hide file tree
Showing 9 changed files with 62 additions and 16 deletions.
4 changes: 3 additions & 1 deletion docs/src/designing-a-schema/scalars.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,16 @@ The Fuel indexer has a collection of GraphQL scalars that cover virtually any va
| ContractId | `u8[32]` |
| HexString | `Vec<u8>` | Byte blob of arbitrary size |
| I128 | `i128` |
| I16 | `i16` |
| I32 | `i32` |
| I64 | `i64` |
| I8 | `i8` |
| ID | `SizedAsciiString<64>` | Alias of `UID`
| Json | `String` | JSON string of arbitary size |
| String | `String` | String of arbitrary size |
| U128 | `u128` |
| U16 | `u16` |
| U32 | `u32` |
| U64 | `u64` |
| U8 | `u8` |
| UID | `SizedAsciiString<64>` | 32-byte unique ID |
| String | `String` | String of arbitrary size |
12 changes: 12 additions & 0 deletions packages/fuel-indexer-database/database-types/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ pub enum ColumnType {
// primary key columns, so we need a version of `ID` that does not include
// a primary key constraint.
UID = 25,
I16 = 26,
U16 = 27,
}

impl From<ColumnType> for i32 {
Expand Down Expand Up @@ -108,6 +110,8 @@ impl From<ColumnType> for i32 {
ColumnType::U8 => 23,
ColumnType::Array => 24,
ColumnType::UID => 25,
ColumnType::I16 => 26,
ColumnType::U16 => 27,
}
}
}
Expand Down Expand Up @@ -154,6 +158,8 @@ impl From<i32> for ColumnType {
23 => ColumnType::U8,
24 => ColumnType::Array,
25 => ColumnType::UID,
26 => ColumnType::I16,
27 => ColumnType::U16,
_ => unimplemented!("Invalid ColumnType: {num}."),
}
}
Expand Down Expand Up @@ -188,6 +194,8 @@ impl From<&str> for ColumnType {
"U64" => ColumnType::U64,
"U8" => ColumnType::U8,
"UID" => ColumnType::UID,
"U16" => ColumnType::U16,
"I16" => ColumnType::I16,
_ => unimplemented!("Invalid ColumnType: '{name}'."),
}
}
Expand Down Expand Up @@ -356,6 +364,8 @@ impl Column {
ColumnType::U32 => "integer".to_string(),
ColumnType::U64 => "numeric(20, 0)".to_string(),
ColumnType::UID => "varchar(64)".to_string(),
ColumnType::U16 => "integer".to_string(),
ColumnType::I16 => "integer".to_string(),
ColumnType::Array => {
let t = match self.array_coltype.expect(
"Column.array_coltype cannot be None when using `ColumnType::Array`.",
Expand All @@ -364,6 +374,8 @@ impl Column {
| ColumnType::U8
| ColumnType::I32
| ColumnType::U32
| ColumnType::I16
| ColumnType::U16
| ColumnType::I64 => "bigint",
ColumnType::U64 => "numeric(20, 0)",
ColumnType::U128 | ColumnType::I128 => "numeric(39, 0)",
Expand Down
6 changes: 6 additions & 0 deletions packages/fuel-indexer-graphql/src/dynamic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,15 @@ lazy_static! {
"Bytes8",
"ContractId",
"I128",
"I16",
"I32",
"I64",
"I8",
"ID",
"Identity",
"Json",
"U128",
"U16",
"U32",
"U64",
"U8",
Expand All @@ -48,9 +50,11 @@ lazy_static! {
/// value type provided for a field filter matches the type of the scalar itself.
static ref NUMERIC_SCALAR_TYPES: HashSet<&'static str> = HashSet::from([
"I128",
"I16",
"I32",
"I64",
"U128",
"U16",
"U32",
"U64",
]);
Expand Down Expand Up @@ -79,11 +83,13 @@ lazy_static! {
"AssetId",
"ContractId",
"I128",
"I16",
"I32",
"I64",
"ID",
"Identity",
"U128",
"U16",
"U32",
"U64",
"UID",
Expand Down
2 changes: 2 additions & 0 deletions packages/fuel-indexer-lib/src/graphql/base.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ scalar Json
scalar U128
scalar U32
scalar U64
scalar I16
scalar U16
scalar U8
scalar UID
scalar String
Expand Down
20 changes: 19 additions & 1 deletion packages/fuel-indexer-schema/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,18 +62,20 @@ pub enum FtColumn {
ContractId(Option<ContractId>),
Enum(Option<String>),
I128(Option<I128>),
I16(Option<I16>),
I32(Option<I32>),
I64(Option<I64>),
I8(Option<I8>),
ID(Option<UID>),
Identity(Option<Identity>),
Json(Option<Json>),
String(Option<String>),
U128(Option<U128>),
U16(Option<U16>),
U32(Option<U32>),
U64(Option<U64>),
U8(Option<U8>),
UID(Option<UID>),
String(Option<String>),
}

impl FtColumn {
Expand Down Expand Up @@ -138,6 +140,14 @@ impl FtColumn {
Some(val) => format!("{val}"),
None => String::from(NULL_VALUE),
},
FtColumn::I16(value) => match value {
Some(val) => format!("{val}"),
None => String::from(NULL_VALUE),
},
FtColumn::U16(value) => match value {
Some(val) => format!("{val}"),
None => String::from(NULL_VALUE),
},
FtColumn::I128(value) => match value {
Some(val) => format!("{val}"),
None => String::from(NULL_VALUE),
Expand Down Expand Up @@ -278,6 +288,8 @@ mod tests {
let uint4 = FtColumn::U32(Some(u32::from_le_bytes([0x78; 4])));
let uint8 = FtColumn::U64(Some(u64::from_le_bytes([0x78; 8])));
let xstring = FtColumn::String(Some(String::from("hello world")));
let uint2 = FtColumn::U16(Some(u16::from_le_bytes([0x78; 2])));
let int2 = FtColumn::I16(Some(i16::from_le_bytes([0x78; 2])));

insta::assert_yaml_snapshot!(addr.query_fragment());
insta::assert_yaml_snapshot!(array.query_fragment());
Expand All @@ -302,6 +314,8 @@ mod tests {
insta::assert_yaml_snapshot!(uint4.query_fragment());
insta::assert_yaml_snapshot!(uint8.query_fragment());
insta::assert_yaml_snapshot!(xstring.query_fragment());
insta::assert_yaml_snapshot!(uint2.query_fragment());
insta::assert_yaml_snapshot!(int2.query_fragment());
}

#[test]
Expand Down Expand Up @@ -329,6 +343,8 @@ mod tests {
let uint4_none = FtColumn::U32(None);
let uint8_none = FtColumn::U64(None);
let xstring_none = FtColumn::String(None);
let uint2_none = FtColumn::U16(None);
let int2_none = FtColumn::I16(None);

insta::assert_yaml_snapshot!(addr_none.query_fragment());
insta::assert_yaml_snapshot!(array.query_fragment());
Expand All @@ -351,6 +367,8 @@ mod tests {
insta::assert_yaml_snapshot!(uint4_none.query_fragment());
insta::assert_yaml_snapshot!(uint8_none.query_fragment());
insta::assert_yaml_snapshot!(xstring_none.query_fragment());
insta::assert_yaml_snapshot!(uint2_none.query_fragment());
insta::assert_yaml_snapshot!(int2_none.query_fragment());
}

#[test]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
source: packages/fuel-indexer-schema/src/lib.rs
expression: tai64_timestamp.query_fragment()
expression: uint2.query_fragment()
---
"'0000000000000000'"
"30840"

Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
source: packages/fuel-indexer-schema/src/lib.rs
expression: message_id.query_fragment()
expression: int2.query_fragment()
---
"'0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f'"
"30840"

Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ type Book @entity {
type Sponsor @entity {
id: ID!
name: String! @unique
amount: U64!
amount: U16!
representative: Person!
}

Expand Down
24 changes: 15 additions & 9 deletions packages/fuel-indexer-types/src/scalar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,39 +8,45 @@ use serde::{Deserialize, Serialize};
/// Scalar for 256-bit value.
pub type B256 = [u8; 32];

/// Scalar for 32-byte unique ID payloads.
/// Scalar for 512-bit unique ID payloads.
pub type UID = SizedAsciiString<64>;

/// Scalar for object IDs.
pub type ID = UID;

/// Scalar for 4-byte signed integers.
/// Scalar for 32-bit signed integers.
pub type I32 = i32;

/// Scalar for 8-byte signed integers.
/// Scalar for 64-bit signed integers.
pub type I64 = i64;

/// Scalar for 16-byte signed integers.
/// Scalar for 128-bit signed integers.
pub type I128 = i128;

/// Scalar for 4-byte unsigned integers.
/// Scalar for 32-bit unsigned integers.
pub type U32 = u32;

/// Scalar for 8-byte unsigned integers.
/// Scalar for 64-bit unsigned integers.
pub type U64 = u64;

/// Scalar for 16-byte unsigned integers.
/// Scalar for 128-bit unsigned integers.
pub type U128 = u128;

/// Scalar for boolean.
pub type Boolean = bool;

/// Scalar for 1-byte signed integers.
/// Scalar for 8-bit signed integers.
pub type I8 = i8;

/// Scalar for 1-byte unsigned integers.
/// Scalar for 8-bit unsigned integers.
pub type U8 = u8;

/// Scalar for 16-bit signed integers.
pub type U16 = u16;

/// Scalar for 16-bit unsigned integers.
pub type I16 = i16;

/// Scalar for arbitrarily-sized byte payloads.
pub type Bytes = Vec<u8>;

Expand Down

0 comments on commit 205ed2d

Please sign in to comment.