This is an example of how a Bytebuddy agent can be used to proxy input object classes, so that it keeps track of fields being explicitly set.
In the InputProxyApplication
main
method a Bytebuddy agent is registered that does the following to the SearchInput
class:
- Make it implement the
FieldSetTracker
interface which has apublic boolean isSet(String field)
method. - Add a field
fieldsSet
of typeHashMap
. - Add a
public boolean isSet(String field)
method that checks thefieldsSet
map. This is the implementation of theFieldSetTracker
interface. - Proxy all
set*
methods, so that it tracks fields being set in thefieldsSet
map.
In a data fetcher you can cast an input object to the FieldSetTracker
interface, and check if a field was explicitly set.
@DgsQuery
public String search(SearchInput searchInput) {
var titleIsSet = ((FieldSetTracker) searchInput).isSet("title");
var scoreIsSet = ((FieldSetTracker) searchInput).isSet("score");
return "Title is set " + titleIsSet + " and score is set " + scoreIsSet;
}
On the latest JDK builds you need to run with -XX:+EnableDynamicAgentLoading
to allow dynamic agent loading.