Skip to content

Commit

Permalink
chore(datastore): add query & mutate impl (#4858)
Browse files Browse the repository at this point in the history
  • Loading branch information
Equartey committed May 15, 2024
1 parent 69a6d71 commit ab777f2
Show file tree
Hide file tree
Showing 13 changed files with 94 additions and 27 deletions.
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
## Platform files generated by Flutter during `flutter create`
**/example/android/** linguist-generated
**/example/ios/** linguist-generated
**/example/ios/unit_tests/** linguist-generated=false
**/example/linux/** linguist-generated
**/example/macos/** linguist-generated
**/example/windows/** linguist-generated
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import com.amazonaws.amplify.amplify_datastore.pigeons.NativeApiPlugin
import com.amazonaws.amplify.amplify_datastore.pigeons.NativeAuthBridge
import com.amazonaws.amplify.amplify_datastore.pigeons.NativeAuthPlugin
import com.amazonaws.amplify.amplify_datastore.pigeons.NativeAuthUser
import com.amazonaws.amplify.amplify_datastore.pigeons.NativeGraphQLSubscriptionResponse
import com.amazonaws.amplify.amplify_datastore.types.model.FlutterCustomTypeSchema
import com.amazonaws.amplify.amplify_datastore.types.model.FlutterModelSchema
import com.amazonaws.amplify.amplify_datastore.types.model.FlutterSerializedModel
Expand Down Expand Up @@ -920,6 +921,13 @@ class AmplifyDataStorePlugin :
callback(kotlin.Result.failure(e))
}
}

override fun sendSubscriptionEvent(
event: NativeGraphQLSubscriptionResponse,
callback: (kotlin.Result<Unit>) -> Unit
) {
throw NotImplementedError("Not yet implemented")
}

override fun configure(
version: String,
Expand Down
6 changes: 3 additions & 3 deletions packages/amplify_datastore/example/ios/Podfile

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ class DataStoreHubEventStreamHandlerTests: XCTestCase {
waitForExpectations(timeout: 1.0)
// cancellation needed to make sure that Hub token is invalidated to
// prevent collisions between tests
hubHandler.onCancel(withArguments: nil)
let _ = hubHandler.onCancel(withArguments: nil)
}

func test_hub_readyEvent_success() throws {
Expand All @@ -88,7 +88,7 @@ class DataStoreHubEventStreamHandlerTests: XCTestCase {
let readyEventPayload = HubPayload(eventName: HubPayload.EventName.DataStore.ready)
Amplify.Hub.dispatch(to: .dataStore, payload: readyEventPayload)
waitForExpectations(timeout: 1.0)
hubHandler.onCancel(withArguments: nil)
let _ = hubHandler.onCancel(withArguments: nil)
}

func test_hub_subscriptionEstablishedEvent_success() throws {
Expand All @@ -113,7 +113,7 @@ class DataStoreHubEventStreamHandlerTests: XCTestCase {
let subscriptionEstablishedPayload = HubPayload(eventName: HubPayload.EventName.DataStore.subscriptionsEstablished)
Amplify.Hub.dispatch(to: .dataStore, payload: subscriptionEstablishedPayload)
waitForExpectations(timeout: 1.0)
hubHandler.onCancel(withArguments: nil)
let _ = hubHandler.onCancel(withArguments: nil)
}

func test_hub_syncQueriesReadyEvent_success() throws {
Expand All @@ -138,7 +138,7 @@ class DataStoreHubEventStreamHandlerTests: XCTestCase {
let syncQueriesReadyPayload = HubPayload(eventName: HubPayload.EventName.DataStore.syncQueriesReady)
Amplify.Hub.dispatch(to: .dataStore, payload: syncQueriesReadyPayload)
waitForExpectations(timeout: 1.0)
hubHandler.onCancel(withArguments: nil)
let _ = hubHandler.onCancel(withArguments: nil)
}

func test_hub_networkStatusEvent_success() throws {
Expand Down Expand Up @@ -166,7 +166,7 @@ class DataStoreHubEventStreamHandlerTests: XCTestCase {
let networkStatusPayload = HubPayload(eventName: HubPayload.EventName.DataStore.networkStatus, data: networkStatusEvent)
Amplify.Hub.dispatch(to: .dataStore, payload: networkStatusPayload)
waitForExpectations(timeout: 1.0)
hubHandler.onCancel(withArguments: nil)
let _ = hubHandler.onCancel(withArguments: nil)
}

func test_hub_outboxStatusEvent_success() throws {
Expand Down Expand Up @@ -194,7 +194,7 @@ class DataStoreHubEventStreamHandlerTests: XCTestCase {
let outboxStatusPayload = HubPayload(eventName: HubPayload.EventName.DataStore.outboxStatus, data: outboxStatusEvent)
Amplify.Hub.dispatch(to: .dataStore, payload: outboxStatusPayload)
waitForExpectations(timeout: 1.0)
hubHandler.onCancel(withArguments: nil)
let _ = hubHandler.onCancel(withArguments: nil)
}

func test_hub_syncQueriesStartedEvent_success() throws {
Expand All @@ -221,7 +221,7 @@ class DataStoreHubEventStreamHandlerTests: XCTestCase {
let syncQueriesStartedPayload = HubPayload(eventName: HubPayload.EventName.DataStore.syncQueriesStarted, data: syncQueriesStartedEvent)
Amplify.Hub.dispatch(to: .dataStore, payload: syncQueriesStartedPayload)
waitForExpectations(timeout: 1.0)
hubHandler.onCancel(withArguments: nil)
let _ = hubHandler.onCancel(withArguments: nil)
}

func test_hub_outboxMutationEnqueued_success() throws {
Expand Down Expand Up @@ -264,7 +264,7 @@ class DataStoreHubEventStreamHandlerTests: XCTestCase {
let outboxMutationEnqueuedPayload = HubPayload(eventName: HubPayload.EventName.DataStore.outboxMutationEnqueued, data: outboxMutationEnqueuedEvent)
Amplify.Hub.dispatch(to: .dataStore, payload: outboxMutationEnqueuedPayload)
waitForExpectations(timeout: 1.0)
hubHandler.onCancel(withArguments: nil)
let _ = hubHandler.onCancel(withArguments: nil)
}

func test_hub_outboxMutationProcessedEvent_success() throws {
Expand All @@ -281,7 +281,7 @@ class DataStoreHubEventStreamHandlerTests: XCTestCase {
let syncMetaData = element["syncMetadata"] as! [String: Any]
XCTAssertEqual(flutterEvent["eventName"] as! String, "outboxMutationProcessed")
XCTAssertEqual(flutterEvent["modelName"] as! String, "Post")
XCTAssertEqual(syncMetaData["_lastChangedAt"] as? Int, 123)
XCTAssertEqual(syncMetaData["_lastChangedAt"] as? Int64, 123)
XCTAssertEqual(syncMetaData["_version"] as? Int, 1)
XCTAssertEqual(syncMetaData["_deleted"] as? Bool, false)
XCTAssertEqual(model["__modelName"] as! String, "Post")
Expand All @@ -300,7 +300,8 @@ class DataStoreHubEventStreamHandlerTests: XCTestCase {

let serializedModel = FlutterSerializedModel(map: try FlutterDataStoreRequestUtils.getJSONValue(modelMap), modelName: "Post")

let syncMetadata = MutationSyncMetadata(id: uuid,
let syncMetadata = MutationSyncMetadata(modelId: uuid,
modelName: "MutationSync",
deleted: false,
lastChangedAt: 123,
version: 1)
Expand All @@ -321,7 +322,7 @@ class DataStoreHubEventStreamHandlerTests: XCTestCase {
expect.fulfill()
}
waitForExpectations(timeout: 1.0)
hubHandler.onCancel(withArguments: nil)
let _ = hubHandler.onCancel(withArguments: nil)
}

func test_hot_restart_replays_sync_and_ready_events() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ let testSchema: ModelSchema = SchemaData.PostSchema
let amplifySuccessResults: [FlutterSerializedModel] =
(try! readJsonArray(filePath: "2_results") as! [[String: Any]]).map { serializedModel in
FlutterSerializedModel.init(
map: try! getJSONValue(serializedModel as! [String: Any]),
map: try! getJSONValue(serializedModel),
modelName: serializedModel["__modelName"] as! String
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ Widget displayQueryButtons(bool isAmplifyConfigured, String queriesToView,
return null;
},
style: ButtonStyle(
// ignore: deprecated_member_use
backgroundColor: MaterialStateProperty.all(
queriesToView == "Blog" ? Colors.white10 : Colors.white60,
),
Expand All @@ -38,6 +39,7 @@ Widget displayQueryButtons(bool isAmplifyConfigured, String queriesToView,
return null;
},
style: ButtonStyle(
// ignore: deprecated_member_use
backgroundColor: MaterialStateProperty.all(
queriesToView == "Post" ? Colors.white10 : Colors.white60,
),
Expand All @@ -56,6 +58,7 @@ Widget displayQueryButtons(bool isAmplifyConfigured, String queriesToView,
return null;
},
style: ButtonStyle(
// ignore: deprecated_member_use
backgroundColor: MaterialStateProperty.all(
queriesToView == "Comment" ? Colors.white10 : Colors.white60,
),
Expand Down Expand Up @@ -113,7 +116,10 @@ Widget getWidgetToDisplayPost(
_postsToView[i].rating.toString() +
", blog: " +
allBlogs
.firstWhere((blog) => blog.id == _postsToView[i].blog?.id)
.firstWhere(
(blog) => blog.id == _postsToView[i].blog?.id,
orElse: () => Blog(name: "Blog not found"),
)
.name,
style: TextStyle(fontSize: 14.0),
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,15 @@ public class DataStoreObserveEventStreamHandler: NSObject, FlutterStreamHandler
}

func sendEvent(flutterEvent: [String: Any]) {
eventSink?(flutterEvent)
DispatchQueue.main.async {
self.eventSink?(flutterEvent)
}
}

func sendError(flutterError: FlutterError) {
eventSink?(flutterError)
DispatchQueue.main.async {
self.eventSink?(flutterError)
}
}

public func onCancel(withArguments arguments: Any?) -> FlutterError? {
Expand Down
29 changes: 25 additions & 4 deletions packages/amplify_datastore/ios/Classes/FlutterApiPlugin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,15 @@ public class FlutterApiPlugin: APICategoryPlugin
self.nativeSubscriptionEvents = subscriptionEventBus
}

// TODO: Implment in Async Swift v2
public func query<R>(request: GraphQLRequest<R>) async throws -> GraphQLTask<R>.Success where R : Decodable {
preconditionFailure("method not supported")
let response = await asyncQuery(nativeRequest: request.toNativeGraphQLRequest())
return try decodeGraphQLPayloadJson(request: request, payload: response.payloadJson)
}

// TODO: Implment in Async Swift v2

public func mutate<R>(request: GraphQLRequest<R>) async throws -> GraphQLTask<R>.Success where R : Decodable {
preconditionFailure("method not supported")
let response = await asyncMutate(nativeRequest: request.toNativeGraphQLRequest())
return try decodeGraphQLPayloadJson(request: request, payload: response.payloadJson)
}

public func subscribe<R: Decodable>(
Expand Down Expand Up @@ -121,6 +122,26 @@ public class FlutterApiPlugin: APICategoryPlugin
)
}

func asyncQuery(nativeRequest: NativeGraphQLRequest) async -> NativeGraphQLResponse {
await withCheckedContinuation { continuation in
DispatchQueue.main.async {
self.nativeApiPlugin.query(request: nativeRequest) { response in
continuation.resume(returning: response)
}
}
}
}

func asyncMutate(nativeRequest: NativeGraphQLRequest) async -> NativeGraphQLResponse{
await withCheckedContinuation { continuation in
DispatchQueue.main.async {
self.nativeApiPlugin.mutate(request: nativeRequest) { response in
continuation.resume(returning: response)
}
}
}
}

public func configure(using configuration: Any?) throws { }

public func apiAuthProviderFactory() -> APIAuthProviderFactory {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,17 @@ public class SwiftAmplifyDataStorePlugin: NSObject, FlutterPlugin, NativeAmplify
modelSchemas.forEach { modelSchema in
modelSchemaRegistry.addModelSchema(modelName: modelSchema.name, modelSchema: modelSchema)
}

// Amplify Swift DataStore system schemas must be added manually
let systemSchemas = [
ModelSyncMetadata.schema,
MutationEvent.schema,
MutationSyncMetadata.schema
]

systemSchemas.forEach { modelSchema in
modelSchemaRegistry.addModelSchema(modelName: modelSchema.name, modelSchema: modelSchema)
}

modelSchemaRegistry.version = modelProviderVersion

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import Combine
public struct FlutterHubElement {
var model: [String: Any]
var version: Int?
// TODO: Migrate to Async Swift v2 -- Is this breaking?
var lastChangedAt: Int64?
var deleted: Bool

Expand Down
10 changes: 7 additions & 3 deletions packages/amplify_datastore/lib/amplify_datastore.dart
Original file line number Diff line number Diff line change
Expand Up @@ -321,18 +321,22 @@ class _NativeAmplifyApi

@override
Future<NativeGraphQLResponse> mutate(NativeGraphQLRequest request) async {
throw UnimplementedError();
final flutterRequest = nativeRequestToGraphQLRequest(request);
final response = await Amplify.API.mutate(request: flutterRequest).response;
return graphQLResponseToNativeResponse(response);
}

@override
Future<NativeGraphQLResponse> query(NativeGraphQLRequest request) async {
throw UnimplementedError();
final flutterRequest = nativeRequestToGraphQLRequest(request);
final response = await Amplify.API.query(request: flutterRequest).response;
return graphQLResponseToNativeResponse(response);
}

@override
Future<NativeGraphQLSubscriptionResponse> subscribe(
NativeGraphQLRequest request) async {
final flutterRequest = NativeRequestToGraphQLRequest(request);
final flutterRequest = nativeRequestToGraphQLRequest(request);

final operation = Amplify.API.subscribe(flutterRequest,
onEstablished: () => sendNativeStartAckEvent(flutterRequest.id));
Expand Down
12 changes: 12 additions & 0 deletions packages/amplify_datastore/lib/src/utils/native_api_helpers.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import 'dart:convert';

import 'package:amplify_core/amplify_core.dart';
import 'package:amplify_datastore/src/native_plugin.g.dart';
import 'package:collection/collection.dart';

/// Convert a [NativeGraphQLResponse] to a [GraphQLResponse]
GraphQLRequest<String> nativeRequestToGraphQLRequest(
Expand All @@ -13,6 +14,17 @@ GraphQLRequest<String> nativeRequestToGraphQLRequest(
);
}

/// Convert a [GraphQLResponse] to a [NativeGraphQLResponse]
NativeGraphQLResponse graphQLResponseToNativeResponse(
GraphQLResponse<String> response) {
final errorJson = jsonEncode(
response.errors.whereNotNull().map((e) => e.toJson()).toList());
return NativeGraphQLResponse(
payloadJson: response.data,
errorsJson: errorJson,
);
}

/// Returns a connecting event [NativeGraphQLResponse] for the given [subscriptionId]
NativeGraphQLSubscriptionResponse getConnectingEvent(String subscriptionId) {
final event = NativeGraphQLSubscriptionResponse(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// SPDX-License-Identifier: Apache-2.0

import Flutter
import AWSDataStorePlugin
import AmplifyPlugins
import AWSPluginsCore
import Amplify
import UIKit
Expand Down

0 comments on commit ab777f2

Please sign in to comment.