Summary: I can't get a total number of records from my GraphQL endpoint. I only know if I have reached the end of my GraphQL records list when I am parsing the response from the endpoint. How can I make my custom pagination component aware that it's on the last page?
Details: I'm using React Admin with AWS AppSync (GraphQL on DynamoDB) using ra-data-graphql
. AppSync can't tell you the total number of records available to a list query, and it also limits the number of records you can return to a 1MB payload. Instead, it includes a nextToken
value if there are more records to query, which you can include in subsequent list queries.
I have created a custom pagination component that only uses "prev" and "next" links, which is fine. But I need to know when the last page is displayed. Right now, I only know this in the parseResponse()
function that I'm passing in to buildQuery()
for the list query. At this point, I have access to the nextToken
value. If it's empty, then I have fetched the last page of results from AppSync. If I could pass this value, or even a boolean e.g. lastPage
to the custom pagination component, I'd be all set. How can I do this in React Admin?
In DynamoDB you can query total count with a (second) Scan query.
Given that you use similar Schema:
You can create resolver for
Query._allPostsMeta
with these VTL templates:Request VTL Template:
Resolve VTL Template:
React-admin 'GET_LIST' query:
This approach is used in ra-data-graphql-simple
There is also a way to adapt AppSync resolver to work with
page
andperPage
native react-admin parameters.It's a bad practice because query response is limited by 1MB and also full dynamodb query response needs to be parsed and transformed for each page query, however it does the trick.
VTL AppSync resolver Request Mapping Template:
VTL AppSync resolver Response Mapping Template:
dataProvider.js
To achieve this I created a custom reducer,
nextTokenReducer
that looks for React Admin'sCRUD_GET_LIST_SUCCESS
action, the payload of which is the entire response from the AppSync GraphQL endpoint. I can pull thenextToken
value out of that:I passed this custom reducer to the
Admin
component in my mainApp
component:I then connected the
nextToken
store to my custom pagination component. It will display "next", "prev", or nothing based on whethernextToken
is in its props:Finally, I passed the custom pagination component into my list component: