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

Added a Variables test #3247

Merged
merged 1 commit into from
Jul 16, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ sealed class Optional<out V> {
fun getOrNull() = (this as? Present)?.value
fun getOrThrow() = getOrNull() ?: throw MissingValueException()

class Present<V>(val value: V) : Optional<V>()
data class Present<V>(val value: V) : Optional<V>()
object Absent : Optional<Nothing>()

fun <V : Any> presentIfNotNull(value: V?): Optional<V> = if (value == null) Absent else Present(value)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package test.variables

import com.apollographql.apollo3.api.Optional
import variables.NonNullVariableWithDefaultValueQuery
import variables.NullableVariableQuery
import variables.NullableVariableWithDefaultValueQuery
import variables.NullableVariableWithOptionalDirectiveQuery
import kotlin.test.Test
import kotlin.test.assertEquals

class VariablesTest {
@Test
fun nullableVariableDefaultsToNonOptional() {
/**
* By default, we remove the `Optional` so that users have to input a parameter
* It's not possible to omit a variable. But it is possible to send null
*/
assertEquals(42, NullableVariableQuery(42).param)
assertEquals(null, NullableVariableQuery(null).param)
}

@Test
fun nullableVariableWithOptionalDirectivesGeneratesOptional() {
/**
* If the user opted-in @optional, it's possible to omit the variable again
*/
assertEquals(Optional.Absent, NullableVariableWithOptionalDirectiveQuery().param)
assertEquals(Optional.Present(42), NullableVariableWithOptionalDirectiveQuery(Optional.Present(42)).param)
assertEquals(Optional.Present(null), NullableVariableWithOptionalDirectiveQuery(Optional.Present(null)).param)
}

@Test
fun nullableVariableWithDefaultValueGeneratesAbsentInKotlin() {
/**
* If the variable has a default value, it always get generated as Optional
*/
assertEquals(Optional.Absent, NullableVariableWithDefaultValueQuery().param)
assertEquals(Optional.Present(42), NullableVariableWithDefaultValueQuery(Optional.Present(42)).param)
// Because the variable is nullable, it is possible to send null
assertEquals(Optional.Present(null), NullableVariableWithDefaultValueQuery(Optional.Present(null)).param)
}

@Test
fun nonnullVariableWithDefaultValueGeneratesAbsentInKotlin() {
/**
* If the variable has a default value, it always gets generated as Optional
*/
assertEquals(Optional.Absent, NonNullVariableWithDefaultValueQuery().param)
assertEquals(Optional.Present(42), NonNullVariableWithDefaultValueQuery(Optional.Present(42)).param)
// Because the variable is nonnull, we cannot send null
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
query NullableVariable($param: Int) {
fieldWithNullableParameter(param: $param)
}

query NullableVariableWithOptionalDirective($param: Int @optional) {
fieldWithNullableParameter(param: $param)
}

query NonNullVariable($param: Int!) {
fieldWithNullableParameter(param: $param)
}

query NullableVariableWithDefaultValue($param: Int = 42) {
fieldWithNullableParameter(param: $param)
}

query NonNullVariableWithDefaultValue($param: Int! = 42) {
fieldWithNullableParameter(param: $param)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
type Query {
fieldWithNullableParameter(param: Int): Int
fieldWithNonNullParameter(param: Int!): Int
}