简体   繁体   中英

How to store data in Apollo cache using GraphQL in Xamarin?

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:

  1. Create a Xamarin.iOS Binding Project to create a C# binding of the AppSync iOS SDK and follow this article on how to get it to work. There's also quite detailed information on Microsoft Docs but it's aimed for Objective-C code so there are certain differences you need to be aware of.
  2. Create aa Xamarin.Android Java Bindings Library to create a C# binding of the AppSync Android SDK . Follow this article on Microsoft Docs for guidance on how to do it.
  3. Now you're ready to use the relevant libraries on Xamarin.iOS and Xamarin.Android.If I'm not mistaken, AppSync libraries automatically cache data on the disk so you don't even need to do anything on your side. Anyway, at this point I could write a three page article on how to achieve what but if you get to this point, it's best to follow the guidance and tutorials on the AppSync iOS and Android repositories I linked to above.

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.

  • GraphQL responses are pure JSON. You could just try to build a caching mechanism for it yourself if you don't need complex delta handling etc.

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM