-
Notifications
You must be signed in to change notification settings - Fork 353
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
1965: make FunctionDataFetcher
handle blocking scopes resulting in a thrown Error
as expected
#1966
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -36,8 +36,8 @@ import kotlin.reflect.full.valueParameters | |
/** | ||
* Simple DataFetcher that invokes target function on the given object. | ||
* | ||
* @param target The target object that performs the data fetching, if not specified then this data fetcher will attempt | ||
* to use source object from the environment | ||
* @param target The target object that performs the data fetching, if not specified, then this data fetcher will attempt | ||
* to use a source object from the environment | ||
* @param fn The Kotlin function being invoked | ||
*/ | ||
@Suppress("Detekt.SpreadOperator") | ||
|
@@ -49,7 +49,7 @@ open class FunctionDataFetcher( | |
/** | ||
* Invoke a suspend function or blocking function, passing in the [target] if not null or default to using the source from the environment. | ||
*/ | ||
override fun get(environment: DataFetchingEnvironment): Any? { | ||
override fun get(environment: DataFetchingEnvironment): CompletableFuture<Any?> { | ||
val instance: Any? = target ?: environment.getSource<Any?>() | ||
val instanceParameter = fn.instanceParameter | ||
|
||
|
@@ -63,7 +63,7 @@ open class FunctionDataFetcher( | |
runBlockingFunction(parameterValues) | ||
} | ||
} else { | ||
null | ||
CompletableFuture.completedFuture(null) | ||
} | ||
} | ||
|
||
|
@@ -82,10 +82,10 @@ open class FunctionDataFetcher( | |
* Retrieves the provided parameter value in the operation input to pass to the function to execute. | ||
* | ||
* If the argument is missing in the input, and the type is not an [OptionalInput], do not return a mapping. | ||
* This allows for default Kotlin values to be used when executing the function. If you need logic when a value | ||
* This allows for default Kotlin values to be used when executing the function. Otherwise, if you need logic when a value | ||
* is missing, use the [OptionalInput] wrapper instead. | ||
* | ||
* If the parameter is of a special type then we do not read the input and instead just pass on that value. | ||
* If the parameter is of a special type, then we do not read the input and instead just pass on that value. | ||
* The special values include: | ||
* - The entire environment is returned if the parameter is of type [DataFetchingEnvironment] | ||
*/ | ||
|
@@ -120,12 +120,15 @@ open class FunctionDataFetcher( | |
} | ||
|
||
/** | ||
* Once all parameters values are properly converted, this function will be called to run a simple blocking function. | ||
* If you need to override the exception handling you can override this method. | ||
* Once all parameter values are properly converted, this function will be called to run a simple blocking function. | ||
* If you need to override the exception handling, you can override this method. | ||
*/ | ||
protected open fun runBlockingFunction(parameterValues: Map<KParameter, Any?>): Any? = try { | ||
fn.callBy(parameterValues) | ||
} catch (exception: InvocationTargetException) { | ||
throw exception.cause ?: exception | ||
} | ||
protected open fun runBlockingFunction(parameterValues: Map<KParameter, Any?>): CompletableFuture<Any?> = | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This will cause an allocation of an object of type There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I made the Shall I do that? |
||
CompletableFuture<Any?>().apply { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same as above, this change is going to cause incompatibility with the update to graphql-java 22 |
||
try { | ||
complete(fn.callBy(parameterValues)) | ||
} catch (exception: InvocationTargetException) { | ||
completeExceptionally(exception.cause ?: exception) | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i believe that this change is going to cause incompatiblity with the update to graphql-java 22
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you help me understand more specifically what you mean so that maybe I can try to adjust my solution accordingly?
Otherwise, I'd say I'm happy to remove the change to this
get
function signature as it's not required for my solution to work. Would that be an okay compromise?*I mainly made the change as it made me have to make less changes to the Test class; where without this change I'll have to cast to
CompletableFuturw
repeatedly. This felt like an efficiency and type-safety boon.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
TLDR; graphql-java 22 introduces polymorphic resolution, in previous versions, even if your data fetcher was returning a materialized object (not a CF), it was anyways creating a CF causing a terrible memory bottleneck where GC pause times were paying the price.
having said that, i wonder, why would you want to catch
Error
s ?screenshot taken from
Error
docsThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
😅 I need to read up on this, as tbh that is pretty far out of my depth of current expertise lol. So I'm inclined to defer to your judgement and undo this part of my change regardless of where the larger change lands as accepted or not.
In regards to this, please see my thoughts/discussion here #1966 (comment) if you would.