Skip to content

Commit 45d1981

Browse files
author
Dariusz Kuc
authored
[docs] update information about optional arguments and default values (ExpediaGroup#1367)
* [docs] update information about optional arguments and default values * fix ktlint
1 parent 7627dd6 commit 45d1981

File tree

2 files changed

+41
-3
lines changed

2 files changed

+41
-3
lines changed

generator/graphql-kotlin-schema-generator/src/test/kotlin/com/expediagroup/graphql/generator/execution/FunctionDataFetcherTest.kt

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,15 @@ class FunctionDataFetcherTest {
7676
is OptionalInput.Undefined -> "input was UNDEFINED"
7777
is OptionalInput.Defined -> "first input was ${input.value?.first()?.field1}"
7878
}
79+
80+
fun optionalWrapperClass(input: InputWrapper) = when (input.optional) {
81+
is OptionalInput.Undefined -> "optional was UNDEFINED"
82+
is OptionalInput.Defined -> "optional was ${input.optional.value}"
83+
}
7984
}
8085

86+
data class InputWrapper(val required: String, val optional: OptionalInput<String>)
87+
8188
@GraphQLName("MyInputClassRenamed")
8289
data class MyInputClass(
8390
@JsonProperty("jacksonField")
@@ -149,7 +156,7 @@ class FunctionDataFetcherTest {
149156
}
150157

151158
@Test
152-
fun `default values are overriden when arument is passed in`() {
159+
fun `default values are overridden when arument is passed in`() {
153160
val dataFetcher = FunctionDataFetcher(target = null, fn = MyClass::printDefault)
154161
val mockEnvironment: DataFetchingEnvironment = mockk {
155162
every { getSource<Any>() } returns MyClass()
@@ -276,6 +283,16 @@ class FunctionDataFetcherTest {
276283
assertEquals(expected = "input was hello", actual = dataFetcher.get(mockEnvironment))
277284
}
278285

286+
@Test
287+
fun `optional inputs return the value when null arguments are passed`() {
288+
val dataFetcher = FunctionDataFetcher(target = MyClass(), fn = MyClass::optionalWrapper)
289+
val mockEnvironment: DataFetchingEnvironment = mockk {
290+
every { arguments } returns mapOf("input" to null)
291+
every { containsArgument("input") } returns true
292+
}
293+
assertEquals(expected = "input was null", actual = dataFetcher.get(mockEnvironment))
294+
}
295+
279296
@Test
280297
fun `optional inputs return undefined when arguments are empty`() {
281298
val dataFetcher = FunctionDataFetcher(target = MyClass(), fn = MyClass::optionalWrapper)
@@ -296,4 +313,24 @@ class FunctionDataFetcherTest {
296313
val result = dataFetcher.get(mockEnvironment)
297314
assertEquals(expected = "first input was foo", actual = result)
298315
}
316+
317+
@Test
318+
fun `optional inputs inside class return the value when arguments are passed`() {
319+
val dataFetcher = FunctionDataFetcher(target = MyClass(), fn = MyClass::optionalWrapperClass)
320+
val mockEnvironment: DataFetchingEnvironment = mockk {
321+
every { arguments } returns mapOf("input" to mapOf("required" to "hello", "optional" to "hello"))
322+
every { containsArgument("input") } returns true
323+
}
324+
assertEquals(expected = "optional was hello", actual = dataFetcher.get(mockEnvironment))
325+
}
326+
327+
@Test
328+
fun `optional inputs inside class return undefined when arguments are empty`() {
329+
val dataFetcher = FunctionDataFetcher(target = MyClass(), fn = MyClass::optionalWrapperClass)
330+
val mockEnvironment: DataFetchingEnvironment = mockk {
331+
every { arguments } returns mapOf("input" to mapOf("required" to "hello"))
332+
every { containsArgument("input") } returns true
333+
}
334+
assertEquals(expected = "optional was UNDEFINED", actual = dataFetcher.get(mockEnvironment))
335+
}
299336
}

website/docs/schema-generator/execution/optional-undefined-arguments.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,11 @@ value and explicit `null` value.
1616

1717
`OptionalInput` is a convenient sealed class wrapper that provides distinction between undefined, null, and non-null
1818
values. If the argument is not specified in the request it will be represented as a `OptionalInput.Undefined` object, otherwise the
19-
value will be wrapped in `OptionalInput.Defined` class.
19+
value will be wrapped in `OptionalInput.Defined` class. As a best practice, we highly recommend to set appropriate
20+
[default values to all optional arguments](https://opensource.expediagroup.com/graphql-kotlin/docs/schema-generator/writing-schemas/arguments#default-values).
2021

2122
```kotlin
22-
fun optionalInput(input: OptionalInput<String>): String = when (input) {
23+
fun optionalInput(input: OptionalInput<String> = OptionalInput.Undefined): String = when (input) {
2324
is OptionalInput.Undefined -> "input was not specified"
2425
is OptionalInput.Defined<String> -> "input value: ${input.value}"
2526
}

0 commit comments

Comments
 (0)