Skip to content

Commit 692d3f6

Browse files
Make the handling of GraphQL in the counter example in line with other contracts. (#4400)
## Motivation The `counter` example was processing GraphQL queries quite differently from other examples. We put it in line with other examples. Fixes #2854 ## Proposal The work is fairly standard, but forces the introduction of a `CounterOperation` since the input must satisfy the `GraphQLMutationRoot` trait. ## Test Plan The CI. ## Release Plan - Nothing to do / These changes follow the usual release cycle. ## Links None.
1 parent ed661c5 commit 692d3f6

File tree

13 files changed

+53
-50
lines changed

13 files changed

+53
-50
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/counter/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ edition = "2021"
88
async-graphql.workspace = true
99
futures.workspace = true
1010
linera-sdk.workspace = true
11+
serde.workspace = true
1112
serde_json.workspace = true
1213

1314
[target.'cfg(not(target_arch = "wasm32"))'.dev-dependencies]

examples/counter/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ query {
8888
- To increase the value of the counter by 3, perform the `increment` operation.
8989
```gql,uri=http://localhost:8080/chains/$CHAIN_1/applications/$APPLICATION_ID
9090
mutation Increment {
91-
increment(value: 3)
91+
increment(field0: 3)
9292
}
9393
```
9494
- Running the query again would yield `4`.

examples/counter/src/contract.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
mod state;
77

8-
use counter::CounterAbi;
8+
use counter::{CounterAbi, CounterOperation};
99
use linera_sdk::{
1010
linera_base_types::WithContractAbi,
1111
views::{RootView, View},
@@ -45,7 +45,8 @@ impl Contract for CounterContract {
4545
self.state.value.set(value);
4646
}
4747

48-
async fn execute_operation(&mut self, operation: u64) -> u64 {
48+
async fn execute_operation(&mut self, operation: CounterOperation) -> u64 {
49+
let CounterOperation::Increment(operation) = operation;
4950
let new_value = self.state.value.get() + operation;
5051
self.state.value.set(new_value);
5152
new_value
@@ -62,6 +63,7 @@ impl Contract for CounterContract {
6263

6364
#[cfg(test)]
6465
mod tests {
66+
use counter::CounterOperation;
6567
use futures::FutureExt as _;
6668
use linera_sdk::{util::BlockingWait, views::View, Contract, ContractRuntime};
6769

@@ -73,9 +75,10 @@ mod tests {
7375
let mut counter = create_and_instantiate_counter(initial_value);
7476

7577
let increment = 42_308_u64;
78+
let operation = CounterOperation::Increment(increment);
7679

7780
let response = counter
78-
.execute_operation(increment)
81+
.execute_operation(operation)
7982
.now_or_never()
8083
.expect("Execution of counter operation should not await anything");
8184

@@ -103,9 +106,10 @@ mod tests {
103106
let mut counter = create_and_instantiate_counter(initial_value);
104107

105108
let increment = 8_u64;
109+
let operation = CounterOperation::Increment(increment);
106110

107111
let response = counter
108-
.execute_operation(increment)
112+
.execute_operation(operation)
109113
.now_or_never()
110114
.expect("Execution of counter operation should not await anything");
111115

examples/counter/src/lib.rs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,22 @@
44
/*! ABI of the Counter Example Application */
55

66
use async_graphql::{Request, Response};
7-
use linera_sdk::linera_base_types::{ContractAbi, ServiceAbi};
7+
use linera_sdk::{
8+
graphql::GraphQLMutationRoot,
9+
linera_base_types::{ContractAbi, ServiceAbi},
10+
};
11+
use serde::{Deserialize, Serialize};
812

913
pub struct CounterAbi;
1014

15+
#[derive(Debug, Deserialize, Serialize, GraphQLMutationRoot)]
16+
pub enum CounterOperation {
17+
/// Increment the counter by the given value
18+
Increment(u64),
19+
}
20+
1121
impl ContractAbi for CounterAbi {
12-
type Operation = u64;
22+
type Operation = CounterOperation;
1323
type Response = u64;
1424
}
1525

examples/counter/src/service.rs

Lines changed: 14 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,17 @@ mod state;
77

88
use std::sync::Arc;
99

10-
use async_graphql::{EmptySubscription, Object, Request, Response, Schema};
11-
use linera_sdk::{linera_base_types::WithServiceAbi, views::View, Service, ServiceRuntime};
10+
use async_graphql::{EmptySubscription, Request, Response, Schema};
11+
use counter::CounterOperation;
12+
use linera_sdk::{
13+
graphql::GraphQLMutationRoot, linera_base_types::WithServiceAbi, views::View, Service,
14+
ServiceRuntime,
15+
};
1216

1317
use self::state::CounterState;
1418

1519
pub struct CounterService {
16-
state: CounterState,
20+
state: Arc<CounterState>,
1721
runtime: Arc<ServiceRuntime<Self>>,
1822
}
1923

@@ -31,49 +35,22 @@ impl Service for CounterService {
3135
.await
3236
.expect("Failed to load state");
3337
CounterService {
34-
state,
38+
state: Arc::new(state),
3539
runtime: Arc::new(runtime),
3640
}
3741
}
3842

3943
async fn handle_query(&self, request: Request) -> Response {
4044
let schema = Schema::build(
41-
QueryRoot {
42-
value: *self.state.value.get(),
43-
},
44-
MutationRoot {
45-
runtime: self.runtime.clone(),
46-
},
45+
self.state.clone(),
46+
CounterOperation::mutation_root(self.runtime.clone()),
4747
EmptySubscription,
4848
)
4949
.finish();
5050
schema.execute(request).await
5151
}
5252
}
5353

54-
struct MutationRoot {
55-
runtime: Arc<ServiceRuntime<CounterService>>,
56-
}
57-
58-
#[Object]
59-
impl MutationRoot {
60-
async fn increment(&self, value: u64) -> [u8; 0] {
61-
self.runtime.schedule_operation(&value);
62-
[]
63-
}
64-
}
65-
66-
struct QueryRoot {
67-
value: u64,
68-
}
69-
70-
#[Object]
71-
impl QueryRoot {
72-
async fn value(&self) -> &u64 {
73-
&self.value
74-
}
75-
}
76-
7754
#[cfg(test)]
7855
mod tests {
7956
use std::sync::Arc;
@@ -94,7 +71,10 @@ mod tests {
9471
.expect("Failed to read from mock key value store");
9572
state.value.set(value);
9673

97-
let service = CounterService { state, runtime };
74+
let service = CounterService {
75+
state: Arc::new(state),
76+
runtime,
77+
};
9878
let request = Request::new("{ value }");
9979

10080
let response = service

examples/counter/src/state.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
use linera_sdk::views::{linera_views, RegisterView, RootView, ViewStorageContext};
55

66
/// The application state.
7-
#[derive(RootView)]
7+
#[derive(RootView, async_graphql::SimpleObject)]
88
#[view(context = ViewStorageContext)]
99
pub struct CounterState {
1010
pub value: RegisterView<u64>,

examples/counter/tests/single_chain.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
66
#![cfg(not(target_arch = "wasm32"))]
77

8+
use counter::CounterOperation;
89
use linera_sdk::test::{QueryOutcome, TestValidator};
910

1011
/// Test setting a counter and testing its coherency across microchains.
@@ -23,9 +24,10 @@ async fn single_chain_test() {
2324
.await;
2425

2526
let increment = 15u64;
27+
let operation = CounterOperation::Increment(increment);
2628
chain
2729
.add_block(|block| {
28-
block.with_operation(application_id, increment);
30+
block.with_operation(application_id, operation);
2931
})
3032
.await;
3133

examples/meta-counter/src/contract.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,8 @@ impl Contract for MetaCounterContract {
9595
Message::Increment(value) => {
9696
let counter_id = self.counter_id();
9797
log::trace!("executing {} via {:?}", value, counter_id);
98-
self.runtime.call_application(true, counter_id, &value);
98+
let operation = counter::CounterOperation::Increment(value);
99+
self.runtime.call_application(true, counter_id, &operation);
99100
}
100101
}
101102
}

0 commit comments

Comments
 (0)