Skip to content

Conversation

smyrick
Copy link
Contributor

@smyrick smyrick commented Dec 27, 2020

📝 Description

Add support for Kotlin default values. By calling functions with the callBy methods instead we can pass a map of KParameters. Then if one of the params is optional and it is not sent a value from GraphQL input we can exclude that parameter from the map, which mean the default value will be used.

Kotlin

fun print(message: String? = "hello"): String? = message

GraphQL

query PrintMessages {
    first: print(message = "foo") # foo
    second: print(message = null) # null
    third: print # hello
}

This info will not be conveyed in the schema, but it simplifies the logic needed for optional arguments. If you still need to have logic for the default values I recommend using the OptionalInput wrapper, since you will still not be able to determine the difference when the client didn't pass a value or it passed in the same value as the default.

This is a breaking change because we are changing the function signature that is open for FunctionDataFetcher

🔗 Related Issues

Fixes #53

@smyrick smyrick added changes: major Changes require a major version module: generator Issue affects the schema generator and federation code labels Dec 27, 2020
@smyrick
Copy link
Contributor Author

smyrick commented Dec 27, 2020

TODO - update docs

Updated in 173b331

@smyrick smyrick added this to the 4.0.0 milestone Dec 27, 2020
@smyrick smyrick marked this pull request as ready for review December 27, 2020 22:40
@smyrick smyrick added the type: enhancement New feature or request label Dec 27, 2020
@dariuszkuc dariuszkuc merged commit a56f390 into ExpediaGroup:master Dec 31, 2020
@smyrick smyrick deleted the default-kotlin-parameters branch December 31, 2020 09:46
@rowi1de
Copy link

rowi1de commented Mar 20, 2021

@smyrick
Copy link
Contributor Author

smyrick commented Mar 20, 2021

I could make this change to the code in FunctionDataFetcher.getParameters to just do a map instead of mapNotNull then pass in null when there is no argument specified.

But then this unit test no longer works and neither will default values.
https://github.com/smyrick/graphql-kotlin/blob/c5b8fdc3739930f712a114b9abc15dee8fc[…]diagroup/graphql/generator/execution/FunctionDataFetcherTest.kt

Since we can’t inspect if a KParameter has a default value, we can only support one use case or the other.

  • Support Kotlin default values but require them for nullable input
  • Do not support Kotlin default values and always pass null for input if argument is missing in request

huehnerlady pushed a commit to huehnerlady/graphql-kotlin that referenced this pull request Apr 27, 2021
* Support default Kotlin values

* Update docs with new supported features

Co-authored-by: Shane Myrick <[email protected]>
dariuszkuc pushed a commit to dariuszkuc/graphql-kotlin that referenced this pull request Aug 5, 2022
* Support default Kotlin values

* Update docs with new supported features

Co-authored-by: Shane Myrick <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

changes: major Changes require a major version module: generator Issue affects the schema generator and federation code type: enhancement New feature or request

Development

Successfully merging this pull request may close these issues.

Support for default schema values

3 participants