0

My question is similar to the one here. I created Sign Up/Sign In using AWSMobileClient and this part worked fine. However, when I tried to query AppSync using Cognito user pool identity I could not get any data returned. I assigned the correct policies to the authenticated IAM role for the identity pool. I think the issue might be I need to implement some code snippets such as the one below:

let credentialsProvider = AWSCognitoCredentialsProvider(regionType:.USWest2,
   identityPoolId:"us-west-2:d2545277-8214-4781-b516-2eb72d1bceba")

let configuration = AWSServiceConfiguration(region:.USWest2, credentialsProvider:credentialsProvider)

AWSServiceManager.defaultServiceManager().defaultServiceConfiguration = configuration

However, I don't think these three lines are the full code and I can't find any documentation on this. If someone knows, can you please post some doc/sample code? Thanks.

Sarah Guo
  • 301
  • 1
  • 3
  • 15

1 Answers1

0

You can use the new AWSMobileClient to automate the Auth routines and then pass that to AppSync client constructor per the docs:

let appSyncConfig = try AWSAppSyncClientConfiguration(appSyncClientInfo: AWSAppSyncClientInfo(), 
credentialsProvider: AWSMobileClient.sharedInstance(), databaseURL: databaseURL)

appSyncClient = try AWSAppSyncClient(appSyncConfig: appSyncConfig)

The Cognito Roles will need appropriate IAM policy for AppSync outlined here.

Richard
  • 1,750
  • 11
  • 11
  • Hi Richard, thanks for answering. Actually I discovered that authentication might not be the issue with the code. I am able to call AppSync queries without getting any errors. But the data returned is always nil. Do you have any idea why this is the case? Thanks. – Sarah Guo Dec 10 '18 at 19:11
  • @SarahGuo if that's the case then it's possible the query that you are running isn't fulfilling the selection set. I would first try running the query in the console to see if it's valid and returns the expected object or list. If so then start troubleshooting the codegen and queries at the client. – Richard Dec 10 '18 at 22:28
  • can you elaborate more on not fulfilling the selection set? What are the possible causes for this issue? I checked my swift AppSync queries, and also the files generated by codegen, and they are both correct. I can't think of another reason why the query could return nil. Thanks! – Sarah Guo Dec 10 '18 at 23:31
  • It's possible that the mutation triggering your subscription might not have the fields defined that you are specifying in your subscription, which is how the data in a subscription notification in AppSync is populated. For instance if you have `subscription newUsers { id fname lname}` which is triggered by a mutation called `addUser(fname: String lname:String)` then you must specify id, fname, and lname in the mutation like so: `mutation addUser(fname:'Sara' lname:'Guo'){ id fname lname}` - e.g. my selection set is { id fname lname} here so they get sent to the subscriber. – Richard Dec 12 '18 at 00:59
  • More info from the documentation: https://docs.aws.amazon.com/appsync/latest/devguide/real-time-data.html – Richard Dec 12 '18 at 00:59
  • Hi Richard, I removed subscriptions completely from my schema but the same error persists. I also removed everything from my schema except for that one query, but it still returned nil data. But when I call the query from the console, it returns correct data. Any idea why? Thanks! – Sarah Guo Dec 12 '18 at 18:07