fuel_core/schema/
blob.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
use crate::{
    fuel_core_graphql_api::query_costs,
    graphql_api::IntoApiResult,
    schema::{
        scalars::{
            BlobId,
            HexString,
        },
        ReadViewProvider,
    },
};
use async_graphql::{
    Context,
    Object,
};
use fuel_core_storage::{
    not_found,
    tables::BlobData,
};
use fuel_core_types::fuel_types;

pub struct Blob(fuel_types::BlobId);

#[Object]
impl Blob {
    async fn id(&self) -> BlobId {
        self.0.into()
    }

    #[graphql(complexity = "query_costs().bytecode_read")]
    async fn bytecode(&self, ctx: &Context<'_>) -> async_graphql::Result<HexString> {
        let query = ctx.read_view()?;
        query
            .blob_bytecode(self.0)
            .map(HexString)
            .map_err(async_graphql::Error::from)
    }
}

impl From<fuel_types::BlobId> for Blob {
    fn from(value: fuel_types::BlobId) -> Self {
        Self(value)
    }
}

#[derive(Default)]
pub struct BlobQuery;

#[Object]
impl BlobQuery {
    #[graphql(complexity = "query_costs().storage_read + child_complexity")]
    async fn blob(
        &self,
        ctx: &Context<'_>,
        #[graphql(desc = "ID of the Blob")] id: BlobId,
    ) -> async_graphql::Result<Option<Blob>> {
        let query = ctx.read_view()?;
        query
            .blob_exists(id.0)
            .and_then(|blob_exists| {
                if blob_exists {
                    Ok(id.0)
                } else {
                    Err(not_found!(BlobData))
                }
            })
            .into_api_result()
    }
}