fuel_gql_client/client/
schema.rs

1// this is the format cynic expects
2#[allow(clippy::module_inception)]
3pub mod schema {
4    cynic::use_schema!("./assets/schema.sdl");
5}
6
7use hex::FromHexError;
8use std::{
9    array::TryFromSliceError,
10    fmt::{
11        self,
12        Debug,
13    },
14    io::ErrorKind,
15    num::TryFromIntError,
16};
17use thiserror::Error;
18
19pub use primitives::*;
20
21pub mod balance;
22pub mod block;
23pub mod chain;
24pub mod coin;
25pub mod contract;
26pub mod message;
27pub mod node_info;
28pub mod primitives;
29pub mod resource;
30pub mod tx;
31
32#[derive(cynic::QueryFragment, Debug)]
33#[cynic(schema_path = "./assets/schema.sdl", graphql_type = "Query")]
34pub struct Health {
35    pub health: bool,
36}
37
38#[derive(cynic::QueryFragment, Debug)]
39#[cynic(schema_path = "./assets/schema.sdl", graphql_type = "Mutation")]
40pub struct StartSession {
41    pub start_session: cynic::Id,
42}
43
44#[derive(cynic::QueryVariables)]
45pub struct IdArg {
46    pub id: cynic::Id,
47}
48
49#[derive(cynic::QueryFragment, Debug)]
50#[cynic(
51    schema_path = "./assets/schema.sdl",
52    graphql_type = "Mutation",
53    variables = "IdArg"
54)]
55pub struct EndSession {
56    #[arguments(id: $id)]
57    pub end_session: bool,
58}
59
60#[derive(cynic::QueryFragment, Debug)]
61#[cynic(
62    schema_path = "./assets/schema.sdl",
63    graphql_type = "Mutation",
64    variables = "IdArg"
65)]
66pub struct Reset {
67    #[arguments(id: $id)]
68    pub reset: bool,
69}
70
71#[derive(cynic::QueryVariables)]
72pub struct ExecuteArgs {
73    pub id: cynic::Id,
74    pub op: String,
75}
76
77#[derive(cynic::QueryFragment, Debug)]
78#[cynic(
79    schema_path = "./assets/schema.sdl",
80    graphql_type = "Mutation",
81    variables = "ExecuteArgs"
82)]
83pub struct Execute {
84    #[arguments(id: $id, op: $op)]
85    pub execute: bool,
86}
87
88#[derive(cynic::QueryVariables)]
89pub struct RegisterArgs {
90    pub id: cynic::Id,
91    pub register: U64,
92}
93
94#[derive(cynic::QueryFragment, Debug)]
95#[cynic(
96    schema_path = "./assets/schema.sdl",
97    graphql_type = "Query",
98    variables = "RegisterArgs"
99)]
100pub struct Register {
101    #[arguments(id: $id, register: $register)]
102    pub register: U64,
103}
104
105#[derive(cynic::QueryVariables)]
106pub struct MemoryArgs {
107    pub id: cynic::Id,
108    pub start: U64,
109    pub size: U64,
110}
111
112#[derive(cynic::QueryFragment, Debug)]
113#[cynic(
114    schema_path = "./assets/schema.sdl",
115    graphql_type = "Query",
116    variables = "MemoryArgs"
117)]
118pub struct Memory {
119    #[arguments(id: $id, start: $start, size: $size)]
120    pub memory: String,
121}
122
123#[derive(cynic::QueryVariables, Debug)]
124pub struct SetBreakpointArgs {
125    pub id: cynic::Id,
126    pub bp: Breakpoint,
127}
128
129#[derive(cynic::QueryFragment, Debug)]
130#[cynic(
131    schema_path = "./assets/schema.sdl",
132    graphql_type = "Mutation",
133    variables = "SetBreakpointArgs"
134)]
135pub struct SetBreakpoint {
136    #[arguments(id: $id, breakpoint: $bp)]
137    pub set_breakpoint: bool,
138}
139
140#[derive(cynic::InputObject, Debug)]
141#[cynic(schema_path = "./assets/schema.sdl")]
142pub struct Breakpoint {
143    pub contract: ContractId,
144    pub pc: U64,
145}
146
147#[derive(cynic::QueryVariables, Debug)]
148pub struct SetSingleSteppingArgs {
149    pub id: cynic::Id,
150    pub enable: bool,
151}
152
153#[derive(cynic::QueryFragment, Debug)]
154#[cynic(
155    schema_path = "./assets/schema.sdl",
156    graphql_type = "Mutation",
157    variables = "SetSingleSteppingArgs"
158)]
159pub struct SetSingleStepping {
160    #[arguments(id: $id, enable: $enable)]
161    pub set_single_stepping: bool,
162}
163
164#[derive(cynic::QueryVariables, Debug)]
165pub struct StartTxArgs {
166    pub id: cynic::Id,
167    pub tx: String,
168}
169
170#[derive(cynic::QueryFragment, Debug)]
171#[cynic(
172    schema_path = "./assets/schema.sdl",
173    graphql_type = "Mutation",
174    variables = "StartTxArgs"
175)]
176pub struct StartTx {
177    #[arguments(id: $id, txJson: $tx)]
178    pub start_tx: RunResult,
179}
180
181#[derive(cynic::QueryVariables, Debug)]
182pub struct ContinueTxArgs {
183    pub id: cynic::Id,
184}
185
186#[derive(cynic::QueryFragment, Debug)]
187#[cynic(
188    schema_path = "./assets/schema.sdl",
189    graphql_type = "Mutation",
190    variables = "ContinueTxArgs"
191)]
192pub struct ContinueTx {
193    #[arguments(id: $id)]
194    pub continue_tx: RunResult,
195}
196
197#[derive(cynic::QueryFragment, Debug)]
198#[cynic(schema_path = "./assets/schema.sdl")]
199pub struct RunResult {
200    pub breakpoint: Option<OutputBreakpoint>,
201    pub json_receipts: Vec<String>,
202}
203
204impl RunResult {
205    pub fn receipts(&self) -> impl Iterator<Item = ::fuel_vm::fuel_tx::Receipt> + '_ {
206        self.json_receipts.iter().map(|r| {
207            serde_json::from_str::<::fuel_vm::fuel_tx::Receipt>(r)
208                .expect("Receipt deserialization failed, server/client version mismatch")
209        })
210    }
211}
212
213impl fmt::Display for RunResult {
214    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
215        let receipts: Vec<::fuel_vm::fuel_tx::Receipt> = self.receipts().collect();
216        f.debug_struct("RunResult")
217            .field("breakpoint", &self.breakpoint)
218            .field("receipts", &receipts)
219            .finish()
220    }
221}
222
223#[derive(cynic::QueryFragment, Debug)]
224#[cynic(schema_path = "./assets/schema.sdl")]
225pub struct OutputBreakpoint {
226    pub contract: ContractId,
227    pub pc: U64,
228}
229
230/// Generic graphql pagination query args
231#[derive(cynic::QueryVariables, Debug, Default)]
232pub struct ConnectionArgs {
233    /// Skip until cursor (forward pagination)
234    pub after: Option<String>,
235    /// Skip until cursor (backward pagination)
236    pub before: Option<String>,
237    /// Retrieve the first n items in order (forward pagination)
238    pub first: Option<i32>,
239    /// Retrieve the last n items in order (backward pagination).
240    /// Can't be used at the same time as `first`.
241    pub last: Option<i32>,
242}
243
244#[derive(cynic::QueryFragment, Debug)]
245#[cynic(schema_path = "./assets/schema.sdl")]
246pub struct PageInfo {
247    pub end_cursor: Option<String>,
248    pub has_next_page: bool,
249    pub has_previous_page: bool,
250    pub start_cursor: Option<String>,
251}
252
253/// Specifies the direction of a paginated query
254#[derive(Clone, Debug)]
255pub enum PageDirection {
256    Forward,
257    Backward,
258}
259
260/// Used to parameterize paginated queries
261#[derive(Clone, Debug)]
262pub struct PaginationRequest<T> {
263    /// The cursor returned from a previous query to indicate an offset
264    pub cursor: Option<T>,
265    /// The number of results to take
266    pub results: usize,
267    /// The direction of the query (e.g. asc, desc order).
268    pub direction: PageDirection,
269}
270
271impl<T: Into<String>> From<PaginationRequest<T>> for ConnectionArgs {
272    fn from(req: PaginationRequest<T>) -> Self {
273        match req.direction {
274            PageDirection::Forward => Self {
275                after: req.cursor.map(Into::into),
276                before: None,
277                first: Some(req.results as i32),
278                last: None,
279            },
280            PageDirection::Backward => Self {
281                after: None,
282                before: req.cursor.map(Into::into),
283                first: None,
284                last: Some(req.results as i32),
285            },
286        }
287    }
288}
289
290pub struct PaginatedResult<T, C> {
291    pub cursor: Option<C>,
292    pub results: Vec<T>,
293    pub has_next_page: bool,
294    pub has_previous_page: bool,
295}
296
297#[derive(Error, Debug)]
298#[non_exhaustive]
299pub enum ConversionError {
300    #[error("Field is required from the GraphQL response {0}")]
301    MissingField(String),
302    #[error("expected 0x prefix")]
303    HexStringPrefixError,
304    #[error("expected 32 bytes, received {0}")]
305    HexString256LengthError(usize),
306    #[error("hex parsing error {0}")]
307    HexDecodingError(FromHexError),
308    #[error("hex parsing error {0}")]
309    HexError(String),
310    #[error("failed integer conversion")]
311    IntegerConversion,
312    #[error("failed to deserialize transaction from bytes {0}")]
313    TransactionFromBytesError(std::io::Error),
314    #[error("failed to deserialize receipt from bytes {0}")]
315    ReceiptFromBytesError(std::io::Error),
316    #[error("failed to convert from bytes due to unexpected length")]
317    BytesLength,
318    #[error("Unknown variant of the {0} enum")]
319    UnknownVariant(&'static str),
320}
321
322impl From<FromHexError> for ConversionError {
323    fn from(hex_error: FromHexError) -> Self {
324        Self::HexDecodingError(hex_error)
325    }
326}
327
328impl From<ConversionError> for std::io::Error {
329    fn from(e: ConversionError) -> Self {
330        std::io::Error::new(ErrorKind::Other, e)
331    }
332}
333
334impl From<TryFromIntError> for ConversionError {
335    fn from(_: TryFromIntError) -> Self {
336        ConversionError::IntegerConversion
337    }
338}
339
340impl From<TryFromSliceError> for ConversionError {
341    fn from(_: TryFromSliceError) -> Self {
342        ConversionError::BytesLength
343    }
344}