I am using GraphQL to synchronize data in aws dynamoDB using AppSync.
but now I need to store the data that I got from the GraphQL API
I found that Apollo client can be used to store these data in the cache to be able to use the data in offline mode.
here is the code I used using GraphQL:
amazonAppSyncClient = new AmazonAppSyncClient ("ACCESS_KEY_ID", "SECRET_ACCESS_KEY", REGION);
graphQLClient = new GraphQLClient ("https://6vl6q5h2c5bxrbmlimiia5hldy.appsync-api.us-west-2.amazonaws.com/graphql",
new GraphQLClientOptions {
HttpMessageHandler = new AWS4SignerMessageHandler (
amazonAppSyncClient,
"ACCESS_KEY_ID",
"SECRET_ACCESS_KEY")
});
public async Task<List<Book>> GetAllBooks () {
var graphQLResponse = await graphQLClient.PostQueryAsync (@"query getBooks {
listBooks {
items {
Id
Title
ISBN
Price
PageCount
Author}}}");
var json = graphQLResponse.Data;
var value = json.GetValue ("listBooks").GetValue ("items");
List<Book> books = value.ToObject<List<Book>> ();
return books;
}
I used this code from here: https://gist.github.com/NeilBostrom/cab8b9275e39bb90ecf8e06ab980664b
How can I save the returned data locally using apollo?
If you want to achieve proper offline caching on mobile platforms using AppSync/Apollo with Xamarin, you'll have to do quite a bit of work. Firstly, Apollo is the original caching GraphQL client for Android and iOS and the Amazon AppSync SDK libraries are based on those. They just build some Amazon specific functionality on top.
The problem is that even though libraries exist for both iOS and Android, they cannot be used as is with Xamarin, a separate binding project needs to be created so that you can invoke the libraries in you Xamarin specific C# code.
Here's what you need to do:
Edit: Other options that you could consider if above looks like too much work:
Try to create a binding for Apollo iOS and Apollo Android and try to achieve the caching that way. Anyway, the AppSync SDK uses them internally (as seen in the picture below) so they should be suitable for the task. Source
If you're not tied to the Amazon platform, there are several other Graph QL databases and providers that might have a better support for Xamarin.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.