Skip to content

fix: use enum value in GraphQLEnumValueDefinition #2112

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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 @@ -56,7 +56,7 @@ private fun getEnumValueDefinition(generator: SchemaGenerator, enum: Enum<*>, kC
validateGraphQLEnumValue(name, kClass)

valueBuilder.name(name)
valueBuilder.value(name)
valueBuilder.value(enum)

generateEnumValueDirectives(generator, valueField, kClass.getSimpleName()).forEach {
valueBuilder.withAppliedDirective(it)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,14 @@ class GenerateEnumTest : TypeTestHelper() {
val actual = generateEnum(generator, MyTestEnum::class)
assertEquals(expected = 4, actual = actual.values.size)
assertEquals(expected = "MyTestEnum", actual = actual.name)
assertEquals(expected = "ONE", actual = actual.values[0].value)
assertEquals(expected = "TWO", actual = actual.values[1].value)
assertEquals(expected = "THREE", actual = actual.values[2].value)
assertEquals(expected = "customFour", actual = actual.values[3].value)
assertEquals(expected = "ONE", actual = actual.values[0].name)
assertEquals(expected = MyTestEnum.ONE, actual = actual.values[0].value)
assertEquals(expected = "TWO", actual = actual.values[1].name)
assertEquals(expected = MyTestEnum.TWO, actual = actual.values[1].value)
assertEquals(expected = "THREE", actual = actual.values[2].name)
assertEquals(expected = MyTestEnum.THREE, actual = actual.values[2].value)
assertEquals(expected = "customFour", actual = actual.values[3].name)
assertEquals(expected = MyTestEnum.FOUR, actual = actual.values[3].value)
}

@Test
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*
* Copyright 2020 Expedia, Inc
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.expediagroup.graphql.generator.test.integration

import com.expediagroup.graphql.generator.TopLevelObject
import com.expediagroup.graphql.generator.internal.types.GenerateEnumTest
import com.expediagroup.graphql.generator.testSchemaConfig
import com.expediagroup.graphql.generator.toSchema
import graphql.GraphQL
import org.junit.jupiter.api.Test
import kotlin.test.assertEquals

@Suppress(
"Detekt.UnusedPrivateMember",
)
class CustomEnumExecutionTest {

private val schema = toSchema(
queries = listOf(TopLevelObject(QueryObject())),
config = testSchemaConfig()
)
private val graphQL: GraphQL = GraphQL.newGraphQL(schema).build()

@Test
fun `a custom enum name can be used as output`() {
val result = graphQL.execute("{ enumOutputs }")
val data: Map<String, List<String>>? = result.getData()

assertEquals(emptyList(), result.errors)
assertEquals(listOf("ONE", "TWO", "THREE", "customFour"), data?.get("enumOutputs"))
}

@Test
fun `an implicit enum name be used as input`() {
val result = graphQL.execute("{ enumInput(value: THREE) }")
val data: Map<String, String>? = result.getData()

assertEquals(emptyList(), result.errors)
assertEquals("hello, THREE", data?.get("enumInput"))
}

@Test
fun `a custom enum name be used as input`() {
val result = graphQL.execute("{ enumInput(value: customFour) }")
val data: Map<String, String>? = result.getData()

assertEquals(emptyList(), result.errors)
assertEquals("hello, FOUR", data?.get("enumInput"))
}

class QueryObject {
fun enumOutputs(): List<GenerateEnumTest.MyTestEnum> = GenerateEnumTest.MyTestEnum.entries
fun enumInput(value: GenerateEnumTest.MyTestEnum) = "hello, $value"
}
}
Loading