Class Extension

Our goal for the library was to maintain end to end type safety, which is a challenge due to the dynamic nature of gRPC payloads that are strictly typed based on protocol buffer definitions and are generated by codegens like protoc.

In typical RESTful services, payloads can be quite flexible, but gRPC enforces a rigid contract for message types. The complication arises because developers can generate any variety of service client stubs using code generators, and these stubs come with their own unique type definitions. Our library needed to interface seamlessly with these varied types without prior knowledge of them.

We addressed this complexity by designing a constructor function that accepts any service client generated by the codegen. This function then extends the service client, preserving its type definitions. This approach has a significant advantage: developers can use our enhanced client as a drop-in replacement for the original client, with no changes to their existing codebase or the way they invoke methods.

To integrate the caching functionality without altering the developer's interaction with the client, we implemented method call interception. By overriding the service client's original methods, we inject a caching layer transparently. This means that whenever a method is invoked, our library checks the cache first, and if a valid cached response is available, it returns that response instead of making a network call.

Type safety and autocompletion

Given the protobuf file below

example_proto_client

The library provides autocompletion

example_autocomplete

And type safety

example_type_safety