|
| 1 | +use lambda_appsync::{ |
| 2 | + appsync_operation, make_appsync, |
| 3 | + subscription_filters::{FieldPath, FilterGroup}, |
| 4 | + AppsyncError, ID, |
| 5 | +}; |
| 6 | + |
| 7 | +make_appsync!("../../../../schema.graphql"); |
| 8 | +fn main() {} |
| 9 | + |
| 10 | +// Query operations |
| 11 | +#[appsync_operation(query(players), inline_and_remove)] |
| 12 | +async fn get_players() -> Result<Vec<Player>, AppsyncError> { |
| 13 | + Ok(vec![]) |
| 14 | +} |
| 15 | + |
| 16 | +#[appsync_operation(query(gameStatus), inline_and_remove)] |
| 17 | +async fn get_game_status() -> Result<GameStatus, AppsyncError> { |
| 18 | + Ok(GameStatus::Started) |
| 19 | +} |
| 20 | + |
| 21 | +#[appsync_operation(query(player), inline_and_remove)] |
| 22 | +async fn get_player(id: ID) -> Result<Option<Player>, AppsyncError> { |
| 23 | + Ok(Some(Player { |
| 24 | + id, |
| 25 | + name: "Test".into(), |
| 26 | + team: Team::Rust, |
| 27 | + })) |
| 28 | +} |
| 29 | + |
| 30 | +// Mutation operations |
| 31 | +#[appsync_operation(mutation(createPlayer), inline_and_remove)] |
| 32 | +async fn create_player(name: String) -> Result<Player, AppsyncError> { |
| 33 | + Ok(Player { |
| 34 | + id: ID::new(), |
| 35 | + name, |
| 36 | + team: Team::Rust, |
| 37 | + }) |
| 38 | +} |
| 39 | + |
| 40 | +#[appsync_operation(mutation(deletePlayer), inline_and_remove)] |
| 41 | +async fn delete_player(id: ID) -> Result<Player, AppsyncError> { |
| 42 | + Ok(Player { |
| 43 | + id, |
| 44 | + name: "Deleted".into(), |
| 45 | + team: Team::MultiWordsTeam, |
| 46 | + }) |
| 47 | +} |
| 48 | + |
| 49 | +#[appsync_operation(mutation(setGameStatus), inline_and_remove)] |
| 50 | +async fn set_game_status() -> Result<GameStatus, AppsyncError> { |
| 51 | + Ok(GameStatus::Started) |
| 52 | +} |
| 53 | + |
| 54 | +// Subscription operations |
| 55 | +#[appsync_operation(subscription(onCreatePlayer), inline_and_remove)] |
| 56 | +async fn on_create_player(name: String) -> Result<Option<FilterGroup>, AppsyncError> { |
| 57 | + Ok(Some(FieldPath::new("name")?.contains(name).into())) |
| 58 | +} |
| 59 | + |
| 60 | +#[appsync_operation(subscription(onDeletePlayer), inline_and_remove)] |
| 61 | +async fn on_delete_player(id: ID) -> Result<Option<FilterGroup>, AppsyncError> { |
| 62 | + Ok(Some(FieldPath::new("id")?.eq(id).into())) |
| 63 | +} |
| 64 | + |
| 65 | +#[appsync_operation(subscription(onGameStatusChange), inline_and_remove)] |
| 66 | +async fn on_game_status_change() -> Result<Option<FilterGroup>, AppsyncError> { |
| 67 | + Ok(None) |
| 68 | +} |
| 69 | + |
| 70 | +// Test that we cannot call the original functions |
| 71 | +async fn test_original_fn() { |
| 72 | + let _ = create_player("test".to_string()).await; |
| 73 | + let _ = get_players().await; |
| 74 | + let _ = get_game_status().await; |
| 75 | + let _ = get_player(ID::new()).await; |
| 76 | + let _ = delete_player(ID::new()).await; |
| 77 | + let _ = set_game_status().await; |
| 78 | + let _ = on_create_player("test".to_string()).await; |
| 79 | + let _ = on_delete_player(ID::new()).await; |
| 80 | + let _ = on_game_status_change().await; |
| 81 | +} |
0 commit comments