Using GraphQL API

You are currently viewing the AWS SDK for Mobile documentation which is a collection of low-level libraries. Use the Amplify libraries for all new app development. Learn more

You can view the Mobile SDK API reference here.

Note: Please review the documentation for API before you proceed with the rest of this section.

You can upload and download Amazon S3 Objects using AWS AppSync, a GraphQL based solution to build data-driven apps with real-time and offline capabilities. Sometimes you might want to create logical objects that have more complex data, such as images or videos, as part of their structure. For example, you might create a Person type with a profile picture or a Post type that has an associated image. You can use AWS AppSync to model these as GraphQL types. If any of your mutations have a variable with bucket, key, region, mimeType, and localUri fields, the SDK uploads the file to S3 for you.

Update file amplify/backend/api/yourAppName/schema.graphql using the following schema which has a S3Object type:

1type Picture @model {
2 id: ID!
3 name: String
4 owner: String
5 visibility: Visibility
6 file: S3Object
7 createdAt: String
8}
9
10type S3Object {
11 bucket: String!
12 region: String!
13 key: String!
14 localUri: String!
15 mimeType: String!
16}
17
18enum Visibility {
19 public
20 private
21}

and run amplify push to update the AppSync backend resource, remember to choose update API.swift when Amplify CLI prompts the choice

The AWS AppSync SDK doesn't take a direct dependency on the AWS SDK for S3, but takes in AWSS3TransferUtility. You will also need to take a dependency on the AWSS3 SDK. You can do that by updating your Podfile:

1target: 'PostsApp' do
2 use_frameworks!
3 pod 'AWSAppSync'
4 pod 'AWSS3'
5end

Then run pod install to fetch the new dependencies.

Update the class AppDelegate: UIResponder, UIApplicationDelegate in AppDelegate.swift to configure AWSAppSyncClient and AWSS3TransferUtility like below:

1@UIApplicationMain
2class AppDelegate: UIResponder, UIApplicationDelegate {
3
4 var appSyncClient: AWSAppSyncClient?
5
6 func application(
7 _ application: UIApplication,
8 didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
9 ) -> Bool {
10 // Add the following config code
11 do {
12 let cacheConfiguration = try AWSAppSyncCacheConfiguration()
13 let appSyncServiceConfig = try AWSAppSyncServiceConfig()
14 let appSyncConfig = try AWSAppSyncClientConfiguration(
15 appSyncServiceConfig: appSyncServiceConfig,
16 cacheConfiguration: cacheConfiguration,
17 s3ObjectManager: AWSS3TransferUtility.default())
18 appSyncClient = try AWSAppSyncClient(appSyncConfig: appSyncConfig)
19 print("AppSync Client Initialized")
20 } catch {
21 print("Error initializing AppSync Client")
22 }
23
24 return true
25 }
26}

And to automatically upload image when doing the api mutation. You can create the following method to trigger the action:

1func uploadImage() {
2 let s3ObjectInput = S3ObjectInput(
3 bucket: "yourBucket",
4 region: "yourBucketRegion",
5 key: "public/imageName.jpeg",
6 localUri: "uriOfImageToUpload",
7 mimeType: "jpeg"
8 )
9
10 let pictureMutationInput = CreatePictureInput(
11 id: UUID().uuidString,
12 name: "yourInputName",
13 owner: "yourName",
14 visibility: Visibility(rawValue: "public"),
15 file: s3ObjectInput
16 )
17
18 appSyncClient.perform(
19 mutation: CreatePictureMutation(input: pictureMutationInput)
20 ) { (result, error) in
21 if let error = error as? AWSAppSyncClientError {
22 print("Error occurred: \(error.localizedDescription )")
23 }
24 if let resultError = result?.errors {
25 print("Error saving the item on server: \(resultError)")
26 return
27 }
28 }
29}

Note: Remember to replace the bucket and region with what you find in awsconfiguration.json

The mutation operation doesn't require any specific changes in method signature. It requires only an S3Object with bucket, key, region, localUri, and mimeType. Now when you do a mutation, it automatically uploads the specified file to Amazon S3 using the AWSS3TransferUtility client internally.