awslabs / aws-mobile-appsync-sdk-android

Android SDK for AWS AppSync.
https://docs.amplify.aws/sdk/api/graphql/q/platform/android/
Apache License 2.0
105 stars 58 forks source link

How to get nextToken from GRAPHQL callback, while using query() from appsyncclient? #324

Closed pradheepkaliraj closed 3 years ago

pradheepkaliraj commented 3 years ago

State your question I am writing an app that queries data from Dynamodb. The query limit is set to 10, and I have a filter to search for the name "tom". I want to get the next 10 items, hence I am trying to use nextToken in the query. However, I don't know how to get the nextToken, from the previous query. I would greatly appreciate any help here

Provide code snippets (if applicable)

 ModelRecipeFilterInput.Builder ownerRecipeFilterInput 
                =ModelRecipeFilterInput.builder().owner(ModelStringInput.builder().contains("tom").build());

   Log.i(TAG, "nextToken --->"+nextToken);
 ClientFactory.appSyncClient().query(ListRecipesQuery.builder()
            .filter(ownerRecipeFilterInput.build())
            .nextToken(nextToken)
            .limit(10)            .build())
            .enqueue(queryVariableCallback)  //error 
            .responseFetcher(AppSyncResponseFetchers.CACHE_AND_NETWORK)
            .enqueue(queryCallback); 

image

Environment(please complete the following information): appsync:2.6.+

Device Information (please complete the following information):

richardmcclellan commented 3 years ago

Hi @pradheepkaliraj,

You can access the nextToken with your GraphQLCall.Callback<ListRecipesQuery.Data>. There is no need to try and create a GraphQLCall.Callback<ListRecipesQuery.Variables>

Try this:

private GrapQLCall.Callback<ListRecipesQuery.Data> queryCallback = new GraphQLCall.Callback<ListRecipesQuery.Data>() {
    @Override
    public void onResponse(@Nonnull Response<ListRecipesQuery.Data> response) {
        Log.d(TAG, "nextToken: " + response.data().listRecipes().nextToken());
    }

    @Override
    public void onFailure(@Nonnull ApolloException e) {
        Log.e(TAG, e.toString(), e);
    }
});
pradheepkaliraj commented 3 years ago

Thanks @richardmcclellan. I am able to get the nextToken now.