use crate::fuel_core_graphql_api::{ database::{ OffChainView, OnChainView, }, ports::{ OffChainDatabase, OnChainDatabase, }, }; use fuel_core_storage::{ transactional::AtomicView, Result as StorageResult, }; use std::sync::Arc; /// The GraphQL can't work with the generics in [`async_graphql::Context::data_unchecked`] and requires a known type. /// It is an `Arc` wrapper around the generic for on-chain and off-chain databases. pub struct ArcWrapper { inner: Provider, _marker: core::marker::PhantomData, } impl ArcWrapper { pub fn new(inner: Provider) -> Self { Self { inner, _marker: core::marker::PhantomData, } } } impl AtomicView for ArcWrapper where Provider: AtomicView, View: OnChainDatabase + 'static, { type View = OnChainView; type Height = Height; fn latest_height(&self) -> Option { self.inner.latest_height() } fn view_at(&self, height: &Height) -> StorageResult { let view = self.inner.view_at(height)?; Ok(Arc::new(view)) } fn latest_view(&self) -> Self::View { Arc::new(self.inner.latest_view()) } } impl AtomicView for ArcWrapper where Provider: AtomicView, View: OffChainDatabase + 'static, { type View = OffChainView; type Height = Height; fn latest_height(&self) -> Option { self.inner.latest_height() } fn view_at(&self, height: &Height) -> StorageResult { let view = self.inner.view_at(height)?; Ok(Arc::new(view)) } fn latest_view(&self) -> Self::View { Arc::new(self.inner.latest_view()) } }