-
Notifications
You must be signed in to change notification settings - Fork 91
/
service.rs
104 lines (83 loc) · 2.43 KB
/
service.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
// Copyright (c) Zefchain Labs, Inc.
// SPDX-License-Identifier: Apache-2.0
#![cfg_attr(target_arch = "wasm32", no_main)]
mod state;
use async_graphql::{EmptySubscription, Object, Request, Response, Schema};
use linera_sdk::{
base::WithServiceAbi,
views::{View, ViewStorageContext},
Service, ServiceRuntime,
};
use self::state::Counter;
pub struct CounterService {
state: Counter,
}
linera_sdk::service!(CounterService);
impl WithServiceAbi for CounterService {
type Abi = counter::CounterAbi;
}
impl Service for CounterService {
type Parameters = ();
async fn new(runtime: ServiceRuntime<Self>) -> Self {
let state = Counter::load(ViewStorageContext::from(runtime.key_value_store()))
.await
.expect("Failed to load state");
CounterService { state }
}
async fn handle_query(&self, request: Request) -> Response {
let schema = Schema::build(
QueryRoot {
value: *self.state.value.get(),
},
MutationRoot {},
EmptySubscription,
)
.finish();
schema.execute(request).await
}
}
struct MutationRoot;
#[Object]
impl MutationRoot {
async fn increment(&self, value: u64) -> Vec<u8> {
bcs::to_bytes(&value).unwrap()
}
}
struct QueryRoot {
value: u64,
}
#[Object]
impl QueryRoot {
async fn value(&self) -> &u64 {
&self.value
}
}
#[cfg(test)]
mod tests {
use async_graphql::{Request, Response, Value};
use futures::FutureExt;
use linera_sdk::{
util::BlockingWait,
views::{View, ViewStorageContext},
Service, ServiceRuntime,
};
use serde_json::json;
use super::{Counter, CounterService};
#[test]
fn query() {
let value = 61_098_721_u64;
let runtime = ServiceRuntime::<CounterService>::new();
let mut state = Counter::load(ViewStorageContext::from(runtime.key_value_store()))
.blocking_wait()
.expect("Failed to read from mock key value store");
state.value.set(value);
let service = CounterService { state };
let request = Request::new("{ value }");
let response = service
.handle_query(request)
.now_or_never()
.expect("Query should not await anything");
let expected = Response::new(Value::from_json(json!({"value" : 61_098_721})).unwrap());
assert_eq!(response, expected)
}
}