-
Notifications
You must be signed in to change notification settings - Fork 381
feat: Add extension, public method for coercing DataFetchingEnvironment arguments with Kotlin reflection #2169
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
Closed
Closed
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 46 additions & 0 deletions
46
...kotlin/com/expediagroup/graphql/generator/extensions/DataFetchingEnvironmentExtensions.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| /* | ||
| * Copyright 2024 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.extensions | ||
|
|
||
| import com.expediagroup.graphql.generator.execution.convertInputMap | ||
| import graphql.schema.DataFetchingEnvironment | ||
| import kotlin.reflect.KClass | ||
|
|
||
| /** | ||
| * Coerces [DataFetchingEnvironment.getArguments] into a typed Kotlin object using Kotlin reflection. | ||
| * | ||
| * The target class must match the top-level shape of the arguments map: its constructor parameters | ||
| * must correspond to GraphQL argument names. | ||
| * | ||
| * Field names are resolved using [@GraphQLName][com.expediagroup.graphql.generator.annotations.GraphQLName] | ||
| * or the Kotlin parameter name β the same logic used to build the schema. Already-coerced values | ||
| * (e.g. custom scalars that graphql-java has already parsed) are passed through as-is. | ||
| * | ||
| * This is the same coercion path that [com.expediagroup.graphql.generator.execution.FunctionDataFetcher] | ||
| * uses internally for resolver parameters, and is the correct alternative to `ObjectMapper.convertValue` | ||
| * for use in instrumentation or custom data fetcher code. | ||
| */ | ||
| fun <T : Any> DataFetchingEnvironment.getArgumentsAs(targetClass: KClass<T>): T = | ||
| convertInputMap(arguments, targetClass) | ||
|
|
||
| /** | ||
| * Coerces [DataFetchingEnvironment.getArguments] into a typed Kotlin object using Kotlin reflection. | ||
| * | ||
| * @see getArgumentsAs | ||
| */ | ||
| inline fun <reified T : Any> DataFetchingEnvironment.getArgumentsAs(): T = | ||
| getArgumentsAs(T::class) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
81 changes: 81 additions & 0 deletions
81
...in/com/expediagroup/graphql/generator/extensions/DataFetchingEnvironmentExtensionsTest.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,81 @@ | ||
| /* | ||
| * Copyright 2024 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.extensions | ||
|
|
||
| import com.expediagroup.graphql.generator.annotations.GraphQLName | ||
| import graphql.schema.DataFetchingEnvironment | ||
| import io.mockk.every | ||
| import io.mockk.mockk | ||
| import org.junit.jupiter.api.Test | ||
| import kotlin.test.assertEquals | ||
|
|
||
| class DataFetchingEnvironmentExtensionsTest { | ||
|
|
||
| data class SimpleInput(val foo: String, val bar: String? = null) | ||
| data class RenamedInput(@GraphQLName("baz") val foo: String) | ||
| data class NestedInput(val inner: SimpleInput, val tag: String) | ||
|
|
||
| @Test | ||
| fun `getArgumentsAs coerces arguments to typed Kotlin object`() { | ||
| val environment = mockk<DataFetchingEnvironment> { | ||
| every { arguments } returns mapOf("foo" to "hello", "bar" to "world") | ||
| } | ||
|
|
||
| val result = environment.getArgumentsAs<SimpleInput>() | ||
|
|
||
| assertEquals("hello", result.foo) | ||
| assertEquals("world", result.bar) | ||
| } | ||
|
|
||
| @Test | ||
| fun `getArgumentsAs respects default parameter values for absent fields`() { | ||
| val environment = mockk<DataFetchingEnvironment> { | ||
| every { arguments } returns mapOf("foo" to "hello") | ||
| } | ||
|
|
||
| val result = environment.getArgumentsAs(SimpleInput::class) | ||
|
|
||
| assertEquals("hello", result.foo) | ||
| assertEquals(null, result.bar) | ||
| } | ||
|
|
||
| @Test | ||
| fun `getArgumentsAs resolves field names via GraphQLName`() { | ||
| val environment = mockk<DataFetchingEnvironment> { | ||
| every { arguments } returns mapOf("baz" to "renamed") | ||
| } | ||
|
|
||
| val result = environment.getArgumentsAs<RenamedInput>() | ||
|
|
||
| assertEquals("renamed", result.foo) | ||
| } | ||
|
|
||
| @Test | ||
| fun `getArgumentsAs passes through already-coerced field values`() { | ||
| // Simulates what environment.arguments looks like after graphql-java has run | ||
| // custom scalar coercers β the field value is already the target type, not a raw string. | ||
| val preCoerced = SimpleInput("already", "coerced") | ||
| val environment = mockk<DataFetchingEnvironment> { | ||
| every { arguments } returns mapOf("inner" to preCoerced, "tag" to "test") | ||
| } | ||
|
|
||
| val result = environment.getArgumentsAs<NestedInput>() | ||
|
|
||
| assertEquals(preCoerced, result.inner) | ||
| assertEquals("test", result.tag) | ||
| } | ||
| } |
138 changes: 138 additions & 0 deletions
138
...m/expediagroup/graphql/generator/test/integration/PlainKotlinInputWithCustomScalarTest.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,138 @@ | ||
| /* | ||
| * Copyright 2024 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.execution.convertInputMap | ||
| import com.expediagroup.graphql.generator.getTestSchemaConfigWithHooks | ||
| import com.expediagroup.graphql.generator.hooks.SchemaGeneratorHooks | ||
| import com.expediagroup.graphql.generator.test.utils.graphqlUUIDType | ||
| import com.expediagroup.graphql.generator.toSchema | ||
| import graphql.GraphQL | ||
| import graphql.schema.GraphQLType | ||
| import org.junit.jupiter.api.Test | ||
| import java.util.UUID | ||
| import kotlin.reflect.KClass | ||
| import kotlin.reflect.KType | ||
| import kotlin.test.assertEquals | ||
| import kotlin.test.assertNull | ||
|
|
||
| /** | ||
| * Verifies that plain Kotlin data classes whose fields include custom scalars are correctly | ||
| * coerced in both of the scenarios covered by this test class: | ||
| * | ||
| * 1. the resolver parameter path exercised end-to-end via [graphql.GraphQL.execute], where | ||
| * [com.expediagroup.graphql.generator.execution.FunctionDataFetcher] maps GraphQL input | ||
| * objects to Kotlin constructor parameters, and | ||
| * | ||
| * 2. the direct input-map conversion path exercised through | ||
| * [com.expediagroup.graphql.generator.execution.convertInputMap], which uses the same | ||
| * Kotlin reflection-based coercion logic for nested input objects and custom scalars. | ||
| * | ||
| * UUID is used as a stand-in for any custom scalar that graphql-java coerces before these | ||
| * coercion paths consume the input values. | ||
| */ | ||
| class PlainKotlinInputWithCustomScalarTest { | ||
|
|
||
| // No @GraphQLName β field names resolve to Kotlin parameter names. | ||
| data class RequestContext( | ||
| val requestId: UUID, | ||
| val userId: String, | ||
| val depth: Int = 0 | ||
| ) | ||
|
|
||
| data class NestedContext( | ||
| val outer: RequestContext, | ||
| val tag: String | ||
| ) | ||
|
|
||
| class ContextQuery { | ||
| fun processContext(context: RequestContext): String = | ||
| "id=${context.requestId},user=${context.userId},depth=${context.depth}" | ||
|
|
||
| fun processNestedContext(context: NestedContext): String = | ||
| "tag=${context.tag},id=${context.outer.requestId},user=${context.outer.userId}" | ||
| } | ||
|
|
||
| private val schema = toSchema( | ||
| queries = listOf(TopLevelObject(ContextQuery())), | ||
| config = getTestSchemaConfigWithHooks(object : SchemaGeneratorHooks { | ||
| override fun willGenerateGraphQLType(type: KType): GraphQLType? = | ||
| when (type.classifier as? KClass<*>) { | ||
| UUID::class -> graphqlUUIDType | ||
| else -> null | ||
| } | ||
| }) | ||
| ) | ||
|
|
||
| private val graphQL = GraphQL.newGraphQL(schema).build() | ||
|
|
||
| @Test | ||
| fun `plain data class with custom scalar field resolves correctly end-to-end`() { | ||
| val result = graphQL.execute( | ||
| """{ processContext(context: { requestId: "550e8400-e29b-41d4-a716-446655440000", userId: "alice", depth: 0 }) }""" | ||
| ) | ||
| assertNull(result.errors.firstOrNull(), "Expected no errors but got: ${result.errors}") | ||
| val data: Map<String, String> = result.getData() | ||
| assertEquals( | ||
| "id=550e8400-e29b-41d4-a716-446655440000,user=alice,depth=0", | ||
| data["processContext"] | ||
| ) | ||
| } | ||
|
|
||
| @Test | ||
| fun `plain data class with nested custom scalar resolves correctly end-to-end`() { | ||
| val result = graphQL.execute( | ||
| """{ processNestedContext(context: { outer: { requestId: "550e8400-e29b-41d4-a716-446655440000", userId: "bob", depth: 0 }, tag: "test" }) }""" | ||
| ) | ||
| assertNull(result.errors.firstOrNull(), "Expected no errors but got: ${result.errors}") | ||
| val data: Map<String, String> = result.getData() | ||
| assertEquals( | ||
| "tag=test,id=550e8400-e29b-41d4-a716-446655440000,user=bob", | ||
| data["processNestedContext"] | ||
| ) | ||
| } | ||
|
|
||
| @Test | ||
| fun `coercion correctly handles pre-coerced custom scalar field`() { | ||
| // This is the instrumentation scenario: by the time beginFieldFetch is called, | ||
| // graphql-java has already run the scalar coercer so the UUID field in | ||
| // environment.arguments is already a UUID object, not a string. | ||
| val preCoercedId = UUID.fromString("550e8400-e29b-41d4-a716-446655440000") | ||
| val result = convertInputMap( | ||
| mapOf("requestId" to preCoercedId, "userId" to "alice"), | ||
| RequestContext::class | ||
| ) | ||
|
|
||
| assertEquals(preCoercedId, result.requestId) | ||
| assertEquals("alice", result.userId) | ||
| assertEquals(0, result.depth) | ||
| } | ||
|
|
||
| @Test | ||
| fun `coercion correctly handles nested pre-coerced custom scalar field`() { | ||
| val preCoercedId = UUID.fromString("550e8400-e29b-41d4-a716-446655440000") | ||
| val result = convertInputMap( | ||
| mapOf("outer" to mapOf("requestId" to preCoercedId, "userId" to "bob"), "tag" to "instrumentation"), | ||
| NestedContext::class | ||
| ) | ||
|
|
||
| assertEquals(preCoercedId, result.outer.requestId) | ||
| assertEquals("bob", result.outer.userId) | ||
| assertEquals("instrumentation", result.tag) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.