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:

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

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:

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

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:

@UIApplicationMain class AppDelegate: UIResponder, UIApplicationDelegate { var appSyncClient: AWSAppSyncClient? func application( _ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]? ) -> Bool { // Add the following config code do { let cacheConfiguration = try AWSAppSyncCacheConfiguration() let appSyncServiceConfig = try AWSAppSyncServiceConfig() let appSyncConfig = try AWSAppSyncClientConfiguration( appSyncServiceConfig: appSyncServiceConfig, cacheConfiguration: cacheConfiguration, s3ObjectManager: AWSS3TransferUtility.default()) appSyncClient = try AWSAppSyncClient(appSyncConfig: appSyncConfig) print("AppSync Client Initialized") } catch { print("Error initializing AppSync Client") } return true } }
2class AppDelegate: UIResponder, UIApplicationDelegate {
4 var appSyncClient: AWSAppSyncClient?
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 }
24 return true
25 }

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

func uploadImage() { let s3ObjectInput = S3ObjectInput( bucket: "yourBucket", region: "yourBucketRegion", key: "public/imageName.jpeg", localUri: "uriOfImageToUpload", mimeType: "jpeg" ) let pictureMutationInput = CreatePictureInput( id: UUID().uuidString, name: "yourInputName", owner: "yourName", visibility: Visibility(rawValue: "public"), file: s3ObjectInput ) appSyncClient.perform( mutation: CreatePictureMutation(input: pictureMutationInput) ) { (result, error) in if let error = error as? AWSAppSyncClientError { print("Error occurred: \(error.localizedDescription )") } if let resultError = result?.errors { print("Error saving the item on server: \(resultError)") return } } }
1func uploadImage() {
2 let s3ObjectInput = S3ObjectInput(
3 bucket: "yourBucket",
4 region: "yourBucketRegion",
5 key: "public/imageName.jpeg",
6 localUri: "uriOfImageToUpload",
7 mimeType: "jpeg"
8 )
10 let pictureMutationInput = CreatePictureInput(
11 id: UUID().uuidString,
12 name: "yourInputName",
13 owner: "yourName",
14 visibility: Visibility(rawValue: "public"),
15 file: s3ObjectInput
16 )
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 }

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.