Skip to content

Commit 25d4466

Browse files
Streamline the use of GraphQLMutationRoot. (#4538)
## Motivation The `GraphQLMutationRoot` is used for some smart contracts like `non-fungible` when it is not needed. ## Proposal There are two ways to implement the trait `MutationRoot`: * One is to use the function `mutation_root` of the trait `GraphQLMutationRoot` implemented by the derive macro. For example, `counter`. * Implement the trait explicitly. For example, `non-fungible`. Therefore, two changes are made: * When the second strategy is used, we should not implement the `GraphQLMutationRoot` (3 cases removed). * When the trait is being used, we formulate it as `GraphQLMutationRoot as _` which is clearer (and examples are supposed to help). ## Test Plan The CI. ## Release Plan It does not break anything. ## Links None.
1 parent 2502447 commit 25d4466

File tree

21 files changed

+46
-42
lines changed

21 files changed

+46
-42
lines changed

examples/amm/src/service.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use std::sync::Arc;
1010
use amm::{Operation, Parameters};
1111
use async_graphql::{EmptySubscription, Request, Response, Schema};
1212
use linera_sdk::{
13-
graphql::GraphQLMutationRoot, linera_base_types::WithServiceAbi, views::View, Service,
13+
graphql::GraphQLMutationRoot as _, linera_base_types::WithServiceAbi, views::View, Service,
1414
ServiceRuntime,
1515
};
1616

examples/counter/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ resulting block, if any.
113113

114114
```gql,uri=http://localhost:8080/chains/$CHAIN/applications/$LINERA_APPLICATION_ID
115115
mutation Increment {
116-
increment(field0: 3)
116+
increment(value: 3)
117117
}
118118
```
119119

examples/counter/src/contract.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,8 @@ impl Contract for CounterContract {
4646
}
4747

4848
async fn execute_operation(&mut self, operation: CounterOperation) -> u64 {
49-
let CounterOperation::Increment(operation) = operation;
50-
let new_value = self.state.value.get() + operation;
49+
let CounterOperation::Increment { value } = operation;
50+
let new_value = self.state.value.get() + value;
5151
self.state.value.set(new_value);
5252
new_value
5353
}
@@ -75,7 +75,7 @@ mod tests {
7575
let mut counter = create_and_instantiate_counter(initial_value);
7676

7777
let increment = 42_308_u64;
78-
let operation = CounterOperation::Increment(increment);
78+
let operation = CounterOperation::Increment { value: increment };
7979

8080
let response = counter
8181
.execute_operation(operation)
@@ -106,7 +106,7 @@ mod tests {
106106
let mut counter = create_and_instantiate_counter(initial_value);
107107

108108
let increment = 8_u64;
109-
let operation = CounterOperation::Increment(increment);
109+
let operation = CounterOperation::Increment { value: increment };
110110

111111
let response = counter
112112
.execute_operation(operation)

examples/counter/src/lib.rs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,15 @@
44
/*! ABI of the Counter Example Application */
55

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

1310
pub struct CounterAbi;
1411

15-
#[derive(Debug, Deserialize, Serialize, GraphQLMutationRoot)]
12+
#[derive(Debug, Deserialize, Serialize)]
1613
pub enum CounterOperation {
1714
/// Increment the counter by the given value
18-
Increment(u64),
15+
Increment { value: u64 },
1916
}
2017

2118
impl ContractAbi for CounterAbi {

examples/counter/src/service.rs

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,9 @@ mod state;
77

88
use std::sync::Arc;
99

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

1714
use self::state::CounterState;
1815

@@ -43,14 +40,29 @@ impl Service for CounterService {
4340
async fn handle_query(&self, request: Request) -> Response {
4441
let schema = Schema::build(
4542
self.state.clone(),
46-
CounterOperation::mutation_root(self.runtime.clone()),
43+
MutationRoot {
44+
runtime: self.runtime.clone(),
45+
},
4746
EmptySubscription,
4847
)
4948
.finish();
5049
schema.execute(request).await
5150
}
5251
}
5352

53+
struct MutationRoot {
54+
runtime: Arc<ServiceRuntime<CounterService>>,
55+
}
56+
57+
#[Object]
58+
impl MutationRoot {
59+
async fn increment(&self, value: u64) -> [u8; 0] {
60+
let operation = CounterOperation::Increment { value };
61+
self.runtime.schedule_operation(&operation);
62+
[]
63+
}
64+
}
65+
5466
#[cfg(test)]
5567
mod tests {
5668
use std::sync::Arc;

examples/counter/tests/single_chain.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ async fn single_chain_test() {
2424
.await;
2525

2626
let increment = 15u64;
27-
let operation = CounterOperation::Increment(increment);
27+
let operation = CounterOperation::Increment { value: increment };
2828
chain
2929
.add_block(|block| {
3030
block.with_operation(application_id, operation);

examples/crowd-funding/src/service.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use std::sync::Arc;
1010
use async_graphql::{EmptySubscription, Request, Response, Schema};
1111
use crowd_funding::Operation;
1212
use linera_sdk::{
13-
graphql::GraphQLMutationRoot,
13+
graphql::GraphQLMutationRoot as _,
1414
linera_base_types::{ApplicationId, WithServiceAbi},
1515
views::View,
1616
Service, ServiceRuntime,

examples/ethereum-tracker/src/service.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use async_graphql::{EmptySubscription, Request, Response, Schema};
1212
use ethereum_tracker::Operation;
1313
use linera_sdk::{
1414
ethereum::{EthereumDataType, EthereumEvent, EthereumQueries, ServiceEthereumClient},
15-
graphql::GraphQLMutationRoot,
15+
graphql::GraphQLMutationRoot as _,
1616
linera_base_types::WithServiceAbi,
1717
views::View,
1818
Service, ServiceRuntime,

examples/fungible/src/service.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use async_graphql::{EmptySubscription, Object, Request, Response, Schema};
1111
use fungible::{OwnerSpender, Parameters};
1212
use linera_sdk::{
1313
abis::fungible::FungibleOperation,
14-
graphql::GraphQLMutationRoot,
14+
graphql::GraphQLMutationRoot as _,
1515
linera_base_types::{AccountOwner, Amount, WithServiceAbi},
1616
views::{MapView, View},
1717
Service, ServiceRuntime,

examples/gen-nft/src/lib.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ use std::fmt::{Display, Formatter};
77

88
use async_graphql::{InputObject, Request, Response, SimpleObject};
99
use linera_sdk::{
10-
graphql::GraphQLMutationRoot,
1110
linera_base_types::{Account, AccountOwner, ApplicationId, ChainId, ContractAbi, ServiceAbi},
1211
ToBcsBytes,
1312
};
@@ -34,7 +33,7 @@ impl ServiceAbi for GenNftAbi {
3433
}
3534

3635
/// An operation.
37-
#[derive(Debug, Deserialize, Serialize, GraphQLMutationRoot)]
36+
#[derive(Debug, Deserialize, Serialize)]
3837
pub enum Operation {
3938
/// Mints a token
4039
Mint {

0 commit comments

Comments
 (0)