Object types are the most commonly used type in indexer GraphQL schema. Each object type marked with an @entity
directive will be converted into a SQL table.
type Account @entity {
id: ID!
address: Address!
balance: U64!
}
This Account
object type from the GraphQL schema, might be used in an indexer module like so:
extern crate alloc;
use fuel_indexer_utils::prelude::*;
#[indexer(manifest = "indexer.manifest.yaml")]
mod indexer_mod {
fn handle_event(event: Event) {
let address = Address::default();
let balance = 0;
let account = Account::new(address, balance);
account.save();
}
}
Enum types are simply implemented as String types.
enum SignatureLabel {
Multi
Single
}
Enum types in relation to Fuel indexer's implementation are just
String
types used primarily to label object types. There is no other way thatenum
types should be used at this time. ThisSignatureLabel
object type from the GraphQL schema, might be used in an indexer module like so:
extern crate alloc;
use fuel_indexer_utils::prelude::*;
#[indexer(manifest = "indexer.manifest.yaml")]
mod indexer_mod {
fn handle_event(event: Event) {
let label = SignatureLabel::Multi;
assert_eq!(label.to_string(), "SignatureLabel::Multi".to_string());
}
}
Union types are unique in that any type marked as a union
will be converted into an Object type, who's fields are the unique set of fields over all members of the union.
enum TransactionLabel {
Create
Script
Mint
}
type CreateTransaction @entity {
id: ID!
bytecode_length: U64!
contract_id: ContractId!
label: TransactionLabel!
}
type ScriptTransaction @entity {
id: ID!
maturity: U64!
label: TransactionLabel!
}
type MintTransaction @entity {
id: ID!
metadata: Json
label: TransactionLabel!
}
union Transaction = CreateTransaction | ScriptTransaction | MintTransaction
The Transaction
union type above, will internally produce the following object type:
type Transaction @entity {
id: ID!
bytecode_length: U64!
contract_id: ContractId!
label: TransactionLabel!
maturity: U64!
metadata: Json
}
IMPORTANT: Note the order of the fields in the derived
Transaction
object type: the fields are ordered according to the unique set of fields from each of the union's members.The
id
,bytecode_length
,contract_id
, andlabel
fields come first, from theCreateTransaction
object type. Next comes thematurity
field from theScriptTransaction
object - because theScriptTransaction
'sid
andlabel
fiels are already a part of the derivedTransaction
object, courtesy of theCreateTransaction
object type. Finally, comes themetadata
field, as part of theMintTransaction
object type. ThisTransaction
union type from the GraphQL schema, might be used in an indexer module like so:
extern crate alloc;
use fuel_indexer_utils::prelude::*;
#[indexer(manifest = "indexer.manifest.yaml")]
mod indexer_mod {
fn handle_event(event: Event) {
let bytecode_length = 1024;
let contract_id = ContractId::default();
let label = TransactionLabel::Create;
let maturity = 10000000;
let metadata = None;
let transaction = Transaction::new(bytecode_length, contract_id, label, maturity, metadata);
transaction.save();
}
}