Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 38 additions & 4 deletions examples/amm/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use std::sync::Arc;
use amm::{Operation, Parameters};
use async_graphql::{EmptySubscription, Request, Response, Schema};
use linera_sdk::{
graphql::GraphQLMutationRoot as _, linera_base_types::WithServiceAbi, views::View, Service,
graphql::GraphQLMutationRoot, linera_base_types::WithServiceAbi, views::View, Service,
ServiceRuntime,
};

Expand Down Expand Up @@ -41,12 +41,46 @@ impl Service for AmmService {
}

async fn handle_query(&self, request: Request) -> Response {
let schema = Schema::build(
self.schema().execute(request).await
}
}

impl AmmService {
/// Builds the GraphQL schema served by [`Self::handle_query`].
fn schema(
&self,
) -> Schema<
Arc<AmmState>,
<Operation as GraphQLMutationRoot<AmmService>>::MutationRoot,
EmptySubscription,
> {
Schema::build(
self.state.clone(),
Operation::mutation_root(self.runtime.clone()),
EmptySubscription,
)
.finish();
schema.execute(request).await
.finish()
}
}

#[cfg(all(test, not(target_arch = "wasm32")))]
mod tests {
use linera_sdk::{util::BlockingWait, views::View, ServiceRuntime};

use super::*;

#[test]
fn schema_sdl() {
let runtime = ServiceRuntime::<AmmService>::new();
let state = AmmState::load(runtime.root_view_storage_context())
.blocking_wait()
.expect("Failed to read from mock key value store");

let service = AmmService {
state: Arc::new(state),
runtime: Arc::new(runtime),
};

insta::assert_snapshot!(service.schema().sdl());
}
}
95 changes: 95 additions & 0 deletions examples/amm/src/snapshots/amm_service__tests__schema_sdl.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
---
source: amm/src/service.rs
expression: service.schema().sdl()
---
"""
An account.
"""
input Account {
"""
The chain of the account.
"""
chainId: ChainId!
"""
The owner of the account.
"""
owner: AccountOwner!
}

"""
An account.
"""
type AccountOutput {
"""
The chain of the account.
"""
chainId: ChainId!
"""
The owner of the account.
"""
owner: AccountOwner!
}

"""
A unique identifier for a user or an application.
"""
scalar AccountOwner

type AmmState {
shares: MapView_AccountOutput_Amount_d6d2d63e!
totalSharesSupply: Amount!
}

"""
A non-negative amount of tokens.
"""
scalar Amount

"""
The unique identifier (UID) of a chain. This is currently computed as the hash value of a ChainDescription.
"""
scalar ChainId

"""
A GraphQL-visible map item, complete with key.
"""
type Entry_AccountOutput_Amount_eed88548 {
key: AccountOutput!
value: Amount
}

input MapFilters_Account_b5463aa1 {
keys: [Account!]
}

input MapInput_Account_b5463aa1 {
filters: MapFilters_Account_b5463aa1
}

type MapView_AccountOutput_Amount_d6d2d63e {
keys(count: Int): [AccountOutput!]!
count: Int!
entry(key: Account!): Entry_AccountOutput_Amount_eed88548!
entries(input: MapInput_Account_b5463aa1): [Entry_AccountOutput_Amount_eed88548!]!
}

type OperationMutationRoot {
swap(owner: AccountOwner!, inputTokenIdx: Int!, inputAmount: Amount!): [Int!]!
addLiquidity(owner: AccountOwner!, maxToken0Amount: Amount!, maxToken1Amount: Amount!): [Int!]!
removeLiquidity(owner: AccountOwner!, tokenToRemoveIdx: Int!, tokenToRemoveAmount: Amount!): [Int!]!
removeAllAddedLiquidity(owner: AccountOwner!): [Int!]!
closeChain: [Int!]!
}

"""
Directs the executor to include this field or fragment only when the `if` argument is true.
"""
directive @include(if: Boolean!) on FIELD | FRAGMENT_SPREAD | INLINE_FRAGMENT
"""
Directs the executor to skip this field or fragment when the `if` argument is true.
"""
directive @skip(if: Boolean!) on FIELD | FRAGMENT_SPREAD | INLINE_FRAGMENT
schema {
query: AmmState
mutation: OperationMutationRoot
}
23 changes: 21 additions & 2 deletions examples/controller/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,26 @@ impl Service for ControllerService {
}

async fn handle_query(&self, query: Self::Query) -> Self::QueryResponse {
self.schema().execute(query).await
}
}

impl ControllerService {
/// Builds the GraphQL schema served by [`Self::handle_query`].
fn schema(
&self,
) -> Schema<
Arc<ControllerState>,
<Operation as GraphQLMutationRoot<ControllerService>>::MutationRoot,
EmptySubscription,
> {
Schema::build(
self.state.clone(),
Operation::mutation_root(self.runtime.clone()),
EmptySubscription,
)
.data(self.runtime.clone())
.finish()
.execute(query)
.await
}
}

Expand Down Expand Up @@ -163,6 +174,14 @@ mod tests {
}
}

#[cfg(not(target_arch = "wasm32"))]
#[test]
fn schema_sdl() {
let service = create_service();

insta::assert_snapshot!(service.schema().sdl());
}

#[test]
fn query_local_worker_state_empty() {
let service = create_service();
Expand Down
Loading
Loading