Closed ksgangadharan closed 3 years ago
HI @ksgangadharan,
When using a custom GraphQL query, I'd suggest using a custom response type as well, instead of trying to make PaginatedResult
work for your use case.
First, create some classes for the response type you are expecting:
class QueryList<T> {
private Iterable<T> items;
private String nextToken;
public Iterable<T> getItems() { return items; }
public String getNextToken() { return nextToken; }
}
class QueryResponse<T> {
private QueryList<T> listTodos;
public QueryList<T> getTodos() { return listTodos; }
}
Then, build your request like this, and specify the response type as QueryResponse<Todo>
:
private GraphQLRequest<Todo> getListTodosRequest() {
String document = "query listTodos { "
+ "listTodos { "
+ "items {
+ "id "
+ "name "
+ "}"
+ "}"
+ "}";
return new SimpleGraphQLRequest<>(
document,
Collections.emptyMap(),
TypeMaker.getParameterizedType(QueryResponse.class, Todo.class)
new GsonVariablesSerializer());
}
Finally, you can access the list of Todo objects via:
response.getTodos().getItems();
and the nextToken
via:
`response.getTodos().getNextToken()`
Thanks @richardmcclellan, that worked
I wish the classes were abstracted at a higher level instead of having dependencies on PaginatedResult and AppSyncGraphQLRequest. The above solution is a roundabout way and will need a custom response class for each new custom query. But yes it works.
Also anyone using this solution, be aware to update the pro guard files (i.e. if you using pro guarding).
Before opening, please confirm:
Language and Async Model
Kotlin
Amplify Categories
GraphQL API
Gradle script dependencies
Environment information
Please include any relevant guides or documentation you're referencing
No response
Describe the bug
We have to invoke a custom list graphQL query for fetching records based on certain implementation specific criteria. This included sorting of the records and use of certain custom parameters that are not supported by AppSyncGraphQLRequest.
As a solution, we wrote our own graphQL query by extending the GraphQLRequest class. The query runs fine and we are able to get the results. However the pagination does not work. The reason for the pagination not working is the following piece of code in GsonGraphQLResponseFactory.java
The code here assumes that the GraphQL request is a type of AppSyncGraphQLRequest. This will fail for the custom query. We are unable to get a workaround for this issue as: 1) The AppSyncGraphQLRequest is final, so we cannot create a custom extension 2) The nextToken string is not available in the GraphQL Response for us to handle this independently
The issue can be resolved in 2 possible ways (without causing any extensive changes)
Solution 1: Add a function in GraphQLRequest class for handling pagination
And modify the GsonGraphQLResponseFactory.java pagination function as follows:
Solution 2: Make the nextToken available in the GraphQLResponse. This will enable the implementation to handle it as required
Reproduction steps (if applicable)
No response
Code Snippet
No response
Log output
No response
amplifyconfiguration.json
No response
GraphQL Schema
No response
Additional information and screenshots
No response