Skip to content
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

Support default parameters for DgsData methods #1403

Merged
merged 2 commits into from Feb 16, 2023

Conversation

kilink
Copy link
Member

@kilink kilink commented Jan 29, 2023

Add support for Kotlin default parameter values for DgsData methods.

This is a follow up to PR #1302, where we added support for Kotlin default parameters when mapping input arguments; with this PR we now also support default parameters for the DgsData methods themselves.

Example:

@DgsComponent
class DataFetcher {
    @DgsData(parentType = "Query", field = "hello")
    fun fetcher(@InputArgument input: List<String> = emptyList()): String {
        // ...
    }
}

If the schema does not require input and the query did not supply it, previously this would fail, even though the fetcher method is written in a way to handle the missing input. This PR updates the DataFetcherInvoker to call Kotlin methods reflectively using callBy which allows supporting default parameters by omitting values in the passed in Map when the parameter is optional.

Add support for Kotlin default parameter values for DgsData methods.
val parameters = kFunc.parameters
val argsByName = CollectionUtils.newLinkedHashMap<KParameter, Any?>(parameters.size)

val paramSeq = if (parameters[0].kind == KParameter.Kind.INSTANCE) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you explain what lines #82 and #83 are doing?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We are checking if the KFunction takes an instance parameter, and supplying it if so; that is, the target object we want to call the method on, in this case the DGS component. That said, I can't really think if a scenario where we won't have an instance parameter, but we are kind of forced to deal with the possibility.

Copy link
Contributor

@srinivasankavitha srinivasankavitha left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR! Looks good to me. fyi - we now have 2 branches we merge PRs to, the master is on Spring Boot 3.0 and we have a spring-boot-2.7 branch as well.

@kilink kilink merged commit b1e395b into master Feb 16, 2023
@kilink kilink deleted the dgs-data-method-default-parameter-support branch January 25, 2024 04:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants