-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Instrumentation methods should have more flexible generic return type #3609
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Well technically some methods return a specific type
but I am assuming you mean the "field fetching" methods like
The reason these are like this is because that is what the engine has at the point it makes the "callback" InstrumentationContext<Object> fetchCtx = nonNullCtx(instrumentation.beginFieldFetch(instrumentationFieldFetchParams,
executionContext.getInstrumentationState())
);
Object fetchedObject = invokeDataFetcher(executionContext, parameters, fieldDef, dataFetchingEnvironment, dataFetcher);
fetchCtx.onDispatched();
.handle((result, exception) -> {
fetchCtx.onCompleted(result, exception); Sometimes the object is a Can you please expand more on use cases where you as the SPI implementor will know what more specific type the "engine returned value" will be We can make it |
I was helping update some existing instrumentation that was using The code was using the @Override
public @Nullable InstrumentationContext<Object> beginFieldFetch(
InstrumentationFieldFetchParameters p, InstrumentationState s) {
//Doesn't compile, needs to be explitily returning Object, not ExecutionResult.
return whenCompleted(
(ExecutionResult r, Throwable t) -> {
if (s instanceof State state) {
state.increment(coordinates(p), result(r, t));
}
});
} This might be completely incorrect, so maybe I'm just looking the wrong direction. return whenCompleted(
(Object r, Throwable t) -> {
if (s instanceof State state && r instanceof ExecutionResult r1) {
state.increment(coordinates(p), result(r1, t));
}
}); |
in v22.1 - I think the modern Java |
Ok, experimenting with public @Nullable FieldFetchingInstrumentationContext beginFieldFetching(InstrumentationFieldFetchParameters parameters, InstrumentationState state) {
return new FieldFetchingInstrumentationContext() {
@Override
public void onDispatched() {
}
@Override
public void onCompleted(Object result, Throwable t) {
var parent = GraphQLTypeUtil.unwrapAll(parameters.getExecutionStepInfo().getParent().getType()).getName();
var field = String.format("%s.%s", parent, parameters.getField().getName());
if(t != null) {
System.out.printf("%s %s\n", field, Result.EXCEPTION);
} else if(result == null) {
System.out.printf("%s %s\n", field, Result.NULL);
} else {
System.out.printf("%s %s\n", field, Result.OK);
}
}
};
} What doesn't work is using public @Nullable FieldFetchingInstrumentationContext beginFieldFetching(InstrumentationFieldFetchParameters parameters, InstrumentationState state) {
return SimpleInstrumentationContext.whenCompleted((Object result, Throwable t) -> { In that case
I think I was just trying the wrong thing. Possibly the javadoc can be a bit more clear on the intended use, it's not obvious how to go from the old APIs to the new one. |
Fair point on the better Java doc - we could do better there on saying to use the other one - I will fix that up - the in v22.0 we improved this so the fetched value could be either a plain old java object unwrapped or a CF if thats what came back. However you could no longer know if the value was a CF or not. This broke libs like Expedia's smart data loader. So via #3571 we introduced a new |
Following the discussion and @bbakerman's javadoc PR I'll close out this issue, but feel free to open a new one if you have new questions. |
Describe the bug
Methods in
Instrumentation
currently returnInstrumentationContext<Object>
. This won't allow to return a more concrete object type. E.g. returningInstrumentationContext<String>
does not compile.I think these methods should return
InstrumentationContext<?>
.To demonstrate this further, take this (plain Java) example:
This won't compile, String is an incompatible return type here.
However, if you change from
object
to?
, it's fine.The text was updated successfully, but these errors were encountered: