No way to get nextToken when using custom GraphQL query?[Amplify]

No way to get nextToken when using custom GraphQL query?[Amplify]


0

 //MovieList function Defination
 private fun getMovieList(): GraphQLRequest<MovieDBFinal2> {
        val document = ("query getMovieList { "
                + "listMovieDBFinal2s(limit: 1000, filter: {Genre1: {eq: "Animation"}}) { "
                + "nextToken "
                + "items { "
                + "IMDB_title "
                + "} "
                + "} "
                + "} ")

        return SimpleGraphQLRequest(
            document,
            Collections.emptyMap(),
            TypeMaker.getParameterizedType(QueryResponse::class.java, MovieDBFinal2::class.java),
            GsonVariablesSerializer())
    }

    internal class QueryResponse<T> {
        private val listMovieDBFinal2s: QueryList<T>? = null

        val moviesList: QueryList<T>?
            get() = listMovieDBFinal2s
    }

    internal class QueryList<T> {
        val items: Iterable<T>? = null
        val nextToken: String? = null
    }
    
    
 //MovieList function call
        Amplify.API.query(
            getMovieList(),
            { response ->
                if (response.hasData()) {
                    Log.e("MovieList", "$response")
                }

            },
            { failure ->
                Log.e("MovieList", "Query failed.", failure)
            }
        )

I tried this type in my schema not working.Github issue
PaginationResult not working.Iterable also not giving any token. Only solution is String.class but that is difficult to serialize. The workaround is make the same request two times once with PaginationResult passing the nextToken as input and second make the same request with string.Class type.

1 Answer
1


0

There is a way to get the response serialized to the appropriate class as well as extract the nextToken from the response. Please have a look at this link

Reference:

This deserialization logic only works when the original request is an AppSyncGraphQLRequest, so it won’t work for a SimpleGraphQLRequest. So, using PaginatedResult is probably not going to work for this use case.

Instead, I’d suggest creating a similar class, and using that as the deserialization type instead. Something like this:

class ResponseData<T> {
      private QueryList<T> propertiesByPrice;

      public QueryList<T> getPropertiesByPrice() { return propertiesByPrice; }
} 

class QueryList<T> {
     private Iterable<T> items;
     private String nextToken;

     public Iterable<T> getItems() { return items; }
     public String getNextToken() { return nextToken; }
}

Then, specify the responseType as:
TypeMaker.getParameterizedType(QueryList::class.java, Property::class.java)



Leave a Reply

Your email address will not be published. Required fields are marked *