aws-amplify / amplify-swift

A declarative library for application development using cloud services.
Apache License 2.0
456 stars 198 forks source link

Using DataStore and custom mutations/queries in the same project via AppSync in iOS #1615

Closed maziarzamani closed 1 year ago

maziarzamani commented 2 years ago

Hi everyone.

I am currently working on a project where we are utilizing DataStore to its fullest extend. However we recently ran into a problem where we need to utilize custom mutations/queries, which are unfortunately not functioning with the DataStore logic, but generated in the legacy API.swift file.

So my question is, is it possible to use the DataStore API logic while also using the AppSync client? And are there any examples on this? This does not seem to be clear in the documentation, and it is lacking these use-cases side-by-side.

If the answer is "no", is there a workaround to call these custom mutation/queries?

UPDATE: On this page for iOS it describes three concepts for AWS AppSync, we are using the Amplify DataStore, but as far as I understand we also need AWS AppSync SDK, however we recently noticed that API.swift and the generated models are somehow conflicting (see the screenshot of API.swift in Xcode)?

Best regards, M.

Which Category is your question related to? Amplify and iOS

Amplify CLI Version 7.6.11

What AWS Services are you utilizing? AppSync

Screen Shot 2022-01-25 at 14 04 12
undefobj commented 2 years ago

This question is best asked in the iOS repo for specific code examples, but what you're looking for is the ability to specifically use GraphQL queries as strongly typed Swift classes via DataStore. This isn't possible right now, you will need to use the API category in your app for the direct API calls to your GraphQL endpoint. You can use both in your app however note that DataStore will return results in a local first manner, handling everything for you from sync to offline to conflict resolution. There's no integration between API category and DataStore at this time (we're looking at that in the future). Also if you want to make queries to one of the GraphQL fields that DataStore uses (e.g. a sync enabled field with versioning enabled on the data source in AppSync) make sure that you pass the current version (_version) along with any mutations as an argument or it will be rejected.

maziarzamani commented 2 years ago

This question is best asked in the iOS repo for specific code examples, but what you're looking for is the ability to specifically use GraphQL queries as strongly typed Swift classes via DataStore. This isn't possible right now, you will need to use the API category in your app for the direct API calls to your GraphQL endpoint. You can use both in your app however note that DataStore will return results in a local first manner, handling everything for you from sync to offline to conflict resolution. There's no integration between API category and DataStore at this time (we're looking at that in the future). Also if you want to make queries to one of the GraphQL fields that DataStore uses (e.g. a sync enabled field with versioning enabled on the data source in AppSync) make sure that you pass the current version (_version) along with any mutations as an argument or it will be rejected.

Thank you so much for your thorough reply. Our AppSync API is using datastore, but these endpoints are just hitting lambda functions without any database manipulation, so I assume we are not in any problems here. I updated my post yesterday and showcased some problems using API.swift file together with the Swift model files. There seem to be some clashing between the naming conventions?

lawmicha commented 2 years ago

Using AppSync SDK can get the work done if your specifically hitting other AppSync APIs that do not conflict with DataStore, but it's not a set of dependencies we're tested fully (which is probably why you are seeing some type conflicts). We'd like to support deveopers to use API plugin and DataStore in the same app, and have some plans for this (marking this issue as feature request). If it's a conflict in the naming of types generated for DataStore and API.swift, can you update API.swift manually to see if you can be unblocked? If you are infact just hitting one endpoint, you can also do custom graphql documents via https://docs.amplify.aws/lib/graphqlapi/advanced-workflows/q/platform/ios/ . Please let us know if this works for you

maziarzamani commented 2 years ago

We actually made a workaround by implementing the GraphQL calls manually with the AppSync SDK by writing them inline, not super optimal to be honest as I would expect the API.swift and DataStore to co-exist. :-/

lawmicha commented 2 years ago

Glad to hear you got it working, we would like to deprecate AppSync SDK and have developers directly use API via Amplify.API for the same use case, along side DataStore

maziarzamani commented 2 years ago

Would be great to have that feature!

lawmicha commented 1 year ago

if this is still relevant for you, we are introducing a compatibility layer to use API.swift with Amplify.API, please track the release of the documentation describing the Upgrade Guide in here https://github.com/aws-amplify/docs/pull/5649