I am currently writing a Java coprocessor for the enterprise version of Apollo Router. I need it to propagate values from the RouterRequest stage, all the way to the RouterResponse stage.
I had initially written it such as the RouterRequest stage was storing some information inside the context entries of the request object. Here the classes are the following:
- Request: custom POJO (following https://www.apollographql.com/docs/router/customizations/coprocessor/#routerrequest model)
- Context: Map<String, Object>
- entries: LinkedHashMap<String, Object> automatically inserted by the router in the Context Map
It worked, but was not ideal; the weakly typed LinkedHashMap of the context.get(“entries) forces me to do all kinds of type casting to retrieve them later.
I then built a DTO, which contains all the field I need for my RouterResponse stage, to replace all the keys I used to put in the context.entries Map.
But I find no way to properly propagate this DTO in the context.entries between the stages. It is always serialized to a LinkedHashMap<String, Object>, which again forces me to deserialize every time I need it.
I gather from the documentation that only the field “entries” of the context is propagated. I also stumbled upon this PR: https://github.com/apollographql/router/pull/4474 which seems to indicate that a field “extensions”, which would maybe support my DTO without changing its type, is also propagated. Though, my tests show that the field context.extensions is not propagated across stages.
This problem is not too important because I can always deserialize my object from the context.entries, but this is a performance issue for me.
So my question would be: am I doing something wrong, or are we not supposed to try and propagate entire Java POJO in the context? Is there a better way to achieve POJO propagation across stages?
Apollo router version: v1.50.0
Ludovic is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.