Skip to content

Commit 6d4ab8b

Browse files
authored
[generator] support returning GraphQL ID scalar from functions 1.x.x (#670)
* [generator] support returning GraphQL ID scalar from functions backport to 1.x Our current logic was not checking whether function return type was annotated with @GraphQLID annotation so it was not possible to return ID scalar type. Related: * #669 * #668 * update GH Action to trigger against 1.x.x branch
1 parent 46a0f02 commit 6d4ab8b

File tree

4 files changed

+20
-2
lines changed

4 files changed

+20
-2
lines changed

.github/workflows/maven.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ name: Maven CI
33
on:
44
pull_request:
55
branches:
6-
- master
6+
- 1.x.x
77

88
jobs:
99
build:

examples/spring/src/main/kotlin/com/expediagroup/graphql/examples/query/ScalarQuery.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@ class ScalarQuery: Query {
3333
fun generateRandomUUID() = UUID.randomUUID()
3434

3535
fun findPersonById(@GraphQLID id: String) = Person(id, "Nelson")
36+
37+
@GraphQLID
38+
fun generateRandomId() = UUID.randomUUID().toString()
3639
}
3740

3841
@Component

graphql-kotlin-schema-generator/src/main/kotlin/com/expediagroup/graphql/generator/types/FunctionBuilder.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import com.expediagroup.graphql.generator.extensions.getDeprecationReason
2323
import com.expediagroup.graphql.generator.extensions.getFunctionName
2424
import com.expediagroup.graphql.generator.extensions.getGraphQLDescription
2525
import com.expediagroup.graphql.generator.extensions.getValidArguments
26+
import com.expediagroup.graphql.generator.extensions.isGraphQLID
2627
import com.expediagroup.graphql.generator.extensions.safeCast
2728
import com.expediagroup.graphql.generator.types.utils.getWrappedReturnType
2829
import graphql.schema.FieldCoordinates
@@ -53,7 +54,7 @@ internal class FunctionBuilder(generator: SchemaGenerator) : TypeBuilder(generat
5354

5455
val typeFromHooks = config.hooks.willResolveMonad(fn.returnType)
5556
val returnType = getWrappedReturnType(typeFromHooks)
56-
val graphQLOutputType = graphQLTypeOf(returnType).safeCast<GraphQLOutputType>()
57+
val graphQLOutputType = graphQLTypeOf(returnType, annotatedAsID = fn.isGraphQLID()).safeCast<GraphQLOutputType>()
5758
val graphQLType = builder.type(graphQLOutputType).build()
5859
val coordinates = FieldCoordinates.coordinates(parentName, functionName)
5960

graphql-kotlin-schema-generator/src/test/kotlin/com/expediagroup/graphql/generator/types/FunctionBuilderTest.kt

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package com.expediagroup.graphql.generator.types
1919
import com.expediagroup.graphql.annotations.GraphQLContext
2020
import com.expediagroup.graphql.annotations.GraphQLDescription
2121
import com.expediagroup.graphql.annotations.GraphQLDirective
22+
import com.expediagroup.graphql.annotations.GraphQLID
2223
import com.expediagroup.graphql.annotations.GraphQLIgnore
2324
import com.expediagroup.graphql.annotations.GraphQLName
2425
import com.expediagroup.graphql.exceptions.TypeNotSupportedException
@@ -107,6 +108,9 @@ internal class FunctionBuilderTest : TypeTestHelper() {
107108
val dataFetcherResult = DataFetcherResult.newResult<String>().data("Hello").build()
108109
return CompletableFuture.completedFuture(dataFetcherResult)
109110
}
111+
112+
@GraphQLID
113+
fun randomId() = UUID.randomUUID().toString()
110114
}
111115

112116
@Test
@@ -295,4 +299,14 @@ internal class FunctionBuilderTest : TypeTestHelper() {
295299
assertTrue(implResult.type is GraphQLNonNull)
296300
assertEquals(kInterfaceResult.type, implResult.type)
297301
}
302+
303+
@Test
304+
fun `function can return GraphQL ID scalar`() {
305+
val kFunction = Happy::randomId
306+
val result = builder.function(kFunction, "Query", target = null, abstract = false)
307+
308+
assertEquals("randomId", result.name)
309+
val returnType = GraphQLTypeUtil.unwrapAll(result.type)
310+
assertEquals(Scalars.GraphQLID, returnType)
311+
}
298312
}

0 commit comments

Comments
 (0)