GraphQL Strawberry Shake client execution strategy

1k views Asked by At

I built a HotChocolate service and a strawberry client according to the documentation on the chillcream website. Here is my client code:

namespace strawberry_shake_graphql_client
{
    internal class Program
    {
        static IServiceCollection serviceCollection = new ServiceCollection();
        static IServiceProvider serviceProvider;
        static IStrawberryShakeGraphQLClient client;
        static async Task Main(string[] args)
        {
            serviceCollection
                .AddStrawberryShakeGraphQLClient(StrawberryShake.ExecutionStrategy.CacheAndNetwork)
                .ConfigureHttpClient(c => c.BaseAddress = new Uri("https://localhost:7148/graphql/"));
            serviceProvider = serviceCollection.BuildServiceProvider();
            client = serviceProvider.GetRequiredService<IStrawberryShakeGraphQLClient>();
            while(Console.ReadLine() != string.Empty)
            {
                await GetBooks();
            }
        }
        static async Task GetBooks()
        {
            IOperationResult<IGetbooksResult> result = await client.Getbooks.ExecuteAsync();
            if (result.IsErrorResult())
            {
                Console.WriteLine("ExecuteAsync() failed!");
                return;
            }
            foreach (var book in result.Data.BooksList)
            {
                Console.WriteLine(book.Title);
            }
        }
    }
}

My goal is to have my client consume data from the HotChocolate service if the network connection is online and from the local store/repository if the network connection is offline.

According to this https://chillicream.com/docs/strawberryshake/v13/caching I expect my client to continue working normally and provide data from the local cache/repository when the HotChocolate service is stopped or not reachable.

Instead of providing data from the local cache I get an HttpRequestExeption when my client executes the GetBooks query with an offline HotChocolate service.

Is the code example missing something? is my expectation wrong? Can someone provide my with sample code how to implement my client to work as expected above?

Best regards, Siraf

1

There are 1 answers

1
Björn F On

Your expectations are incorrect. As per the documentation you link to the CacheAndNetwork execution strategy will first load data from the local store, if there is any, to quickly render the page for the user. After that it will make a network request to the API to get "fresh" data to display, this happens every time, hence the network error you received.

To achieve what you're asking for you would have to implement some sort of fallback logic by first making a query using ExecutionStrategy.Network, if that fails, execute a query using ExecutionStrategy.CacheFirst. But even then if there's no data in the store matching the query it will attempt a network connection, which would fail.

Adding to that, the StrawberryShake Store, is only an in memory cache intended to reduce the number of network requests necessary (it's whiped clean every time the application is restarted) and as such an ill fit for what you're trying to archive, data available in an offline scenario.

I'd recommend you look into adding your own caching for offline use by storing the results of your querys in IndexDB or LocalStorage, and implement a fallback logic in your app to look there if there's a network error when making the request.